diff --git a/source/board.cpp b/source/board.cpp index 139ade9..26ba5e9 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -123,13 +123,13 @@ void Board::SetByFen(const char* fen) { castlingRights[White] = true; break; case 'Q': - castlingRights[White + 2] = true; + castlingRights[White + 1] = true; break; case 'k': - castlingRights[Black] = true; + castlingRights[Black * 2] = true; break; case 'q': - castlingRights[Black + 2] = true; + castlingRights[Black * 2 + 1] = true; break; default: if (*fen != '-') { @@ -292,6 +292,7 @@ void Board::DoMove(Move move) { int direction = attackerColor ? south : north; + pieces[attackerType].PopBit(move.moveFrom); colors[attackerColor].PopBit(move.moveFrom); occupied.PopBit(move.moveFrom); @@ -306,6 +307,46 @@ void Board::DoMove(Move move) { } else if (moveType == capture || moveType == epCapture) { pieces[targetType].PopBit(move.moveTo); colors[!attackerColor].PopBit(move.moveTo); + } else if (moveType == queenCastle) { + int rookSquare = attackerColor ? a8 : a1; + + // Removing rook from old position + pieces[Rook].PopBit(rookSquare); + colors[attackerColor].PopBit(rookSquare); + occupied.PopBit(rookSquare); + + // Setting rook on new position + pieces[Rook].SetBit(rookSquare + 3); + colors[attackerColor].SetBit(rookSquare + 3); + occupied.SetBit(rookSquare); + } else if (moveType == kingCastle) { + int rookSquare = attackerColor ? h8 : h1; + std::cout << squareCoords[rookSquare - 2] << '\n'; + + // Removing rook from old position + pieces[Rook].PopBit(rookSquare); + colors[attackerColor].PopBit(rookSquare); + occupied.PopBit(rookSquare); + + // Setting rook on new position + pieces[Rook].SetBit(rookSquare - 2); + colors[attackerColor].SetBit(rookSquare - 2); + occupied.SetBit(rookSquare); + } + + // Removing the right to castle on king movement + if (attackerType == King) { + castlingRights[attackerColor * 2] = false; + castlingRights[attackerColor * 2 + 1] = false; + } + + // Removing the right to castle on rook movement + if (attackerType == Rook) { + if ((Bitboard::GetSquare(move.moveFrom) & files[A] & (ranks[r_1] | ranks[r_8])).GetBoard()) { + castlingRights[attackerColor * 2 + 1] = false; + } else if ((Bitboard::GetSquare(move.moveFrom) & files[H] & (ranks[r_1] | ranks[r_8])).GetBoard()) { + castlingRights[attackerColor * 2] = false; + } } halfMoves++; diff --git a/source/move.h b/source/move.h index dc58509..528e261 100644 --- a/source/move.h +++ b/source/move.h @@ -1,9 +1,8 @@ #pragma once -#include "bitboards.h" enum MOVE_FLAGS { quiet, doublePawnPush, - kingCastle, QueenCastle, + kingCastle, queenCastle, capture, epCapture, knightPromotion, bishopPromotion, rookPromotion, queenPromotion, knightPromoCapture, bishopPromoCapture, rookPromoCapture, queenPromoCapture diff --git a/source/movegen.cpp b/source/movegen.cpp index b5bd374..26d81b6 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -280,7 +280,7 @@ static void GenPawnMoves(Board &board, bool color) { // Checking for possible en passant if (board.enPassantTarget) { - if ((attacks & Bitboard::GetSquare(board.enPassantTarget)).GetBoard() + if ((attacks & Bitboard::GetSquare(board.enPassantTarget)).GetBoard() && board.sideToMove == color) { board.AddMove(Move(square, board.enPassantTarget, epCapture)); @@ -466,6 +466,22 @@ static void GenKingMoves(Board &board, bool color) { captures.PopBit(targetSquare); } + + if (board.castlingRights[color * 2 + 1]) { + U64 QueenSide = color ? 0x1f00000000000000 : 0x1f; + int targetSquare = color ? c8 : c1; + if ((Bitboard(QueenSide) & board.occupied).popCount() == 2) { + board.AddMove(Move(square, targetSquare, queenCastle)); + } + } + + if (board.castlingRights[color * 2]) { + U64 KingSide = color ? 0xf000000000000000 : 0xf0; + int targetSquare = color ? g8 : g1; + if ((Bitboard(KingSide) & board.occupied).popCount() == 2) { + board.AddMove(Move(square, targetSquare, kingCastle)); + } + } } void GenerateMoves(Board &board) {