< Summary

Information
Class: Rudim.Board.PieceSquareTableEvaluation
Assembly: Rudim
File(s): /home/runner/work/rudim/rudim/Rudim/Board/PieceSquareTableEvaluation.cs
Line coverage
100%
Covered lines: 168
Uncovered lines: 0
Coverable lines: 168
Total lines: 215
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Evaluate(...)100%22100%
ScorePosition(...)100%66100%
MirrorSquare(...)100%11100%
.cctor()100%44100%

File(s)

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

#LineLine coverage
 1using Rudim.Common;
 2
 3namespace Rudim.Board
 4{
 5    public static class PieceSquareTableEvaluation
 6    {
 7        private static readonly int[,] MidGamePositions;
 8        private static readonly int[,] EndGamePositions;
 9
 10
 11        public static int Evaluate(BoardState boardState)
 12        {
 430596813            int score = 0;
 14
 430596815            score += ScorePosition(boardState);
 16
 430596817            return boardState.SideToMove == Side.White ? score : -score;
 18        }
 19
 20        private static int ScorePosition(BoardState boardState)
 21        {
 430596822            int positionalScore = 0;
 430596823            int midGamePhase = boardState.ClippedPhase;
 430596824            int endGamePhase = GamePhase.TotalPhase - midGamePhase;
 6028355225            for (int piece = 0; piece < Constants.Pieces; ++piece)
 26            {
 2583580827                Bitboard whiteBoard = new(boardState.Pieces[(int)Side.White, piece].Board);
 2583580828                Bitboard blackBoard = new(boardState.Pieces[(int)Side.Black, piece].Board);
 29
 8107743230                while (whiteBoard.Board > 0)
 31                {
 5524162432                    int square = whiteBoard.GetLsb();
 5524162433                    whiteBoard.ClearBit(square);
 5524162434                    positionalScore += (MidGamePositions[piece, square] * midGamePhase) + (EndGamePositions[piece, squar
 35                }
 36
 8024172637                while (blackBoard.Board > 0)
 38                {
 5440591839                    int square = blackBoard.GetLsb();
 5440591840                    blackBoard.ClearBit(square);
 5440591841                    square = MirrorSquare(square);
 5440591842                    positionalScore -=  (MidGamePositions[piece, square] * midGamePhase) + (EndGamePositions[piece, squa
 43                }
 44            }
 45
 430596846            positionalScore = (int)(positionalScore * GamePhase.PhaseFactor);
 430596847            return positionalScore;
 48        }
 49
 50        private static int MirrorSquare(int square)
 51        {
 5440591852            int row = square >> 3; // Square / 8
 5440591853            int col = square & (8 - 1); // Square % 8
 54
 5440591855            return ((7 - row) << 3) + col; // (7 - Row) * 8 + col
 56        }
 57
 58        static PieceSquareTableEvaluation()
 59        {
 160            int[] midGamePieceValues = [82, 337, 365, 477, 1025,  0];
 161            int[] endGamePieceValues = [94, 281, 297, 512,  936,  0];
 62
 63            // Values borrowed from Rofchade
 64            // http://www.talkchess.com/forum3/viewtopic.php?f=2&t=68311&start=19
 165            MidGamePositions = new[,]
 166            {
 167                // Pawn
 168                {
 169                0,   0,   0,   0,   0,   0,  0,   0,
 170                98, 134,  61,  95,  68, 126, 34, -11,
 171                -6,   7,  26,  31,  65,  56, 25, -20,
 172                -14,  13,   6,  21,  23,  12, 17, -23,
 173                -27,  -2,  -5,  12,  17,   6, 10, -25,
 174                -26,  -4,  -4, -10,   3,   3, 33, -12,
 175                -35,  -1, -20, -23, -15,  24, 38, -22,
 176                0,   0,   0,   0,   0,   0,  0,   0
 177                },
 178                // Knight
 179               {
 180                -167, -89, -34, -49,  61, -97, -15, -107,
 181                -73, -41,  72,  36,  23,  62,   7,  -17,
 182                -47,  60,  37,  65,  84, 129,  73,   44,
 183                -9,  17,  19,  53,  37,  69,  18,   22,
 184                -13,   4,  16,  13,  28,  19,  21,   -8,
 185                -23,  -9,  12,  10,  19,  17,  25,  -16,
 186                -29, -53, -12,  -3,  -1,  18, -14,  -19,
 187                -105, -21, -58, -33, -17, -28, -19,  -23,
 188               },
 189               // Bishop
 190               {
 191                -29,   4, -82, -37, -25, -42,   7,  -8,
 192                -26,  16, -18, -13,  30,  59,  18, -47,
 193                -16,  37,  43,  40,  35,  50,  37,  -2,
 194                -4,   5,  19,  50,  37,  37,   7,  -2,
 195                -6,  13,  13,  26,  34,  12,  10,   4,
 196                0,  15,  15,  15,  14,  27,  18,  10,
 197                4,  15,  16,   0,   7,  21,  33,   1,
 198                -33,  -3, -14, -21, -13, -12, -39, -21,
 199               },
 1100               // Rook
 1101               {
 1102                32,  42,  32,  51, 63,  9,  31,  43,
 1103                27,  32,  58,  62, 80, 67,  26,  44,
 1104                -5,  19,  26,  36, 17, 45,  61,  16,
 1105                -24, -11,   7,  26, 24, 35,  -8, -20,
 1106                -36, -26, -12,  -1,  9, -7,   6, -23,
 1107                -45, -25, -16, -17,  3,  0,  -5, -33,
 1108                -44, -16, -20,  -9, -1, 11,  -6, -71,
 1109                -19, -13,   1,  17, 16,  7, -37, -26,
 1110               },
 1111               // Queen
 1112               {
 1113                -28,   0,  29,  12,  59,  44,  43,  45,
 1114                -24, -39,  -5,   1, -16,  57,  28,  54,
 1115                -13, -17,   7,   8,  29,  56,  47,  57,
 1116                -27, -27, -16, -16,  -1,  17,  -2,   1,
 1117                -9, -26,  -9, -10,  -2,  -4,   3,  -3,
 1118                -14,   2, -11,  -2,  -5,   2,  14,   5,
 1119                -35,  -8,  11,   2,   8,  15,  -3,   1,
 1120                -1, -18,  -9,  10, -15, -25, -31, -50,
 1121               },
 1122               // King
 1123               {
 1124                -65,  23,  16, -15, -56, -34,   2,  13,
 1125                29,  -1, -20,  -7,  -8,  -4, -38, -29,
 1126                -9,  24,   2, -16, -20,   6,  22, -22,
 1127                -17, -20, -12, -27, -30, -25, -14, -36,
 1128                -49,  -1, -27, -39, -46, -44, -33, -51,
 1129                -14, -14, -22, -46, -44, -30, -15, -27,
 1130                1,   7,  -8, -64, -43, -16,   9,   8,
 1131                -15,  36,  12, -54,   8, -28,  24,  14,
 1132               }
 1133            };
 134
 1135            EndGamePositions = new[,]
 1136            {
 1137                // Pawn
 1138                {
 1139                0,   0,   0,   0,   0,   0,   0,   0,
 1140                178, 173, 158, 134, 147, 132, 165, 187,
 1141                94, 100,  85,  67,  56,  53,  82,  84,
 1142                32,  24,  13,   5,  -2,   4,  17,  17,
 1143                13,   9,  -3,  -7,  -7,  -8,   3,  -1,
 1144                4,   7,  -6,   1,   0,  -5,  -1,  -8,
 1145                13,   8,   8,  10,  13,   0,   2,  -7,
 1146                0,   0,   0,   0,   0,   0,   0,   0,
 1147                },
 1148                // Knight
 1149               {
 1150                -58, -38, -13, -28, -31, -27, -63, -99,
 1151                -25,  -8, -25,  -2,  -9, -25, -24, -52,
 1152                -24, -20,  10,   9,  -1,  -9, -19, -41,
 1153                -17,   3,  22,  22,  22,  11,   8, -18,
 1154                -18,  -6,  16,  25,  16,  17,   4, -18,
 1155                -23,  -3,  -1,  15,  10,  -3, -20, -22,
 1156                -42, -20, -10,  -5,  -2, -20, -23, -44,
 1157                -29, -51, -23, -15, -22, -18, -50, -64,
 1158               },
 1159               // Bishop
 1160               {
 1161                -14, -21, -11,  -8, -7,  -9, -17, -24,
 1162                -8,  -4,   7, -12, -3, -13,  -4, -14,
 1163                2,  -8,   0,  -1, -2,   6,   0,   4,
 1164                -3,   9,  12,   9, 14,  10,   3,   2,
 1165                -6,   3,  13,  19,  7,  10,  -3,  -9,
 1166                -12,  -3,   8,  10, 13,   3,  -7, -15,
 1167                -14, -18,  -7,  -1,  4,  -9, -15, -27,
 1168                -23,  -9, -23,  -5, -9, -16,  -5, -17,
 1169               },
 1170               // Rook
 1171               {
 1172                   13, 10, 18, 15, 12,  12,   8,   5,
 1173                   11, 13, 13, 11, -3,   3,   8,   3,
 1174                   7,  7,  7,  5,  4,  -3,  -5,  -3,
 1175                   4,  3, 13,  1,  2,   1,  -1,   2,
 1176                   3,  5,  8,  4, -5,  -6,  -8, -11,
 1177                   -4,  0, -5, -1, -7, -12,  -8, -16,
 1178                   -6, -6,  0,  2, -9,  -9, -11,  -3,
 1179                   -9,  2,  3, -1, -5, -13,   4, -20,
 1180               },
 1181               // Queen
 1182               {
 1183                -9,  22,  22,  27,  27,  19,  10,  20,
 1184                -17,  20,  32,  41,  58,  25,  30,   0,
 1185                -20,   6,   9,  49,  47,  35,  19,   9,
 1186                3,  22,  24,  45,  57,  40,  57,  36,
 1187                -18,  28,  19,  47,  31,  34,  39,  23,
 1188                -16, -27,  15,   6,   9,  17,  10,   5,
 1189                -22, -23, -30, -16, -16, -23, -36, -32,
 1190                -33, -28, -22, -43,  -5, -32, -20, -41,
 1191               },
 1192               // King
 1193               {
 1194                -74, -35, -18, -18, -11,  15,   4, -17,
 1195                -12,  17,  14,  17,  17,  38,  23,  11,
 1196                10,  17,  23,  15,  20,  45,  44,  13,
 1197                -8,  22,  24,  27,  26,  33,  26,   3,
 1198                -18,  -4,  21,  24,  27,  23,   9, -11,
 1199                -19,  -3,  11,  21,  23,  16,   7,  -9,
 1200                -27, -11,   4,  13,  14,   4,  -5, -17,
 1201                -53, -34, -21, -11, -28, -14, -24, -43
 1202               }
 1203            };
 204
 14205            for (int piece = 0; piece < Constants.Pieces; ++piece)
 206            {
 780207                for (int square = 0; square < Constants.Squares; ++square)
 208                {
 384209                    MidGamePositions[piece, square] += midGamePieceValues[piece];
 384210                    EndGamePositions[piece, square] += endGamePieceValues[piece];
 211                }
 212            }
 1213        }
 214    }
 215}