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.cpp73
1 files changed, 38 insertions, 35 deletions
diff --git a/src/board/board.cpp b/src/board/board.cpp
index 4ca0667..7525fab 100644
--- a/src/board/board.cpp
+++ b/src/board/board.cpp
@@ -1,4 +1,5 @@
#include <cassert>
+#include <chrono>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
@@ -114,8 +115,8 @@ bool isRepetionDraw(uint64_t key, Game *g) {
return repetions >= 3;
};
-static void movePiece(uint8_t fromSquare, Piece fromPiece, uint8_t toSquare,
- Piece toPiece, Game *g) {
+static void movePiece(const uint8_t &fromSquare, const Piece &fromPiece,
+ const uint8_t &toSquare, const Piece &toPiece, Game *g) {
uint64_t from = 1ULL << fromSquare;
uint64_t to = 1ULL << toSquare;
@@ -238,6 +239,8 @@ 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 (piece.type == PAWN &&
IndexToPosition(toSquare).rank == (piece.color ? 7 : 0)) {
piece.type = promotion;
@@ -373,13 +376,34 @@ Undo MakeMove(uint16_t move, Game *g) {
return undo;
};
void UndoMove(Undo undo, Game *g) {
- g->pieces[undo.from] = undo.movedPiece;
+ movePiece(undo.to, undo.movedPiece, undo.from, {.type = NONEPIECE}, g);
+
+ // uint64_t from = 1ULL << undo.from;
+ uint64_t to = 1ULL << undo.to;
g->pieces[undo.to] = undo.capturedPiece;
+ if (undo.capturedPiece.type != NONEPIECE) {
+ g->PieceBitboards[undo.capturedPiece.color][undo.capturedPiece.type] ^= to;
+ if (undo.capturedPiece.color) {
+ g->WhitePieceBitboard ^= to;
+ } else {
+ g->BlackPieceBitboard ^= to;
+ }
+ };
if (undo.wasEnPassantCapture) {
+ const uint64_t capturedSquare =
+ 1ULL << PositionToIndex(undo.enPassantCapturedSquare);
g->pieces[PositionToIndex(undo.enPassantCapturedSquare)] =
undo.enPassantCapturedPiece;
+ g->PieceBitboards[undo.enPassantCapturedPiece.color]
+ [undo.enPassantCapturedPiece.type] |= capturedSquare;
+ if (undo.enPassantCapturedPiece.color) {
+ g->WhitePieceBitboard |= capturedSquare;
+
+ } else {
+ g->BlackPieceBitboard |= capturedSquare;
+ }
}
g->turn = undo.oldTurn;
@@ -406,11 +430,11 @@ void UndoMove(Undo undo, Game *g) {
Position rookTo = IndexToPosition(undo.from);
rookTo.file = 7;
- g->pieces[PositionToIndex(rookTo)] = g->pieces[PositionToIndex(rookFrom)];
- g->pieces[PositionToIndex(rookFrom)] = {
- .color = false,
- .type = NONEPIECE,
- };
+ movePiece(static_cast<uint8_t>(PositionToIndex(rookFrom)),
+ g->pieces[PositionToIndex(rookFrom)],
+ static_cast<uint8_t>(PositionToIndex(rookTo)),
+ {.type = NONEPIECE}, g);
+
} else {
// d -> a
Position rookFrom = IndexToPosition(undo.from);
@@ -419,35 +443,14 @@ void UndoMove(Undo undo, Game *g) {
Position rookTo = IndexToPosition(undo.from);
rookTo.file = 0;
- g->pieces[PositionToIndex(rookTo)] = g->pieces[PositionToIndex(rookFrom)];
- g->pieces[PositionToIndex(rookFrom)] = {
- .color = false,
- .type = NONEPIECE,
- };
- }
+ movePiece(static_cast<uint8_t>(PositionToIndex(rookFrom)),
+ g->pieces[PositionToIndex(rookFrom)],
+ static_cast<uint8_t>(PositionToIndex(rookTo)),
+ {.type = NONEPIECE}, g);
+ };
}
g->history.pop_back();
- UpdateHelpers(g);
-};
-void UpdateHelpers(Game *g) {
- g->PieceBitboard = 0;
- g->WhitePieceBitboard = 0;
- g->BlackPieceBitboard = 0;
- std::memset(g->PieceBitboards, 0, sizeof(g->PieceBitboards));
- for (uint8_t i = 0; i < 64; i++) {
- Piece piece = g->pieces[i];
- if (piece.type == NONEPIECE) {
- continue;
- }
- g->PieceBitboards[piece.color][piece.type] |= (1ULL << i);
-
- if (piece.color) {
- g->WhitePieceBitboard |= (1ULL << i);
- } else {
- g->BlackPieceBitboard |= (1ULL << i);
- }
- }
g->PieceBitboard = g->WhitePieceBitboard | g->BlackPieceBitboard;
-}
+}; \ No newline at end of file