aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam <adammegarules1@gmail.com>2026-08-19 10:37:53 +0200
committerAdam <adammegarules1@gmail.com>2026-08-19 10:37:53 +0200
commit37f7feaf48fba54dc993a5120995b8ca465000bd (patch)
tree52012fe42d0b95e5be3195fbb2f0dbd32040d613
parentd510e0a23ad67a12d993792751c4c23b07f2e8c7 (diff)
refactor(move): making a helper
-rw-r--r--src/board/board.cpp80
1 files changed, 45 insertions, 35 deletions
diff --git a/src/board/board.cpp b/src/board/board.cpp
index 822e1b5..4ca0667 100644
--- a/src/board/board.cpp
+++ b/src/board/board.cpp
@@ -4,6 +4,7 @@
#include <cstdlib>
#include <cstring>
#include <iostream>
+#include <numbers>
#include "board.hpp"
#include "moves.hpp"
@@ -113,6 +114,39 @@ 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) {
+ uint64_t from = 1ULL << fromSquare;
+ uint64_t to = 1ULL << toSquare;
+
+ PieceType sourceType = g->pieces[fromSquare].type;
+ // remove moving piece from source
+
+ g->PieceBitboards[fromPiece.color][sourceType] ^= from;
+
+ if (toPiece.type != NONEPIECE) {
+ g->PieceBitboards[toPiece.color][toPiece.type] ^= to;
+
+ if (toPiece.color) {
+ g->WhitePieceBitboard ^= to;
+ } else {
+ g->BlackPieceBitboard ^= to;
+ }
+ }
+ // add moving piece to destination
+ g->PieceBitboards[fromPiece.color][fromPiece.type] ^= to;
+
+ if (fromPiece.color) {
+ g->WhitePieceBitboard ^= from | to;
+ } else {
+ g->BlackPieceBitboard ^= from | to;
+ }
+
+ g->PieceBitboard = g->WhitePieceBitboard | g->BlackPieceBitboard;
+ g->pieces[toSquare] = fromPiece;
+ g->pieces[fromSquare] = {.color = false, .type = NONEPIECE};
+};
+
Undo MakeMove(uint16_t move, Game *g) {
uint8_t fromSquare = getFromValueFromMove(move);
uint8_t toSquare = getToValueFromMove(move);
@@ -230,11 +264,11 @@ Undo MakeMove(uint16_t move, Game *g) {
g->PieceBitboards[piece.color][ROOK] ^= from;
g->PieceBitboards[piece.color][ROOK] ^= to;
- if (piece.color)
+ if (piece.color) {
g->WhitePieceBitboard ^= from | to;
- else
+ } else {
g->BlackPieceBitboard ^= from | to;
-
+ }
g->PieceBitboard = g->WhitePieceBitboard | g->BlackPieceBitboard;
g->pieces[PositionToIndex(rookTo)] = g->pieces[PositionToIndex(rookFrom)];
@@ -253,15 +287,18 @@ Undo MakeMove(uint16_t move, Game *g) {
g->PieceBitboards[piece.color][ROOK] ^= from;
g->PieceBitboards[piece.color][ROOK] ^= to;
- if (piece.color)
+ if (piece.color) {
g->WhitePieceBitboard ^= from | to;
- else
+ } 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};
+ g->pieces[PositionToIndex(rookFrom)] = {
+ .color = false,
+ .type = NONEPIECE,
+ };
}
}
@@ -321,34 +358,7 @@ Undo MakeMove(uint16_t move, Game *g) {
}
// playing the moves
- uint64_t from = 1ULL << fromSquare;
- uint64_t to = 1ULL << toSquare;
-
- // 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[toSquare] = piece;
- g->pieces[fromSquare] = {.color = false, .type = NONEPIECE};
+ movePiece(fromSquare, piece, toSquare, piece2, g);
// changing who turn it is
g->turn = !g->turn;