implemented quiescence search

This commit is contained in:
rektdie committed 2024-05-13 13:07:13 +02:00
1 parent b78c9cc196
commit d89956a2fb
4 files changed
+62 -36

No files matched your search

+1 -1
View File
@@ -3,4 +3,4 @@ obj/
# Ignore executable files # Ignore executable files
*.exe *.exe
Chess-Test Eleanor
+6
View File
@@ -48,6 +48,12 @@ public:
return m_move >> 12; return m_move >> 12;
} }
int IsCapture() {
return (GetFlags() == capture || GetFlags() == epCapture
|| GetFlags() == knightPromoCapture || GetFlags() == bishopPromoCapture
|| GetFlags() == rookPromoCapture || GetFlags() == queenPromoCapture);
}
void PrintMove() { void PrintMove() {
std::cout << squareCoords[MoveFrom()] << squareCoords[MoveTo()]; std::cout << squareCoords[MoveFrom()] << squareCoords[MoveTo()];
std::cout << " (" << moveTypes[GetFlags()] << ")\n"; std::cout << " (" << moveTypes[GetFlags()] << ")\n";
+55 -33
View File
@@ -2,28 +2,66 @@
#include "movegen.h" #include "movegen.h"
#include "evaluate.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); 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) { if (board.moveList.size() == 0) {
// Checkmate if (board.InCheck(board.sideToMove)) { // checkmate
if (board.InCheck(board.sideToMove)) { return -49000 + ply;
return -4900000 + ply; } else { // stalemate
} else { // Stalemate
return 0; return 0;
} }
} }
if (depth == 0) return Evaluate(board); if (depth == 0) {
return Quiescence(board, -50000, 50000);
//return Evaluate(board);
}
int max = -5000000; int max = -50000;
for (Move move : board.moveList) { for (Move move : board.moveList) {
Board copy = board; Board copy = board;
board.MakeMove(move); board.MakeMove(move);
ply++; ply++;
int score = -NegaMax(board, depth - 1, -beta, -alpha); int score = -NegaMax(board, depth - 1, -beta, -alpha, false);
board = copy; board = copy;
ply--; ply--;
@@ -38,37 +76,21 @@ int NegaMax(Board board, int depth, int alpha, int beta) {
if (score > max) { if (score > max) {
max = score; max = score;
} }
}
return max; if (isRoot) {
} if (max > bestScore) {
bestScore = 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; bestMove = move;
} }
} }
}
return bestMove; return max;
} }
void SearchPosition(Board &board, int depth) { 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()] std::cout << "bestmove " << squareCoords[bestMove.MoveFrom()]
<< squareCoords[bestMove.MoveTo()] << '\n'; << squareCoords[bestMove.MoveTo()] << '\n';
} }
-2
View File
@@ -1,6 +1,4 @@
#pragma once #pragma once
#include "board.h" #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); void SearchPosition(Board &board, int depth);