From d89956a2fbaefe326f991e93e04fca119a2e808b Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 13 May 2024 13:07:13 +0200 Subject: [PATCH] implemented quiescence search --- .gitignore | 2 +- source/move.h | 8 +++- source/search.cpp | 96 +++++++++++++++++++++++++++++------------------ source/search.h | 4 +- 4 files changed, 68 insertions(+), 42 deletions(-) diff --git a/.gitignore b/.gitignore index fa103f5..f60144a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ obj/ # Ignore executable files *.exe -Chess-Test +Eleanor diff --git a/source/move.h b/source/move.h index 31e469d..bed6afb 100644 --- a/source/move.h +++ b/source/move.h @@ -48,8 +48,14 @@ public: return m_move >> 12; } + int IsCapture() { + return (GetFlags() == capture || GetFlags() == epCapture + || GetFlags() == knightPromoCapture || GetFlags() == bishopPromoCapture + || GetFlags() == rookPromoCapture || GetFlags() == queenPromoCapture); + } + void PrintMove() { std::cout << squareCoords[MoveFrom()] << squareCoords[MoveTo()]; std::cout << " (" << moveTypes[GetFlags()] << ")\n"; } -}; \ No newline at end of file +}; diff --git a/source/search.cpp b/source/search.cpp index e0c8383..d4569fc 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -2,28 +2,66 @@ #include "movegen.h" #include "evaluate.h" -static int ply = 0; +static inline int ply = 0; +static inline Move bestMove = Move(); +static inline int bestScore = -50000; + +int Quiescence(Board board, int alpha, int beta) { + int staticScore = Evaluate(board); + + if (staticScore >= beta) { + return beta; + } + + if (staticScore > alpha) { + alpha = staticScore; + } -int NegaMax(Board board, int depth, int alpha, int beta) { GenerateMoves(board, board.sideToMove); + + for (Move move : board.moveList) { + if (move.IsCapture()) { + Board copy = board; + board.MakeMove(move); + int score = -Quiescence(board, -beta, -alpha); + board = copy; + + if (score >= beta) { + return beta; + } + + if (score > alpha) { + alpha = score; + } + } + } + + return alpha; +} + +int NegaMax(Board board, int depth, int alpha, int beta, bool isRoot) { + GenerateMoves(board, board.sideToMove); + if (board.moveList.size() == 0) { - // Checkmate - if (board.InCheck(board.sideToMove)) { - return -4900000 + ply; - } else { // Stalemate + if (board.InCheck(board.sideToMove)) { // checkmate + return -49000 + ply; + } else { // stalemate return 0; } } - - if (depth == 0) return Evaluate(board); - int max = -5000000; + if (depth == 0) { + return Quiescence(board, -50000, 50000); + //return Evaluate(board); + } + + int max = -50000; for (Move move : board.moveList) { Board copy = board; board.MakeMove(move); ply++; - int score = -NegaMax(board, depth - 1, -beta, -alpha); + int score = -NegaMax(board, depth - 1, -beta, -alpha, false); board = copy; ply--; @@ -38,37 +76,21 @@ int NegaMax(Board board, int depth, int alpha, int beta) { if (score > max) { max = score; } - } + if (isRoot) { + if (max > bestScore) { + bestScore = max; + bestMove = move; + } + } + } return max; } -Move NegaMaxHandler(Board &board, int depth) { - Move bestMove = Move(); - - GenerateMoves(board, board.sideToMove); - - int max = -5000000; - - for (Move move : board.moveList) { - Board copy = board; - board.MakeMove(move); - ply++; - int score = -NegaMax(board, depth - 1, -5000000, 5000000); - board = copy; - ply--; - - if (score > max) { - max = score; - bestMove = move; - } - } - - return bestMove; -} - void SearchPosition(Board &board, int depth) { - Move bestMove = NegaMaxHandler(board, depth); + bestScore = -50000; + bestMove = Move(); + NegaMax(board, depth, -50000, 50000, true); std::cout << "bestmove " << squareCoords[bestMove.MoveFrom()] << squareCoords[bestMove.MoveTo()] << '\n'; -} \ No newline at end of file +} diff --git a/source/search.h b/source/search.h index cfdedcc..005e60b 100644 --- a/source/search.h +++ b/source/search.h @@ -1,6 +1,4 @@ #pragma once #include "board.h" -int NegaMax(Board board, int depth, int alpha, int beta); -Move NegaMaxHandler(Board &board, int depth); -void SearchPosition(Board &board, int depth); \ No newline at end of file +void SearchPosition(Board &board, int depth);