From bc20789325250b1ca6b5b06a8021a99d91a5dc20 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 28 Jul 2026 10:28:36 +0200 Subject: fixing memory bug --- .gitignore | 1 + moves.txt | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/bot.cpp | 30 ++++++++++++++++++++----- src/bot.hpp | 1 + src/moves.cpp | 15 ++++++++----- src/uci.cpp | 2 +- 6 files changed, 107 insertions(+), 13 deletions(-) create mode 100644 moves.txt diff --git a/.gitignore b/.gitignore index dec6bd0..461b652 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ build/ tmp/ tmp .cache/ +uci_log.txt diff --git a/moves.txt b/moves.txt new file mode 100644 index 0000000..998bbcd --- /dev/null +++ b/moves.txt @@ -0,0 +1,71 @@ +a2b3 +b2a3 +b2c3 +c2b3 +c2d3 +d2c3 +e2e3 +e2e4 +e2d3 +f2f3 +f2g3 +g2h3 +h2g3 +b1c3 +b1a3 +g1h3 +g1f3 +a2a3 +a2a4 +a2b3 +b2c3 +c2b3 +c2d3 +d2c3 +d2e3 +e2d3 +f2f3 +f2e3 +g2g3 +h2h3 +b1c3 +b1a3 +g1h3 +g1f3 +a2a3 +a2a4 +a2b3 +b2c3 +c2b3 +c2d3 +d2c3 +d2e3 +e2d3 +f2f3 +f2e3 +g2g3 +h2h3 +b1c3 +b1a3 +g1h3 +g1f3 +a2a3 +a2a4 +b2b3 +b2b4 +c2c3 +c2c4 +d2d3 +d2d4 +e2e3 +e2e4 +f2f3 +f2f4 +g2g3 +g2g4 +h2h3 +h2h4 +b1c3 +b1a3 +g1h3 +g1f3 diff --git a/src/bot.cpp b/src/bot.cpp index 3054b0d..e976d11 100644 --- a/src/bot.cpp +++ b/src/bot.cpp @@ -10,7 +10,7 @@ int KNIGHT_VALUE = 200; int BISHOP_VALUE = 300; int ROOK_VALUE = 400; int QUEEN_VALUE = 900; -int CHECK = 300; +int CHECK = 20; int MATE = 10000; int KNIGHT_TABLE[64] = {-50, -40, -30, -30, -30, -30, -40, -50, -40, -20, 0, @@ -22,6 +22,21 @@ int KNIGHT_TABLE[64] = {-50, -40, -30, -30, -30, -30, -40, -50, -40, -20, 0, const int SEARCH_DEPTH = 3; +#include + +void LogMove(const Move &move) { + std::ofstream file("moves.txt", std::ios::app); + + if (!file.is_open()) + return; + + file << static_cast('a' + move.From.file) + << static_cast('8' - move.From.rank) + << static_cast('a' + move.To.file) + << static_cast('8' - move.To.rank); + + file << '\n'; +} Move EngineGetBestMove(Game *b) { auto moves = GetLegalMoves(b); if (moves.size() == 0) { @@ -30,6 +45,7 @@ Move EngineGetBestMove(Game *b) { Move bestMove = moves[0]; float BestEval = (b->turn ? -INFINITY : INFINITY); for (Move move : moves) { + LogMove(move); Game TestBoard = *b; PlayMove(move, &TestBoard); float alpha = -INFINITY; @@ -95,6 +111,7 @@ float minimax(int depth, Game *b, float alpha, float beta) { return BestEval; } } +int PSTIndex(int square, bool white) { return white ? square : (56 ^ square); } float EvaluateBoardForWhite(Game *b) { float score = 0; @@ -125,7 +142,7 @@ float EvaluateBoardForWhite(Game *b) { break; case KNIGHT: value = KNIGHT_VALUE; - value += KNIGHT_TABLE[i]; + value += KNIGHT_TABLE[PSTIndex(i, piece.color)]; break; case BISHOP: value = BISHOP_VALUE; @@ -145,10 +162,11 @@ float EvaluateBoardForWhite(Game *b) { else score -= value; } - bool isCheck = IsPieceTypeAttacked(b, KING, 1); - if (isCheck) { - score += CHECK; - } + if (IsPieceTypeAttacked(b, KING, true)) + score -= CHECK; // white king attacked + + if (IsPieceTypeAttacked(b, KING, false)) + score += CHECK; // black king attacked if (isStaleMate) { return 0; } diff --git a/src/bot.hpp b/src/bot.hpp index 242fdfe..74daefa 100644 --- a/src/bot.hpp +++ b/src/bot.hpp @@ -5,5 +5,6 @@ Move EngineGetBestMove(Game *b); float EvaluateBoardForWhite(Game *b); float minimax(int depth, Game *b, float alpha, float beta); +void LogMove(const Move &move); #endif /* SRC_BOT_H_ */ diff --git a/src/moves.cpp b/src/moves.cpp index d7df595..00cd437 100644 --- a/src/moves.cpp +++ b/src/moves.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -61,20 +62,22 @@ void GeneratePawnMoves(Game *b, int from, std::vector &moves) { moves.push_back({position, IndexToPosition(twoSteps)}); } } - - // check for (int fileOffset : {-1, 1}) { int targetFile = position.file + fileOffset; - if (targetFile < 0 || targetFile >= 8) { + int targetRank = position.rank + (pawn.color ? -1 : 1); + + if (targetFile < 0 || targetFile >= 8) continue; - } - int target = (position.rank + (pawn.color ? -1 : 1)) * 8 + targetFile; + if (targetRank < 0 || targetRank >= 8) + continue; + + int target = targetRank * 8 + targetFile; if (IndexToPosition(target) == b->enPassant && b->canEnpassant) { - // enpasstant!!!!!!! moves.push_back({position, IndexToPosition(target)}); } + if (b->pieces[target].type != NONE && b->pieces[target].color != pawn.color) { moves.push_back({position, IndexToPosition(target)}); diff --git a/src/uci.cpp b/src/uci.cpp index 4825ece..a2c3672 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -59,7 +59,7 @@ Move UciToMove(string uci) { return {from, to}; } static Game initBoard(string startingFEN) { - Game b; + Game b{}; b.enPassant = IndexToPosition(0); // default value b.canEnpassant = false; b.state = TURN; -- cgit v1.2.3