aboutsummaryrefslogtreecommitdiff
path: root/src/bot.cpp
diff options
context:
space:
mode:
authorAdam <adammegarules1@gmail.com>2026-08-01 08:58:05 +0200
committerAdam <adammegarules1@gmail.com>2026-08-01 08:58:05 +0200
commit61bbd36293516e122297dc9d157b3b8ee886fab6 (patch)
tree6a58a6c686ed1a990e765d2d1745c13dd0c92b55 /src/bot.cpp
parent839b4740af77743c7141e113eb7f110b4f6a9983 (diff)
cleaning code
Diffstat (limited to 'src/bot.cpp')
-rw-r--r--src/bot.cpp147
1 files changed, 45 insertions, 102 deletions
diff --git a/src/bot.cpp b/src/bot.cpp
index c269665..b5d2b57 100644
--- a/src/bot.cpp
+++ b/src/bot.cpp
@@ -11,11 +11,10 @@
#include <ctime>
#include <iostream>
#include <iterator>
-#include <limits>
#include <vector>
constexpr int MAXIMUM_DEPTH = 10;
-constexpr int MAXIMUM_TIME_PER_MOVE = 6;
+constexpr int MAXIMUM_TIME_PER_MOVE = 7;
constexpr int Q_DEPTH_LIMIT = 4;
constexpr int MATE = 10000;
@@ -79,15 +78,16 @@ static std::vector<Move> GetSortedLegalMoves(Game *g, bool generateQuietMoves,
// faster than a real mate-in-1). White perspective throughout.
static int quiescenceSearch(Game *b, int qdepth, int alpha, int beta, int ply) {
// Runs GetNewGameState internally, so b->state is up to date afterwards.
- const int standPat = EvaluateBoardForWhite(b);
+ const int standPat = EvaluateBoard(b);
switch (b->state) {
case WHITE_WON:
- return MATE - ply; // black is mated
case BLACK_WON:
- return -(MATE - ply); // white is mated
+ return -(MATE - ply);
+ break;
case DRAW:
return 0;
+ break;
case TURN:
break;
}
@@ -96,48 +96,28 @@ static int quiescenceSearch(Game *b, int qdepth, int alpha, int beta, int ply) {
return standPat;
}
- auto moves = GetSortedLegalMoves(b, false, nullptr);
+ if (standPat >= beta) {
+ return beta;
+ }
+ alpha = std::max(alpha, standPat);
- if (b->turn) {
- if (standPat >= beta) {
- return beta;
- }
- alpha = std::max(alpha, standPat);
+ auto moves = GetSortedLegalMoves(b, false, nullptr);
- for (Move move : moves) {
- UndoMove undo = MakeMove(move, b);
+ for (Move move : moves) {
+ UndoMove undo = MakeMove(move, b);
- int score = quiescenceSearch(b, qdepth + 1, alpha, beta, ply + 1);
+ int score = -quiescenceSearch(b, qdepth + 1, -beta, -alpha, ply + 1);
- UnMakeMove(undo, b);
+ UnMakeMove(undo, b);
- if (score >= beta) {
- return beta;
- }
- alpha = std::max(alpha, score);
- }
- return alpha;
- } else {
- if (standPat <= alpha) {
- return alpha;
+ if (score >= beta) {
+ return beta;
}
- beta = std::min(beta, standPat);
-
- for (Move move : moves) {
- UndoMove undo = MakeMove(move, b);
-
- int score = quiescenceSearch(b, qdepth + 1, alpha, beta, ply + 1);
-
- UnMakeMove(undo, b);
+ alpha = std::max(alpha, score);
+ };
+ return alpha;
+};
- if (score <= alpha) {
- return alpha;
- }
- beta = std::min(beta, score);
- }
- return beta;
- }
-}
static int search(int depth, Game *b, int alpha, int beta, int ply) {
const uint64_t gameHash = GenerateZobristKey(b);
@@ -176,72 +156,43 @@ static int search(int depth, Game *b, int alpha, int beta, int ply) {
if (moves.empty()) {
const Position king = FindKing(b, b->turn);
if (IsSquareAttacked(b, king, !b->turn)) {
- return b->turn ? -(MATE - ply) : (MATE - ply); // mated
+ return -(MATE - ply); // mated
}
return 0; // stalemate
}
Move bestMove = moves[0];
const int alphaOrig = alpha;
+ int bestScore = -INF;
- if (b->turn) { // white maximizes
- int bestScore = std::numeric_limits<int>::lowest();
-
- for (Move move : moves) {
- UndoMove undo = MakeMove(move, b);
+ for (Move move : moves) {
+ UndoMove undo = MakeMove(move, b);
- int score = search(depth - 1, b, alpha, beta, ply + 1);
+ int score = -search(depth - 1, b, -beta, -alpha, ply + 1);
- UnMakeMove(undo, b);
+ UnMakeMove(undo, b);
- if (score > bestScore) {
- bestScore = score;
- bestMove = move;
- }
- alpha = std::max(alpha, score);
- if (alpha >= beta) {
- break;
- }
+ if (score > bestScore) {
+ bestScore = score;
+ bestMove = move;
}
-
- const Flag flag = bestScore <= alphaOrig ? UPPERBOUND
- : bestScore >= beta ? LOWERBOUND
- : EXACT;
- (*b->Transpositions)[gameHash] = {
- .depth = depth, .Eval = bestScore, .flag = flag, .bestMove = bestMove};
- return bestScore;
- } else { // black minimizes
- int bestScore = std::numeric_limits<int>::max();
-
- for (Move move : moves) {
- UndoMove undo = MakeMove(move, b);
-
- int score = search(depth - 1, b, alpha, beta, ply + 1);
-
- UnMakeMove(undo, b);
-
- if (score < bestScore) {
- bestScore = score;
- bestMove = move;
- }
- beta = std::min(beta, score);
- if (alpha >= beta) {
- break;
- }
+ alpha = std::max(alpha, score);
+ if (alpha >= beta) {
+ break;
}
-
- const Flag flag = bestScore <= alphaOrig ? UPPERBOUND
- : bestScore >= beta ? LOWERBOUND
- : EXACT;
- (*b->Transpositions)[gameHash] = {
- .depth = depth, .Eval = bestScore, .flag = flag, .bestMove = bestMove};
- return bestScore;
}
+
+ const Flag flag = bestScore <= alphaOrig ? UPPERBOUND
+ : bestScore >= beta ? LOWERBOUND
+ : EXACT;
+ (*b->Transpositions)[gameHash] = {
+ .depth = depth, .Eval = bestScore, .flag = flag, .bestMove = bestMove};
+ return bestScore;
}
struct SearchResult {
Move bestMove;
- int score; // white perspective
+ int score;
};
// Searches every root move to `depth` plies and returns the best one.
@@ -255,26 +206,18 @@ static SearchResult SearchDepth(Game *b, int depth, const Move *previousBest) {
}
Move bestMove = moves[0];
- int bestEval = b->turn ? std::numeric_limits<int>::lowest()
- : std::numeric_limits<int>::max();
+ int bestEval = -INF;
for (Move move : moves) {
UndoMove undo = MakeMove(move, b);
- int eval = search(depth - 1, b, -INF, INF, 1);
+ int eval = -search(depth - 1, b, -INF, INF, 1);
UnMakeMove(undo, b);
- if (b->turn) {
- if (eval > bestEval) {
- bestEval = eval;
- bestMove = move;
- }
- } else {
- if (eval < bestEval) {
- bestEval = eval;
- bestMove = move;
- }
+ if (eval > bestEval) {
+ bestEval = eval;
+ bestMove = move;
}
}