< Summary

Information
Class: Rudim.Board.PieceSquareTableEvaluation
Assembly: Rudim
File(s): /home/runner/work/rudim/rudim/Rudim/Board/PieceSquareTableEvaluation.cs
Line coverage
100%
Covered lines: 169
Uncovered lines: 0
Coverable lines: 169
Total lines: 216
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        {
 383968013            int score = 0;
 14
 383968015            score += ScorePosition(boardState);
 383968016            score += PawnStructureEvaluation.Evaluate(boardState);
 17
 383968018            return boardState.SideToMove == Side.White ? score : -score;
 19        }
 20
 21        private static int ScorePosition(BoardState boardState)
 22        {
 383968023            int positionalScore = 0;
 383968024            int midGamePhase = boardState.ClippedPhase;
 383968025            int endGamePhase = GamePhase.TotalPhase - midGamePhase;
 5375552026            for (int piece = 0; piece < Constants.Pieces; ++piece)
 27            {
 2303808028                Bitboard whiteBoard = new(boardState.Pieces[(int)Side.White, piece].Board);
 2303808029                Bitboard blackBoard = new(boardState.Pieces[(int)Side.Black, piece].Board);
 30
 7272680231                while (whiteBoard.Board > 0)
 32                {
 4968872233                    int square = whiteBoard.GetLsb();
 4968872234                    whiteBoard.ClearBit(square);
 4968872235                    positionalScore += (MidGamePositions[piece, square] * midGamePhase) + (EndGamePositions[piece, squar
 36                }
 37
 7163856638                while (blackBoard.Board > 0)
 39                {
 4860048640                    int square = blackBoard.GetLsb();
 4860048641                    blackBoard.ClearBit(square);
 4860048642                    square = MirrorSquare(square);
 4860048643                    positionalScore -=  (MidGamePositions[piece, square] * midGamePhase) + (EndGamePositions[piece, squa
 44                }
 45            }
 46
 383968047            positionalScore = (int)(positionalScore * GamePhase.PhaseFactor);
 383968048            return positionalScore;
 49        }
 50
 51        private static int MirrorSquare(int square)
 52        {
 4860048653            int row = square >> 3; // Square / 8
 4860048654            int col = square & (8 - 1); // Square % 8
 55
 4860048656            return ((7 - row) << 3) + col; // (7 - Row) * 8 + col
 57        }
 58
 59        static PieceSquareTableEvaluation()
 60        {
 161            int[] midGamePieceValues = [82, 337, 365, 477, 1025,  0];
 162            int[] endGamePieceValues = [94, 281, 297, 512,  936,  0];
 63
 64            // Values borrowed from Rofchade
 65            // http://www.talkchess.com/forum3/viewtopic.php?f=2&t=68311&start=19
 166            MidGamePositions = new[,]
 167            {
 168                // Pawn
 169                {
 170                0,   0,   0,   0,   0,   0,  0,   0,
 171                98, 134,  61,  95,  68, 126, 34, -11,
 172                -6,   7,  26,  31,  65,  56, 25, -20,
 173                -14,  13,   6,  21,  23,  12, 17, -23,
 174                -27,  -2,  -5,  12,  17,   6, 10, -25,
 175                -26,  -4,  -4, -10,   3,   3, 33, -12,
 176                -35,  -1, -20, -23, -15,  24, 38, -22,
 177                0,   0,   0,   0,   0,   0,  0,   0
 178                },
 179                // Knight
 180               {
 181                -167, -89, -34, -49,  61, -97, -15, -107,
 182                -73, -41,  72,  36,  23,  62,   7,  -17,
 183                -47,  60,  37,  65,  84, 129,  73,   44,
 184                -9,  17,  19,  53,  37,  69,  18,   22,
 185                -13,   4,  16,  13,  28,  19,  21,   -8,
 186                -23,  -9,  12,  10,  19,  17,  25,  -16,
 187                -29, -53, -12,  -3,  -1,  18, -14,  -19,
 188                -105, -21, -58, -33, -17, -28, -19,  -23,
 189               },
 190               // Bishop
 191               {
 192                -29,   4, -82, -37, -25, -42,   7,  -8,
 193                -26,  16, -18, -13,  30,  59,  18, -47,
 194                -16,  37,  43,  40,  35,  50,  37,  -2,
 195                -4,   5,  19,  50,  37,  37,   7,  -2,
 196                -6,  13,  13,  26,  34,  12,  10,   4,
 197                0,  15,  15,  15,  14,  27,  18,  10,
 198                4,  15,  16,   0,   7,  21,  33,   1,
 199                -33,  -3, -14, -21, -13, -12, -39, -21,
 1100               },
 1101               // Rook
 1102               {
 1103                32,  42,  32,  51, 63,  9,  31,  43,
 1104                27,  32,  58,  62, 80, 67,  26,  44,
 1105                -5,  19,  26,  36, 17, 45,  61,  16,
 1106                -24, -11,   7,  26, 24, 35,  -8, -20,
 1107                -36, -26, -12,  -1,  9, -7,   6, -23,
 1108                -45, -25, -16, -17,  3,  0,  -5, -33,
 1109                -44, -16, -20,  -9, -1, 11,  -6, -71,
 1110                -19, -13,   1,  17, 16,  7, -37, -26,
 1111               },
 1112               // Queen
 1113               {
 1114                -28,   0,  29,  12,  59,  44,  43,  45,
 1115                -24, -39,  -5,   1, -16,  57,  28,  54,
 1116                -13, -17,   7,   8,  29,  56,  47,  57,
 1117                -27, -27, -16, -16,  -1,  17,  -2,   1,
 1118                -9, -26,  -9, -10,  -2,  -4,   3,  -3,
 1119                -14,   2, -11,  -2,  -5,   2,  14,   5,
 1120                -35,  -8,  11,   2,   8,  15,  -3,   1,
 1121                -1, -18,  -9,  10, -15, -25, -31, -50,
 1122               },
 1123               // King
 1124               {
 1125                -65,  23,  16, -15, -56, -34,   2,  13,
 1126                29,  -1, -20,  -7,  -8,  -4, -38, -29,
 1127                -9,  24,   2, -16, -20,   6,  22, -22,
 1128                -17, -20, -12, -27, -30, -25, -14, -36,
 1129                -49,  -1, -27, -39, -46, -44, -33, -51,
 1130                -14, -14, -22, -46, -44, -30, -15, -27,
 1131                1,   7,  -8, -64, -43, -16,   9,   8,
 1132                -15,  36,  12, -54,   8, -28,  24,  14,
 1133               }
 1134            };
 135
 1136            EndGamePositions = new[,]
 1137            {
 1138                // Pawn
 1139                {
 1140                0,   0,   0,   0,   0,   0,   0,   0,
 1141                178, 173, 158, 134, 147, 132, 165, 187,
 1142                94, 100,  85,  67,  56,  53,  82,  84,
 1143                32,  24,  13,   5,  -2,   4,  17,  17,
 1144                13,   9,  -3,  -7,  -7,  -8,   3,  -1,
 1145                4,   7,  -6,   1,   0,  -5,  -1,  -8,
 1146                13,   8,   8,  10,  13,   0,   2,  -7,
 1147                0,   0,   0,   0,   0,   0,   0,   0,
 1148                },
 1149                // Knight
 1150               {
 1151                -58, -38, -13, -28, -31, -27, -63, -99,
 1152                -25,  -8, -25,  -2,  -9, -25, -24, -52,
 1153                -24, -20,  10,   9,  -1,  -9, -19, -41,
 1154                -17,   3,  22,  22,  22,  11,   8, -18,
 1155                -18,  -6,  16,  25,  16,  17,   4, -18,
 1156                -23,  -3,  -1,  15,  10,  -3, -20, -22,
 1157                -42, -20, -10,  -5,  -2, -20, -23, -44,
 1158                -29, -51, -23, -15, -22, -18, -50, -64,
 1159               },
 1160               // Bishop
 1161               {
 1162                -14, -21, -11,  -8, -7,  -9, -17, -24,
 1163                -8,  -4,   7, -12, -3, -13,  -4, -14,
 1164                2,  -8,   0,  -1, -2,   6,   0,   4,
 1165                -3,   9,  12,   9, 14,  10,   3,   2,
 1166                -6,   3,  13,  19,  7,  10,  -3,  -9,
 1167                -12,  -3,   8,  10, 13,   3,  -7, -15,
 1168                -14, -18,  -7,  -1,  4,  -9, -15, -27,
 1169                -23,  -9, -23,  -5, -9, -16,  -5, -17,
 1170               },
 1171               // Rook
 1172               {
 1173                   13, 10, 18, 15, 12,  12,   8,   5,
 1174                   11, 13, 13, 11, -3,   3,   8,   3,
 1175                   7,  7,  7,  5,  4,  -3,  -5,  -3,
 1176                   4,  3, 13,  1,  2,   1,  -1,   2,
 1177                   3,  5,  8,  4, -5,  -6,  -8, -11,
 1178                   -4,  0, -5, -1, -7, -12,  -8, -16,
 1179                   -6, -6,  0,  2, -9,  -9, -11,  -3,
 1180                   -9,  2,  3, -1, -5, -13,   4, -20,
 1181               },
 1182               // Queen
 1183               {
 1184                -9,  22,  22,  27,  27,  19,  10,  20,
 1185                -17,  20,  32,  41,  58,  25,  30,   0,
 1186                -20,   6,   9,  49,  47,  35,  19,   9,
 1187                3,  22,  24,  45,  57,  40,  57,  36,
 1188                -18,  28,  19,  47,  31,  34,  39,  23,
 1189                -16, -27,  15,   6,   9,  17,  10,   5,
 1190                -22, -23, -30, -16, -16, -23, -36, -32,
 1191                -33, -28, -22, -43,  -5, -32, -20, -41,
 1192               },
 1193               // King
 1194               {
 1195                -74, -35, -18, -18, -11,  15,   4, -17,
 1196                -12,  17,  14,  17,  17,  38,  23,  11,
 1197                10,  17,  23,  15,  20,  45,  44,  13,
 1198                -8,  22,  24,  27,  26,  33,  26,   3,
 1199                -18,  -4,  21,  24,  27,  23,   9, -11,
 1200                -19,  -3,  11,  21,  23,  16,   7,  -9,
 1201                -27, -11,   4,  13,  14,   4,  -5, -17,
 1202                -53, -34, -21, -11, -28, -14, -24, -43
 1203               }
 1204            };
 205
 14206            for (int piece = 0; piece < Constants.Pieces; ++piece)
 207            {
 780208                for (int square = 0; square < Constants.Squares; ++square)
 209                {
 384210                    MidGamePositions[piece, square] += midGamePieceValues[piece];
 384211                    EndGamePositions[piece, square] += endGamePieceValues[piece];
 212                }
 213            }
 1214        }
 215    }
 216}