diff --git a/source/evaluate.cpp b/source/evaluate.cpp index dec8472..0433c61 100644 --- a/source/evaluate.cpp +++ b/source/evaluate.cpp @@ -1,14 +1,41 @@ #include "evaluate.h" +static int GetMaterialScore(Board &board, int type, int color) { + int side = color ? -1 : 1; + + return (board.pieces[type] & board.colors[color]).PopCount() + * materialValues[type] * side; +} + +static int GetPositionalScore(Board &board, int type, int color) { + int score = 0; + int side = color ? -1 : 1; + + Bitboard pieces = board.pieces[type] & board.colors[color]; + + while (pieces.GetBoard()) { + int square = pieces.getLS1BIndex(); + + // White + if (!color) { + score += pieceSquareScores[type][scoreSquares[square]] * side; + } else { + score += pieceSquareScores[type][square] * side; + } + + pieces.PopBit(square); + } + + return score; +} + int Evaluate(Board &board) { int score = 0; for (int color = White; color <= Black; color++) { for (int type = Pawn; type <= King; type++) { - int side = color ? -1 : 1; - - score += (board.pieces[type] & board.colors[color]).PopCount() - * materialValues[type] * side; + score += GetMaterialScore(board, type, color); + score += GetPositionalScore(board, type, color); } } diff --git a/source/evaluate.h b/source/evaluate.h index a2a4936..8a03a01 100644 --- a/source/evaluate.h +++ b/source/evaluate.h @@ -1,7 +1,7 @@ #pragma once #include "board.h" -constexpr int materialValues[] { +constexpr int materialValues[6] { 100, // Pawn 300, // Knight 350, // Bishop @@ -10,4 +10,87 @@ constexpr int materialValues[] { 10000 // King }; +constexpr int pieceSquareScores[7][64] { + // Pawn + {0, 0, 0, 0, 0, 0, 0, 0, + 50, 50, 50, 50, 50, 50, 50, 50, + 10, 10, 20, 30, 30, 20, 10, 10, + 5, 5, 10, 25, 25, 10, 5, 5, + 0, 0, 0, 20, 20, 0, 0, 0, + 5, -5,-10, 0, 0,-10, -5, 5, + 5, 10, 10,-20,-20, 10, 10, 5, + 0, 0, 0, 0, 0, 0, 0, 0}, + + // Knight + {-50,-40,-30,-30,-30,-30,-40,-50, + -40,-20, 0, 0, 0, 0,-20,-40, + -30, 0, 10, 15, 15, 10, 0,-30, + -30, 5, 15, 20, 20, 15, 5,-30, + -30, 0, 15, 20, 20, 15, 0,-30, + -30, 5, 10, 15, 15, 10, 5,-30, + -40,-20, 0, 5, 5, 0,-20,-40, + -50,-40,-30,-30,-30,-30,-40,-50}, + + // Bishop + {-20,-10,-10,-10,-10,-10,-10,-20, + -10, 0, 0, 0, 0, 0, 0,-10, + -10, 0, 5, 10, 10, 5, 0,-10, + -10, 5, 5, 10, 10, 5, 5,-10, + -10, 0, 10, 10, 10, 10, 0,-10, + -10, 10, 10, 10, 10, 10, 10,-10, + -10, 5, 0, 0, 0, 0, 5,-10, + -20,-10,-10,-10,-10,-10,-10,-20}, + + // Rook + {0, 0, 0, 0, 0, 0, 0, 0, + 5, 10, 10, 10, 10, 10, 10, 5, + -5, 0, 0, 0, 0, 0, 0, -5, + -5, 0, 0, 0, 0, 0, 0, -5, + -5, 0, 0, 0, 0, 0, 0, -5, + -5, 0, 0, 0, 0, 0, 0, -5, + -5, 0, 0, 0, 0, 0, 0, -5, + 0, 0, 0, 5, 5, 0, 0, 0}, + + // Queen + {-20,-10,-10, -5, -5,-10,-10,-20, + -10, 0, 0, 0, 0, 0, 0,-10, + -10, 0, 5, 5, 5, 5, 0,-10, + -5, 0, 5, 5, 5, 5, 0, -5, + 0, 0, 5, 5, 5, 5, 0, -5, + -10, 5, 5, 5, 5, 5, 0,-10, + -10, 0, 5, 0, 0, 0, 0,-10, + -20,-10,-10, -5, -5,-10,-10,-20}, + + // King middle game + {-30,-40,-40,-50,-50,-40,-40,-30, + -30,-40,-40,-50,-50,-40,-40,-30, + -30,-40,-40,-50,-50,-40,-40,-30, + -30,-40,-40,-50,-50,-40,-40,-30, + -20,-30,-30,-40,-40,-30,-30,-20, + -10,-20,-20,-20,-20,-20,-20,-10, + 20, 20, 0, 0, 0, 0, 20, 20, + 20, 30, 10, 0, 0, 10, 30, 20}, + + // King end game + {-30,-40,-40,-50,-50,-40,-40,-30, + -30,-40,-40,-50,-50,-40,-40,-30, + -30,-40,-40,-50,-50,-40,-40,-30, + -30,-40,-40,-50,-50,-40,-40,-30, + -20,-30,-30,-40,-40,-30,-30,-20, + -10,-20,-20,-20,-20,-20,-20,-10, + 20, 20, 0, 0, 0, 0, 20, 20, + 20, 30, 10, 0, 0, 10, 30, 20} +}; + +constexpr int scoreSquares[64] { + a8, b8, c8, d8, e8, f8, g8, h8, + a7, b7, c7, d7, e7, f7, g7, h7, + a6, b6, c6, d6, e6, f6, g6, h6, + a5, b5, c5, d5, e5, f5, g5, h5, + a4, b4, c4, d4, e4, f4, g4, h4, + a3, b3, c3, d3, e3, f3, g3, h3, + a2, b2, c2, d2, e2, f2, g2, h2, + a1, b1, c1, d1, e1, f1, g1, h1 +}; + int Evaluate(Board &board); \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index c3c0107..0e775e8 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -9,9 +9,9 @@ int main() { initLeaperAttacks(); initSliderAttacks(); - Board board; - + Board board; + std::cout << Evaluate(board) << '\n'; - + return 0; } \ No newline at end of file