diff options
| author | Adam <adammegarules1@gmail.com> | 2026-07-29 12:04:28 +0200 |
|---|---|---|
| committer | Adam <adammegarules1@gmail.com> | 2026-07-29 12:04:28 +0200 |
| commit | b05b0862b9ad4f4c2ebc15b3ce23b2bcf0b21378 (patch) | |
| tree | e538a887ad2de54527632887f3aa51ec4b7a7864 | |
| parent | 828b5a414a58d1b09c201fd33cb465e768589610 (diff) | |
improving the bot
| -rw-r--r-- | src/bot.cpp | 57 | ||||
| -rw-r--r-- | src/bot.hpp | 2 |
2 files changed, 44 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; } diff --git a/src/bot.hpp b/src/bot.hpp index b95822f..26ae9f2 100644 --- a/src/bot.hpp +++ b/src/bot.hpp @@ -2,8 +2,10 @@ #define SRC_BOT_H_ #include "board.hpp" +#include <vector> Move EngineGetBestMove(Game *b); float EvaluateBoardForWhite(Game *b, int depth); float minimax(int depth, Game *b, float alpha, float beta); int ScoreMove(const Game *board, const Move &move); +std::vector<Move> GetSortedLegalMoves(Game *g); #endif /* SRC_BOT_H_ */ |
