From 9d2a95f566c5cb31d92af969d052b665ccbea9be Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 2 Aug 2026 13:14:55 +0200 Subject: caching kign position --- src/board/board.cpp | 44 +++++++++++++++++++++++++++++++------------- src/board/board.hpp | 6 ++++-- src/board/fen.cpp | 2 +- src/bot.cpp | 2 +- 4 files changed, 37 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/board/board.cpp b/src/board/board.cpp index 2cd705d..572a3f0 100644 --- a/src/board/board.cpp +++ b/src/board/board.cpp @@ -2,21 +2,32 @@ #include "moves.hpp" #include "zobrist.hpp" #include +#include #include int PositionToIndex(Position i) { return i.rank * 8 + i.file; } Position FindKing(Game *b, bool color) { - for (int i = 0; i < 64; i++) { - Piece piece = b->pieces[i]; - - if (piece.type == KING && piece.color == color) { - return IndexToPosition(i); - } + if (b->WhiteKingPosition >= 64) { + assert(false && "weird position"); + exit(1); } - - assert(false && "King not found"); - return {0, 0}; + if (b->BlackKingPosition >= 64) { + assert(false && "weird position"); + exit(1); + } + if (b->pieces[b->WhiteKingPosition].color != true || + b->pieces[b->WhiteKingPosition].type != KING) { + assert(false && "Expected king at king position"); + exit(1); + } + if (b->pieces[b->BlackKingPosition].color != false || + b->pieces[b->BlackKingPosition].type != KING) { + assert(false && "Expected king at king position"); + exit(1); + } + return color ? IndexToPosition(b->WhiteKingPosition) + : IndexToPosition(b->BlackKingPosition); } UndoMove MakeMove(Move move, Game *g) { UndoMove undo = {}; @@ -188,7 +199,7 @@ UndoMove MakeMove(Move move, Game *g) { if (g->halfMoveClock >= 50) { g->state = DRAW; } - UpdateBitboards(g); + UpdateHelpers(g); return undo; }; void UnMakeMove(UndoMove undo, Game *g) { @@ -244,15 +255,22 @@ void UnMakeMove(UndoMove undo, Game *g) { if (g->ThreeFoldMap[undo.ZobristKey] <= 0) { g->ThreeFoldMap.erase(undo.ZobristKey); }; - UpdateBitboards(g); + UpdateHelpers(g); }; -void UpdateBitboards(Game *g) { +void UpdateHelpers(Game *g) { g->PieceBitboard = 0; - for (int i = 0; i < 64; i++) { + for (uint8_t i = 0; i < 64; i++) { Piece piece = g->pieces[i]; if (piece.type == NONEPIECE) { continue; } + if (piece.type == KING) { + if (piece.color) { + g->WhiteKingPosition = i; + } else { + g->BlackKingPosition = i; + } + } g->PieceBitboard |= (1ULL << i); } } diff --git a/src/board/board.hpp b/src/board/board.hpp index bcd0b95..869e223 100644 --- a/src/board/board.hpp +++ b/src/board/board.hpp @@ -53,7 +53,9 @@ struct TranspositionsEntry { struct Game { Piece pieces[64]; uint64_t PieceBitboard = 0; // used for quicly iterating over all squares - bool turn = true; // 1 white; 0 black + uint8_t WhiteKingPosition = 0; + uint8_t BlackKingPosition = 0; + bool turn = true; // 1 white; 0 black bool whiteCastleKing = false; bool whiteCastleQueen = false; bool blackCastleKing = false; @@ -102,6 +104,6 @@ UndoMove MakeMove(Move move, Game *g); void UnMakeMove(UndoMove undo, Game *g); Position FindKing(Game *g, bool white); -void UpdateBitboards(Game *g); +void UpdateHelpers(Game *g); void SetPiece(int i, PieceType type, bool color, Game *g); #endif /* SRC_BOARD_H_ */ diff --git a/src/board/fen.cpp b/src/board/fen.cpp index a7e65eb..fc52bfe 100644 --- a/src/board/fen.cpp +++ b/src/board/fen.cpp @@ -154,5 +154,5 @@ void setBoardFen(const std::string fen, Game *g) { break; } g->enPassant = enpassant; - UpdateBitboards(g); + UpdateHelpers(g); } diff --git a/src/bot.cpp b/src/bot.cpp index cffa202..3567ef3 100644 --- a/src/bot.cpp +++ b/src/bot.cpp @@ -254,7 +254,7 @@ Move GetBestMove(Game *b, const int maxDepth) { Nodes = 0; int actualDepth = maxDepth > 0 ? maxDepth : MAXIMUM_DEPTH; - bool usingDefaultDepth = maxDepth > 0; + bool usingDefaultDepth = maxDepth < 0; auto legalMoves = GetSortedLegalMoves(b, true, nullptr); if (legalMoves.empty()) { assert(false && "GetBestMove called with no legal moves"); -- cgit v1.2.3