diff options
| -rw-r--r-- | src/board.cpp | 14 | ||||
| -rw-r--r-- | src/bot.cpp | 25 | ||||
| -rw-r--r-- | src/bot.hpp | 2 | ||||
| -rw-r--r-- | src/moves.cpp | 6 |
4 files changed, 26 insertions, 21 deletions
diff --git a/src/board.cpp b/src/board.cpp index f78d429..e49aaf7 100644 --- a/src/board.cpp +++ b/src/board.cpp @@ -41,12 +41,8 @@ void printBoard(Game *b) { std::string whiteLetters = " a b c d e f g h \n"; std::string blackLetters = " h g f e d c b a \n"; - // const char *lightSquare = "\033[48;5;145m"; - // // const char *darkSquare = "\033[48;5;28m"; - // const char *lightSquare = "\033[48;5;240m"; // Dark gray - // const char *darkSquare = "\033[48;5;22m"; // Forest green - const char *lightSquare = "\033[48;5;245m"; // Medium gray - const char *darkSquare = "\033[48;5;29m"; // Rich green + const char *lightSquare = "\033[48;5;245m"; + const char *darkSquare = "\033[48;5;29m"; const char *reset = "\033[0m"; std::cout << (b->turn ? whiteLetters : blackLetters); @@ -98,7 +94,7 @@ void printBoard(Game *b) { 8 - (int)b->enPassant.rank); std::printf("Move clock: %d\n", b->MoveClock); std::printf("Half move clock: %d\n", b->halfMoveClock); - std::printf("Board Eval: %f\n", EvaluateBoardForWhite(b)); + std::printf("Board Eval: %f\n", EvaluateBoardForWhite(b, 0)); } void PlayMove(Move move, Game *b) { @@ -241,12 +237,12 @@ UndoMove MakeMove(Move move, Game *g) { undo.zobristKey = key; - g->ThreeFoldMap[key]++; - if (g->ThreeFoldMap[key] >= 3) { g->state = DRAW; } + g->ThreeFoldMap[key]++; + if (g->halfMoveClock >= 50) { g->state = DRAW; } diff --git a/src/bot.cpp b/src/bot.cpp index 4137f05..dc58fe0 100644 --- a/src/bot.cpp +++ b/src/bot.cpp @@ -57,7 +57,7 @@ int ROOK_TABLE[64] = { 0, 0, 5, 10, 10, 5, 0, 0, // }; -const int SEARCH_DEPTH = 4; +const int SEARCH_DEPTH = 5; #include <fstream> @@ -89,6 +89,7 @@ Move EngineGetBestMove(Game *b) { float beta = INFINITY; float eval = minimax(SEARCH_DEPTH - 1, &TestBoard, alpha, beta); + if (b->turn) { if (eval > BestEval) { BestEval = eval; @@ -105,11 +106,11 @@ Move EngineGetBestMove(Game *b) { } float minimax(int depth, Game *b, float alpha, float beta) { if (depth == 0) { - return EvaluateBoardForWhite(b); + return EvaluateBoardForWhite(b, depth); } auto moves = GetLegalMoves(b); if (moves.size() == 0) { - return EvaluateBoardForWhite(b); + return EvaluateBoardForWhite(b, depth); } if (b->turn) { float bestEval = -INFINITY; @@ -118,7 +119,11 @@ float minimax(int depth, Game *b, float alpha, float beta) { Game next = *b; PlayMove(move, &next); - float eval = minimax(depth - 1, &next, alpha, beta); + int newDepth = depth - 1; + if (IsPieceTypeAttacked(b, KING, b->turn)) { + newDepth += 1; + }; + float eval = minimax(newDepth, &next, alpha, beta); bestEval = std::max(bestEval, eval); alpha = std::max(alpha, bestEval); @@ -136,7 +141,11 @@ float minimax(int depth, Game *b, float alpha, float beta) { Game next = *b; PlayMove(move, &next); - float eval = minimax(depth - 1, &next, alpha, beta); + int newDepth = depth - 1; + if (IsPieceTypeAttacked(b, KING, b->turn)) { + newDepth += 1; + }; + float eval = minimax(newDepth, &next, alpha, beta); BestEval = std::min(BestEval, eval); @@ -150,17 +159,17 @@ float minimax(int depth, Game *b, float alpha, float beta) { } int PSTIndex(int square, bool white) { return white ? square : (56 ^ square); } -float EvaluateBoardForWhite(Game *b) { +float EvaluateBoardForWhite(Game *b, int depth) { float score = 0; bool isStaleMate = false; GetLegalMoves(b); if (b->state == WHITE_WON) { - return MATE; + return MATE - depth; } if (b->state == BLACK_WON) { - return -MATE; + return -MATE + depth; } if (b->state == STALEMATE || b->state == DRAW) { isStaleMate = true; diff --git a/src/bot.hpp b/src/bot.hpp index 74daefa..0b92292 100644 --- a/src/bot.hpp +++ b/src/bot.hpp @@ -3,7 +3,7 @@ #include "board.hpp" Move EngineGetBestMove(Game *b); -float EvaluateBoardForWhite(Game *b); +float EvaluateBoardForWhite(Game *b, int depth); float minimax(int depth, Game *b, float alpha, float beta); void LogMove(const Move &move); diff --git a/src/moves.cpp b/src/moves.cpp index 664971d..b8d86df 100644 --- a/src/moves.cpp +++ b/src/moves.cpp @@ -125,6 +125,9 @@ std::vector<Move> GetPseudoLegalMoves(Game *b) { if (piece.type == BISHOP) { GenerateSlidingMoves(b, i, bishop_Moves, moves); } + if (piece.type == KING) { + GenerateKingMoves(b, i, moves); + }; if (piece.type == ROOK) { GenerateSlidingMoves(b, i, rook_Moves, moves); } @@ -132,9 +135,6 @@ std::vector<Move> GetPseudoLegalMoves(Game *b) { GenerateSlidingMoves(b, i, rook_Moves, moves); GenerateSlidingMoves(b, i, bishop_Moves, moves); } - if (piece.type == KING) { - GenerateKingMoves(b, i, moves); - }; }; return moves; |
