< Summary

Information
Class: Rudim.Board.MoveOrdering
Assembly: Rudim
File(s): /home/runner/work/rudim/rudim/Rudim/Board/MoveOrdering.cs
Line coverage
95%
Covered lines: 44
Uncovered lines: 2
Coverable lines: 46
Total lines: 95
Line coverage: 95.6%
Branch coverage
100%
Covered branches: 18
Total branches: 18
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
PopulateMoveScore(...)100%88100%
AddKillerMove(...)100%22100%
AddHistoryMove(...)100%11100%
ResetMoveHeuristic()100%11100%
IsMoveHeuristicEmpty()100%22100%
PopulateHashMove(...)100%210%
SortNextBestMove(...)100%66100%

File(s)

/home/runner/work/rudim/rudim/Rudim/Board/MoveOrdering.cs

#LineLine coverage
 1using Rudim.Common;
 2using System.Collections.Generic;
 3using System.Linq;
 4
 5namespace Rudim.Board
 6{
 7    public static class MoveOrdering
 8    {
 9        private static readonly int[,] MostValuableVictimLeastValuableAttacker;
 10        private static Move[,] _killerMoves;
 11        private static int[,] _historyMoves;
 12
 13        static MoveOrdering()
 14        {
 115            MostValuableVictimLeastValuableAttacker = new[,]
 116            {
 117                // P , N , B , R , Q , K , None
 118                { 15_000, 14_000, 13_000, 12_000, 11_000, 10_000, 0 }, // P
 119                { 25_000, 24_000, 23_000, 22_000, 21_000, 20_000, 0 }, // N
 120                { 35_000, 34_000, 33_000, 32_000, 31_000, 30_000, 0 }, // B
 121                { 45_000, 44_000, 43_000, 42_000, 41_000, 40_000, 0 }, // R
 122                { 55_000, 54_000, 53_000, 52_000, 51_000, 50_000, 0 }, // Q
 123                { 65_000, 64_000, 63_000, 62_000, 61_000, 60_000, 0 }, // K
 124                { 0, 0, 0, 0, 0, 0, 0 } // None
 125            };
 126            ResetMoveHeuristic();
 127        }
 28
 29        public static void PopulateMoveScore(Move move, BoardState boardState, int ply = Constants.MaxPly - 1)
 30        {
 6228154631            if (!move.IsCapture())
 32            {
 5533612033                if (move == _killerMoves[0, ply])
 36496734                    move.Score = 9000; // TODO : Revisit, assign better values and extract to constants
 5497115335                else if (move == _killerMoves[1, ply])
 27600436                    move.Score = 8000;
 37                else
 5469514938                    move.Score = _historyMoves[boardState.GetPieceOn(move.Source), (int)move.Target];
 5469514939                return;
 40            }
 41            int targetPiece;
 694542642            int sourcePiece = boardState.GetPieceOn(move.Source, boardState.SideToMove);
 694542643            if (move.Type == MoveTypes.EnPassant)
 1671044                targetPiece = (int)Piece.Pawn;
 45            else
 692871646                targetPiece = boardState.GetPieceOn(move.Target, boardState.SideToMove.Other());
 694542647            move.Score = MostValuableVictimLeastValuableAttacker[targetPiece, sourcePiece];
 694542648        }
 49
 50        public static void AddKillerMove(Move move, int ply)
 51        {
 6955752            if (_killerMoves[0, ply] == move)
 53            {
 6245354                return;
 55            }
 56
 710457            _killerMoves[1, ply] = _killerMoves[0, ply];
 710458            _killerMoves[0, ply] = move;
 710459        }
 60
 61        public static void AddHistoryMove(int piece, Move move, int depth)
 62        {
 112063            _historyMoves[piece, (int)move.Target] += depth * depth;
 112064        }
 65
 66        public static void ResetMoveHeuristic()
 67        {
 1068            _killerMoves = new Move[Constants.Sides, Constants.MaxPly];
 1069            _historyMoves = new int[Constants.Pieces * 2, Constants.Squares];
 1070        }
 71
 72        public static bool IsMoveHeuristicEmpty()
 73        {
 89974            return _killerMoves.Cast<Move>().All(move => move == null) && _historyMoves.Cast<int>().All(move => move == 
 75        }
 76
 77        public static void PopulateHashMove(Move move)
 78        {
 079            move.Score = 10_500;
 080        }
 81
 82        public static void SortNextBestMove(List<Move> moves, int startingIndex)
 83        {
 639642484            int bestIndex = startingIndex;
 38824179885            for (int i = startingIndex + 1; i < moves.Count; ++i)
 86            {
 18772447587                if (moves[i].Score >= moves[bestIndex].Score)
 4613833488                    bestIndex = i;
 89            }
 90
 639642491            if(bestIndex != startingIndex)
 619150092                (moves[bestIndex], moves[startingIndex]) = (moves[startingIndex], moves[bestIndex]);
 639642493        }
 94    }
 95}