diff options
Diffstat (limited to 'src/board/board.cpp')
| -rw-r--r-- | src/board/board.cpp | 174 |
1 files changed, 81 insertions, 93 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); |
