aboutsummaryrefslogtreecommitdiff
path: root/src/board/board.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/board/board.cpp')
-rw-r--r--src/board/board.cpp41
1 files changed, 29 insertions, 12 deletions
diff --git a/src/board/board.cpp b/src/board/board.cpp
index 81a3c4e..faabfac 100644
--- a/src/board/board.cpp
+++ b/src/board/board.cpp
@@ -142,7 +142,10 @@ static void movePiece(const uint8_t &fromSquare, const Piece &fromPiece,
}
g->PieceBitboard = g->WhitePieceBitboard | g->BlackPieceBitboard;
+ xorSquare(fromSquare, g);
+ xorSquare(toSquare, g);
g->pieces[toSquare] = fromPiece;
+ xorSquare(toSquare, g);
g->pieces[fromSquare] = {.color = false, .type = NONEPIECE};
};
@@ -173,6 +176,8 @@ Undo MakeMove(uint16_t move, Game *g) {
undo.OldBlackCastleKing = g->blackCastleKing;
undo.OldBlackCastleQueen = g->blackCastleQueen;
+ undo.ZobristKey = g->hash;
+
// play move
Piece movingPiece = g->pieces[fromSquare];
Piece captuaredPiece = g->pieces[toSquare];
@@ -196,6 +201,9 @@ Undo MakeMove(uint16_t move, Game *g) {
}
// Playing enpasstant
+
+ xorEnpassantKey(g); // remove existing key
+
if (movingPiece.type == PAWN && g->canEnpassant &&
IndexToPosition(toSquare) == g->enPassant) {
Position capturedPawn = IndexToPosition(toSquare);
@@ -217,6 +225,7 @@ Undo MakeMove(uint16_t move, Game *g) {
}
g->PieceBitboard = g->WhitePieceBitboard | g->BlackPieceBitboard;
+ xorSquare(static_cast<uint8_t>(PositionToIndex(capturedPawn)), g);
g->pieces[PositionToIndex(capturedPawn)] = {.color = false,
.type = NONEPIECE};
}
@@ -236,10 +245,6 @@ Undo MakeMove(uint16_t move, Game *g) {
};
}
- // promotion is always set and should only be aplied when reached final rank
- // (default: knight)
- // because we use only two bits of memory for promotion we cant represent a
- // none type so we always have knight set and only it last rank and its pawn
if (movingPiece.type == PAWN &&
IndexToPosition(toSquare).rank == (movingPiece.color ? 7 : 0)) {
movingPiece.type = promotion;
@@ -269,7 +274,7 @@ Undo MakeMove(uint16_t move, Game *g) {
rookFrom.file = 0;
rookTo.file = 3;
- }
+ };
uint64_t from = 1ULL << PositionToIndex(rookFrom);
uint64_t to = 1ULL << PositionToIndex(rookTo);
@@ -283,16 +288,22 @@ Undo MakeMove(uint16_t move, Game *g) {
}
g->PieceBitboard = g->WhitePieceBitboard | g->BlackPieceBitboard;
+ xorSquare(static_cast<uint8_t>(PositionToIndex(rookFrom)), g);
g->pieces[PositionToIndex(rookTo)] = g->pieces[PositionToIndex(rookFrom)];
+ xorSquare(static_cast<uint8_t>(PositionToIndex(rookTo)), g);
g->pieces[PositionToIndex(rookFrom)] = {.type = NONEPIECE};
}
// remove castling right if king has moved
if (movingPiece.type == KING) {
if (movingPiece.color) {
+ xorCastleKey(true, true, g);
+ xorCastleKey(true, false, g);
g->whiteCastleKing = false;
g->whiteCastleQueen = false;
} else {
+ xorCastleKey(false, true, g);
+ xorCastleKey(false, false, g);
g->blackCastleKing = false;
g->blackCastleQueen = false;
}
@@ -302,38 +313,45 @@ Undo MakeMove(uint16_t move, Game *g) {
if (movingPiece.type == ROOK) {
if (movingPiece.color) { // White
if (fromSquare == 0) { // a1
+ xorCastleKey(true, false, g);
g->whiteCastleQueen = false;
}
if (fromSquare == 7) { // h1
+ xorCastleKey(true, true, g);
g->whiteCastleKing = false;
}
} else { // Black
if (fromSquare == 7 * 8) { // a8
+ xorCastleKey(false, false, g);
g->blackCastleQueen = false;
}
if (fromSquare == 7 * 9) { // h8
+ xorCastleKey(false, true, g);
g->blackCastleKing = false;
}
}
}
- // this part is written with ai
if (captuaredPiece.type == ROOK) {
if (captuaredPiece.color) { // White rook captured
if (toSquare == 0) { // a1
+ xorCastleKey(true, false, g);
g->whiteCastleQueen = false;
}
if (toSquare == 7) { // h1
+ xorCastleKey(true, true, g);
g->whiteCastleKing = false;
}
} else { // Black rook captured
if (toSquare == 7 * 8) { // a8
+ xorCastleKey(false, false, g);
g->blackCastleQueen = false;
}
if (toSquare == 7 * 9) {
// h8
+ xorCastleKey(false, true, g);
g->blackCastleKing = false;
}
}
@@ -344,13 +362,10 @@ Undo MakeMove(uint16_t move, Game *g) {
// changing who turn it is
g->turn = !g->turn;
+ xorSidekey(g); // toggle the side key
+ xorEnpassantKey(g); // add new en passant key
- // 3 fold check
- uint64_t key = GenerateZobristKey(g);
-
- undo.ZobristKey = key;
-
- g->history.push_back(key);
+ g->history.push_back(g->hash);
return undo;
};
@@ -432,4 +447,6 @@ void UndoMove(Undo undo, Game *g) {
g->history.pop_back();
g->PieceBitboard = g->WhitePieceBitboard | g->BlackPieceBitboard;
+
+ g->hash = undo.ZobristKey;
};