aboutsummaryrefslogtreecommitdiff
path: root/src/board
diff options
context:
space:
mode:
Diffstat (limited to 'src/board')
-rw-r--r--src/board/board.cpp69
-rw-r--r--src/board/fen.cpp15
2 files changed, 69 insertions, 15 deletions
diff --git a/src/board/board.cpp b/src/board/board.cpp
index 16efbd3..28183a2 100644
--- a/src/board/board.cpp
+++ b/src/board/board.cpp
@@ -61,6 +61,19 @@ UndoMove MakeMove(Move move, Game *g) {
undo.wasEnPassantCapture = true;
undo.enPassantCapturedSquare = capturedPawn;
undo.enPassantCapturedPiece = g->pieces[PositionToIndex(capturedPawn)];
+
+ bool captuaredColor = !piece.color;
+ uint64_t square = 1ULL << PositionToIndex(capturedPawn);
+
+ g->PieceBitboards[captuaredColor][PAWN] ^= square;
+
+ if (captuaredColor)
+ g->WhitePieceBitboard ^= square;
+ else
+ g->BlackPieceBitboard ^= square;
+
+ g->PieceBitboard = g->WhitePieceBitboard | g->BlackPieceBitboard;
+
g->pieces[PositionToIndex(capturedPawn)] = {false, NONEPIECE};
}
@@ -69,11 +82,11 @@ UndoMove MakeMove(Move move, Game *g) {
// Adding enpassant
if (piece.type == PAWN) {
Position to = move.To;
- to.rank -= static_cast<uint8_t>(piece.color ? -2 : 2);
+ to.rank -= static_cast<uint8_t>(piece.color ? 2 : -2);
if (to == move.From) {
g->canEnpassant = true;
Position target = move.From;
- target.rank += static_cast<uint8_t>(piece.color ? -1 : 1);
+ target.rank += static_cast<uint8_t>(piece.color ? 1 : -1);
g->enPassant = target;
};
}
@@ -98,6 +111,19 @@ UndoMove MakeMove(Move move, Game *g) {
Position rookTo = move.From;
rookTo.file = 5;
+ uint64_t from = 1ULL << PositionToIndex(rookFrom);
+ uint64_t to = 1ULL << PositionToIndex(rookTo);
+
+ g->PieceBitboards[piece.color][ROOK] ^= from;
+ g->PieceBitboards[piece.color][ROOK] ^= to;
+
+ if (piece.color)
+ g->WhitePieceBitboard ^= from | to;
+ else
+ g->BlackPieceBitboard ^= from | to;
+
+ g->PieceBitboard = g->WhitePieceBitboard | g->BlackPieceBitboard;
+
g->pieces[PositionToIndex(rookTo)] = g->pieces[PositionToIndex(rookFrom)];
g->pieces[PositionToIndex(rookFrom)] = {false, NONEPIECE};
} else {
@@ -108,6 +134,19 @@ UndoMove MakeMove(Move move, Game *g) {
Position rookTo = move.From;
rookTo.file = 3;
+ uint64_t from = 1ULL << PositionToIndex(rookFrom);
+ uint64_t to = 1ULL << PositionToIndex(rookTo);
+
+ g->PieceBitboards[piece.color][ROOK] ^= from;
+ g->PieceBitboards[piece.color][ROOK] ^= to;
+
+ if (piece.color)
+ g->WhitePieceBitboard ^= from | to;
+ else
+ g->BlackPieceBitboard ^= from | to;
+
+ g->PieceBitboard = g->WhitePieceBitboard | g->BlackPieceBitboard;
+
g->pieces[PositionToIndex(rookTo)] = g->pieces[PositionToIndex(rookFrom)];
g->pieces[PositionToIndex(rookFrom)] = {false, NONEPIECE};
}
@@ -162,6 +201,31 @@ UndoMove MakeMove(Move move, Game *g) {
}
// playing the moves
+ uint64_t from = 1ULL << PositionToIndex(move.From);
+ uint64_t to = 1ULL << PositionToIndex(move.To);
+
+ // remove moving piece from source
+ g->PieceBitboards[piece.color][undo.movedPiece.type] ^= from;
+
+ // remove captured piece
+ if (piece2.type != NONEPIECE) {
+ g->PieceBitboards[piece2.color][piece2.type] ^= to;
+
+ if (piece2.color)
+ g->WhitePieceBitboard ^= to;
+ else
+ g->BlackPieceBitboard ^= to;
+ }
+
+ // add moving piece to destination
+ g->PieceBitboards[piece.color][piece.type] ^= to;
+
+ if (piece.color)
+ g->WhitePieceBitboard ^= from | to;
+ else
+ g->BlackPieceBitboard ^= from | to;
+
+ g->PieceBitboard = g->WhitePieceBitboard | g->BlackPieceBitboard;
g->pieces[PositionToIndex(move.To)] = piece;
g->pieces[PositionToIndex(move.From)] = {false, NONEPIECE};
@@ -182,7 +246,6 @@ UndoMove MakeMove(Move move, Game *g) {
if (g->halfMoveClock >= 50) {
g->state = DRAW;
}
- UpdateHelpers(g);
return undo;
};
void UnMakeMove(UndoMove undo, Game *g) {
diff --git a/src/board/fen.cpp b/src/board/fen.cpp
index fc52bfe..c4c5e7a 100644
--- a/src/board/fen.cpp
+++ b/src/board/fen.cpp
@@ -2,7 +2,6 @@
#include "board.hpp"
#include <cassert>
#include <cctype>
-#include <cstdint>
#include <cstdlib>
#include <print>
#include <string>
@@ -14,7 +13,7 @@ bool BLACK = false;
void setBoardFen(const std::string fen, Game *g) {
// example fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
int file = 0;
- int rank = 0;
+ int rank = 7;
Position enpassant = {};
enum parserState {
@@ -52,14 +51,6 @@ void setBoardFen(const std::string fen, Game *g) {
}
}
if (parser_state == ENPASSANT) {
- if (toupper(c) >= 'A' && toupper(c) <= 'H') {
- enpassant.file = static_cast<uint8_t>(toupper(c) - 'A');
- g->canEnpassant = true;
- }
- if (c >= '1' && c <= '8') {
- enpassant.rank = static_cast<uint8_t>(c - '0');
- g->canEnpassant = true;
- }
continue;
}
if (parser_state == CASTLE) {
@@ -111,8 +102,8 @@ void setBoardFen(const std::string fen, Game *g) {
}
if (c == '/') {
file = 0;
- rank++;
- if (rank >= 8) {
+ rank--;
+ if (rank < 0) {
std::println("Fatal: rank was to big");
assert(false && "Malformed rank");
exit(1);