diff options
| author | Adam <adammegarules1@gmail.com> | 2026-08-23 09:32:50 +0200 |
|---|---|---|
| committer | Adam <adammegarules1@gmail.com> | 2026-08-23 09:32:50 +0200 |
| commit | 393e544bbc3a14e38179bc7a73ca79690c154b28 (patch) | |
| tree | c449df4446345061b873f250d7405486a99ab86a | |
| parent | 101f58159685d72756bc08e26e64d56e607809b3 (diff) | |
refactor(move): making makemove function be nicer
| -rw-r--r-- | src/board/board.cpp | 174 | ||||
| -rw-r--r-- | src/hash.cpp | 2 | ||||
| -rw-r--r-- | src/hash.hpp | 2 |
3 files changed, 83 insertions, 95 deletions
diff --git a/src/board/board.cpp b/src/board/board.cpp index 48d0975..22a86bd 100644 --- a/src/board/board.cpp +++ b/src/board/board.cpp @@ -112,6 +112,73 @@ bool isRepetionDraw(uint64_t key, Game *g) { } return repetions >= 3; }; +static void updateCastlingRights(uint8_t fromSquare, Piece movingPiece, + uint8_t toSquare, Piece captuaredPiece, + Game *g) { + // if king has moved then that side cant castle + 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; + } + } + + // if rook has moved + 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; + } + } + } + + // if the rook was captuared he cant castle + 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; + } + } + } +} static void movePiece(const uint8_t &fromSquare, const Piece &fromPiece, const uint8_t &toSquare, const Piece &toPiece, Game *g) { @@ -180,10 +247,10 @@ Undo MakeMove(uint16_t move, Game *g) { Piece movingPiece = g->pieces[fromSquare]; Piece captuaredPiece = g->pieces[toSquare]; - if (captuaredPiece.type != NONEPIECE) { - if (captuaredPiece.color == g->turn) { - assert(false && "capturing friendly piece error"); - } + if (captuaredPiece.type != NONEPIECE && captuaredPiece.color == g->turn) { + assert(false && "capturing friendly piece error"); + std::cout << "capturing friendly piece error"; + exit(1); } // clock @@ -227,16 +294,11 @@ Undo MakeMove(uint16_t move, Game *g) { g->canEnpassant = false; // Adding enpassant - if (movingPiece.type == PAWN) { - int to = toSquare; - // check if a pawn has moved two squares - to -= movingPiece.color ? 2 * 8 : -2 * 8; - if (to == fromSquare) { - g->canEnpassant = true; - int target = fromSquare; - target += movingPiece.color ? 8 : -8; - g->enPassant = IndexToPosition(target); - }; + if (movingPiece.type == PAWN && abs(fromSquare - toSquare) == 16) { + g->canEnpassant = true; + int target = fromSquare; + target += movingPiece.color ? 8 : -8; + g->enPassant = IndexToPosition(target); } if (movingPiece.type == PAWN && @@ -246,29 +308,17 @@ Undo MakeMove(uint16_t move, Game *g) { // caslte if (movingPiece.type == KING && fromSquare == (movingPiece.color ? 4 : 60) && - std::abs(toSquare - fromSquare) == 2) { + abs(toSquare - fromSquare) == 2) { undo.wasCastle = true; undo.CastledSide = toSquare > fromSquare; Position rookFrom = IndexToPosition(fromSquare); - rookFrom.file = 7; + rookFrom.file = undo.CastledSide ? 7 : 0; Position rookTo = IndexToPosition(toSquare); - rookTo.file = 5; - - if (undo.CastledSide) { - // king side: h -> f - rookFrom.file = 7; + rookTo.file = undo.CastledSide ? 5 : 3; - rookTo.file = 5; - - } else { - // Queenside: a -> d - rookFrom.file = 0; - - rookTo.file = 3; - }; uint64_t from = 1ULL << PositionToIndex(rookFrom); uint64_t to = 1ULL << PositionToIndex(rookTo); @@ -288,75 +338,13 @@ Undo MakeMove(uint16_t move, Game *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; - } - } - - // if rook has moved - 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; - } - } - } - - 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; - } - } - } - + updateCastlingRights(fromSquare, movingPiece, toSquare, captuaredPiece, g); // playing the moves movePiece(fromSquare, movingPiece, toSquare, captuaredPiece, g); // changing who turn it is g->turn = !g->turn; - xorSidekey(g); // toggle the side key + toogleSideKey(g); // toggle the side key xorEnpassantKey(g); // add new en passant key g->history.push_back(g->hash); diff --git a/src/hash.cpp b/src/hash.cpp index 5d6a72d..ad011b9 100644 --- a/src/hash.cpp +++ b/src/hash.cpp @@ -340,7 +340,7 @@ void xorCastleKey(bool color, bool IS_KING_SIDE, Game *g) { g->hash ^= CastleKeys[3]; }; } -void xorSidekey(Game *g) { g->hash ^= SideKey; } +void toogleSideKey(Game *g) { g->hash ^= SideKey; } void xorEnpassantKey(Game *g) { if (g->canEnpassant && IsEnpassantLegal(g)) { g->hash ^= EnPassantKeys[g->enPassant.file]; diff --git a/src/hash.hpp b/src/hash.hpp index 6253363..f56b4d2 100644 --- a/src/hash.hpp +++ b/src/hash.hpp @@ -7,7 +7,7 @@ void InitHashing(); uint64_t GenerateHashFromScratch(Game *b); void xorCastleKey(bool color, bool IS_KING_SIDE, Game *g); -void xorSidekey(Game *g); +void toogleSideKey(Game *g); void xorEnpassantKey(Game *g); void xorSquare(uint8_t square, Game *g); |
