aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam <adammegarules1@gmail.com>2026-08-19 20:16:19 +0200
committerAdam <adammegarules1@gmail.com>2026-08-19 20:16:19 +0200
commit692029dd4614fd4b5f99538be0b46153eb8a19bc (patch)
treec1167d40dfcb1f3bd45c355babfb02b26bcee759
parent8ab2570ecc9a8d008e7d4cec8c89204ffd752547 (diff)
refactor(move): refactoring move to be nicer and better
-rw-r--r--src/board/board.cpp115
1 files changed, 47 insertions, 68 deletions
diff --git a/src/board/board.cpp b/src/board/board.cpp
index d339e1e..81a3c4e 100644
--- a/src/board/board.cpp
+++ b/src/board/board.cpp
@@ -1,11 +1,9 @@
#include <cassert>
-#include <chrono>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <iostream>
-#include <numbers>
#include "board.hpp"
#include "moves.hpp"
@@ -176,37 +174,38 @@ Undo MakeMove(uint16_t move, Game *g) {
undo.OldBlackCastleQueen = g->blackCastleQueen;
// play move
- Piece piece = g->pieces[fromSquare];
- Piece piece2 = g->pieces[toSquare];
+ Piece movingPiece = g->pieces[fromSquare];
+ Piece captuaredPiece = g->pieces[toSquare];
- if (piece2.type != NONEPIECE) {
- if (piece2.color == g->turn) {
+ if (captuaredPiece.type != NONEPIECE) {
+ if (captuaredPiece.color == g->turn) {
assert(false && "capturing friendly piece error");
}
}
// clock
g->MoveClock++;
- if (piece.type == PAWN || piece2.type != NONEPIECE) {
+ g->halfMoveClock++;
+
+ if (movingPiece.type == PAWN || captuaredPiece.type != NONEPIECE) {
g->halfMoveClock = 0;
- } else {
- g->halfMoveClock++;
}
+
if (g->halfMoveClock >= 100) {
g->state = DRAW;
}
// Playing enpasstant
- if (piece.type == PAWN && g->canEnpassant &&
+ if (movingPiece.type == PAWN && g->canEnpassant &&
IndexToPosition(toSquare) == g->enPassant) {
Position capturedPawn = IndexToPosition(toSquare);
- capturedPawn.rank += piece.color ? -1 : 1;
+ capturedPawn.rank += movingPiece.color ? -1 : 1;
undo.wasEnPassantCapture = true;
undo.enPassantCapturedSquare = capturedPawn;
undo.enPassantCapturedPiece = g->pieces[PositionToIndex(capturedPawn)];
- bool captuaredColor = !piece.color;
+ bool captuaredColor = !movingPiece.color;
uint64_t square = 1ULL << PositionToIndex(capturedPawn);
g->PieceBitboards[captuaredColor][PAWN] ^= square;
@@ -225,14 +224,14 @@ Undo MakeMove(uint16_t move, Game *g) {
g->canEnpassant = false;
// Adding enpassant
- if (piece.type == PAWN) {
+ if (movingPiece.type == PAWN) {
int to = toSquare;
// check if a pawn has moved two squares
- to -= piece.color ? 2 * 8 : -2 * 8;
+ to -= movingPiece.color ? 2 * 8 : -2 * 8;
if (to == fromSquare) {
g->canEnpassant = true;
int target = fromSquare;
- target += piece.color ? 8 : -8;
+ target += movingPiece.color ? 8 : -8;
g->enPassant = IndexToPosition(target);
};
}
@@ -241,76 +240,56 @@ Undo MakeMove(uint16_t move, Game *g) {
// (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;
+ if (movingPiece.type == PAWN &&
+ IndexToPosition(toSquare).rank == (movingPiece.color ? 7 : 0)) {
+ movingPiece.type = promotion;
}
// caslte
- if (piece.type == KING &&
- IndexToPosition(fromSquare).rank == (piece.color ? 0 : 7) &&
+ if (movingPiece.type == KING && fromSquare == (movingPiece.color ? 4 : 60) &&
std::abs(toSquare - fromSquare) == 2) {
undo.wasCastle = true;
undo.CastledSide = toSquare > fromSquare;
+ Position rookFrom = IndexToPosition(fromSquare);
+ rookFrom.file = 7;
+
+ Position rookTo = IndexToPosition(toSquare);
+ rookTo.file = 5;
+
if (undo.CastledSide) {
- Position rookFrom = IndexToPosition(fromSquare);
+ // king side: h -> f
rookFrom.file = 7;
- Position rookTo = IndexToPosition(toSquare);
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 {
// Queenside: a -> d
- Position rookFrom = IndexToPosition(fromSquare);
rookFrom.file = 0;
- Position rookTo = IndexToPosition(toSquare);
rookTo.file = 3;
+ }
+ uint64_t from = 1ULL << PositionToIndex(rookFrom);
+ uint64_t to = 1ULL << PositionToIndex(rookTo);
- uint64_t from = 1ULL << PositionToIndex(rookFrom);
- uint64_t to = 1ULL << PositionToIndex(rookTo);
-
- g->PieceBitboards[piece.color][ROOK] ^= from;
- g->PieceBitboards[piece.color][ROOK] ^= to;
+ g->PieceBitboards[movingPiece.color][ROOK] ^= from;
+ g->PieceBitboards[movingPiece.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)] = {
- .color = false,
- .type = NONEPIECE,
- };
+ if (movingPiece.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)] = {.type = NONEPIECE};
}
- /*
- * remove castling right if king has moved
- * but only for color of the king
- */
- if (piece.type == KING) {
- if (piece.color) {
+ // remove castling right if king has moved
+ if (movingPiece.type == KING) {
+ if (movingPiece.color) {
g->whiteCastleKing = false;
g->whiteCastleQueen = false;
} else {
@@ -320,8 +299,8 @@ Undo MakeMove(uint16_t move, Game *g) {
}
// if rook has moved
- if (piece.type == ROOK) {
- if (piece.color) { // White
+ if (movingPiece.type == ROOK) {
+ if (movingPiece.color) { // White
if (fromSquare == 0) { // a1
g->whiteCastleQueen = false;
}
@@ -340,9 +319,9 @@ Undo MakeMove(uint16_t move, Game *g) {
}
// this part is written with ai
- if (piece2.type == ROOK) {
- if (piece2.color) { // White rook captured
- if (toSquare == 0) { // a1
+ if (captuaredPiece.type == ROOK) {
+ if (captuaredPiece.color) { // White rook captured
+ if (toSquare == 0) { // a1
g->whiteCastleQueen = false;
}
if (toSquare == 7) { // h1
@@ -361,7 +340,7 @@ Undo MakeMove(uint16_t move, Game *g) {
}
// playing the moves
- movePiece(fromSquare, piece, toSquare, piece2, g);
+ movePiece(fromSquare, movingPiece, toSquare, captuaredPiece, g);
// changing who turn it is
g->turn = !g->turn;