aboutsummaryrefslogtreecommitdiff
path: root/src/bot.cpp
diff options
context:
space:
mode:
authorAdam <adammegarules1@gmail.com>2026-07-29 12:04:28 +0200
committerAdam <adammegarules1@gmail.com>2026-07-29 12:04:28 +0200
commitb05b0862b9ad4f4c2ebc15b3ce23b2bcf0b21378 (patch)
treee538a887ad2de54527632887f3aa51ec4b7a7864 /src/bot.cpp
parent828b5a414a58d1b09c201fd33cb465e768589610 (diff)
improving the bot
Diffstat (limited to 'src/bot.cpp')
-rw-r--r--src/bot.cpp57
1 files changed, 42 insertions, 15 deletions
diff --git a/src/bot.cpp b/src/bot.cpp
index 5a4601e..ff7a771 100644
--- a/src/bot.cpp
+++ b/src/bot.cpp
@@ -10,7 +10,7 @@
#include <iostream>
#include <vector>
-const int SEARCH_DEPTH = 5;
+const int SEARCH_DEPTH = 4;
const int PAWN_VALUE = 100;
const int KNIGHT_VALUE = 320;
@@ -63,6 +63,29 @@ int ROOK_TABLE[64] = {
5, 10, 10, 10, 10, 10, 10, 5, //
0, 0, 5, 10, 10, 5, 0, 0, //
};
+
+int QUEEN_TABLE[64] = {
+ -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, //
+};
+
+int KING_TABLE_EARLY[64] = {
+ -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, //
+};
+
int ScoreMove(const Game *board, const Move &move) {
int score = 0;
@@ -94,18 +117,22 @@ int ScoreMove(const Game *board, const Move &move) {
return score;
}
+std::vector<Move> GetSortedLegalMoves(Game *g) {
+ auto moves = GetLegalMoves(g);
+ std::sort(moves.begin(), moves.end(), [&](const Move &a, const Move &c) {
+ return ScoreMove(g, a) > ScoreMove(g, c);
+ });
+ return moves;
+}
+
Move EngineGetBestMove(Game *b) {
- auto moves = GetLegalMoves(b);
+ auto moves = GetSortedLegalMoves(b);
if (moves.size() == 0) {
std::cout << "Expected a position with legal moves";
assert(false && "Unhanled error zero legal moves for bot");
exit(1);
}
- // sort legal moves
- std::sort(moves.begin(), moves.end(), [&](const Move &a, const Move &c) {
- return ScoreMove(b, a) > ScoreMove(b, c);
- });
Move bestMove = moves[0];
float BestEval = (b->turn ? -INFINITY : INFINITY);
for (Move move : moves) {
@@ -140,7 +167,7 @@ float minimax(int depth, Game *b, float alpha, float beta) {
}
}
- auto moves = GetLegalMoves(b);
+ auto moves = GetSortedLegalMoves(b);
if (depth == 0 || moves.size() == 0) {
return EvaluateBoardForWhite(b, depth);
}
@@ -185,8 +212,6 @@ int PSTIndex(int square, bool white) { return white ? square : (56 ^ square); }
float EvaluateBoardForWhite(Game *b, int depth) {
float score = 0;
- bool isStaleMate = false;
-
if (b->state == WHITE_WON) {
return MATE + depth;
}
@@ -194,7 +219,7 @@ float EvaluateBoardForWhite(Game *b, int depth) {
return -MATE - depth;
}
if (b->state == STALEMATE || b->state == DRAW) {
- isStaleMate = true;
+ return 0;
}
for (int i = 0; i < 64; i++) {
@@ -223,6 +248,10 @@ float EvaluateBoardForWhite(Game *b, int depth) {
break;
case QUEEN:
value = QUEEN_VALUE;
+ value += QUEEN_TABLE[PSTIndex(i, piece.color)];
+ break;
+ case KING:
+ value = KING_TABLE_EARLY[PSTIndex(i, piece.color)];
break;
default:
break;
@@ -233,13 +262,11 @@ float EvaluateBoardForWhite(Game *b, int depth) {
else
score -= value;
}
- if (IsPieceTypeAttacked(b, KING, true))
+ if (IsPieceTypeAttacked(b, KING, true)) {
score -= CHECK; // white king attacked
-
- if (IsPieceTypeAttacked(b, KING, false))
+ }
+ if (IsPieceTypeAttacked(b, KING, false)) {
score += CHECK; // black king attacked
- if (isStaleMate) {
- return 0;
}
return score;
}