aboutsummaryrefslogtreecommitdiff
path: root/src/board/board.cpp
diff options
context:
space:
mode:
authorAdam <adammegarules1@gmail.com>2026-08-02 13:14:55 +0200
committerAdam <adammegarules1@gmail.com>2026-08-02 13:14:55 +0200
commit9d2a95f566c5cb31d92af969d052b665ccbea9be (patch)
treee6ceb7c1ba9c938f5aced88a667fa96d2533db31 /src/board/board.cpp
parentf948514c5337961444f555374523902ae94dd00f (diff)
caching kign position
Diffstat (limited to 'src/board/board.cpp')
-rw-r--r--src/board/board.cpp44
1 files changed, 31 insertions, 13 deletions
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 <cassert>
+#include <cstdint>
#include <cstdlib>
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);
}
}