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 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) (limited to 'src/board/board.cpp') 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); } } -- cgit v1.2.3