Simplified making and unmaking moves with helper functions + fixed minor bugs
This commit is contained in:
1 parent
8535042c04
commit
e0444257b3
2 files changed
+50
-72
No files matched your search
+46
-71
@@ -282,23 +282,31 @@ bool Board::InCheck(bool side) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Board::SetPiece(int piece, int square, bool color) {
|
||||||
|
pieces[piece].SetBit(square);
|
||||||
|
colors[color].SetBit(square);
|
||||||
|
occupied.SetBit(square);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Board::RemovePiece(int piece, int square, bool color) {
|
||||||
|
pieces[piece].PopBit(square);
|
||||||
|
colors[color].PopBit(square);
|
||||||
|
occupied.PopBit(square);
|
||||||
|
}
|
||||||
|
|
||||||
void Board::Promote(int square, int pieceType, int color, bool isCapture) {
|
void Board::Promote(int square, int pieceType, int color, bool isCapture) {
|
||||||
if (isCapture) {
|
if (isCapture) {
|
||||||
int targetType = GetPieceType(square);
|
int targetType = GetPieceType(square);
|
||||||
std::cout << targetType << '\n';
|
RemovePiece(targetType, square, !color);
|
||||||
|
|
||||||
pieces[targetType].PopBit(square);
|
|
||||||
colors[!color].PopBit(square);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
occupied.SetBit(square);
|
SetPiece(pieceType, square, color);
|
||||||
pieces[pieceType].SetBit(square);
|
|
||||||
colors[color].SetBit(square);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Board::DoMove(Move move) {
|
void Board::MakeMove(Move move) {
|
||||||
int oldEPTarget = enPassantTarget;
|
int oldEPTarget = enPassantTarget;
|
||||||
enPassantTarget = a1; // resetting (a1 is impossible)
|
enPassantTarget = a1; // resetting (a1 is impossible)
|
||||||
|
std::array<bool, 4> oldCastlingRights = castlingRights;
|
||||||
uint16_t moveType = move.moveFlags;
|
uint16_t moveType = move.moveFlags;
|
||||||
int attackerType = GetPieceType(move.moveFrom);
|
int attackerType = GetPieceType(move.moveFrom);
|
||||||
bool attackerColor = GetPieceColor(move.moveFrom);
|
bool attackerColor = GetPieceColor(move.moveFrom);
|
||||||
@@ -307,50 +315,29 @@ void Board::DoMove(Move move) {
|
|||||||
|
|
||||||
int direction = attackerColor ? south : north;
|
int direction = attackerColor ? south : north;
|
||||||
|
|
||||||
pieces[attackerType].PopBit(move.moveFrom);
|
// Removing attacker from old square
|
||||||
colors[attackerColor].PopBit(move.moveFrom);
|
RemovePiece(attackerType, move.moveFrom, attackerColor);
|
||||||
occupied.PopBit(move.moveFrom);
|
|
||||||
|
|
||||||
colors[attackerColor].SetBit(move.moveTo);
|
if (moveType == doublePawnPush) {
|
||||||
|
|
||||||
// if the move is NOT a promotion
|
|
||||||
if (moveType < knightPromotion) {
|
|
||||||
pieces[attackerType].SetBit(move.moveTo);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (moveType == quiet) {
|
|
||||||
occupied.SetBit(move.moveTo);
|
|
||||||
} else if (moveType == doublePawnPush) {
|
|
||||||
occupied.SetBit(move.moveTo);
|
|
||||||
enPassantTarget = move.moveFrom + direction;
|
enPassantTarget = move.moveFrom + direction;
|
||||||
} else if (moveType == capture || moveType == epCapture) {
|
} else if (moveType == capture || moveType == epCapture) {
|
||||||
pieces[targetType].PopBit(move.moveTo);
|
RemovePiece(targetType, move.moveTo, !attackerColor);
|
||||||
colors[!attackerColor].PopBit(move.moveTo);
|
|
||||||
} else if (moveType == queenCastle) {
|
} else if (moveType == queenCastle) {
|
||||||
int rookSquare = attackerColor ? a8 : a1;
|
int rookSquare = attackerColor ? a8 : a1;
|
||||||
|
|
||||||
// Removing rook from old position
|
// Removing rook from old position
|
||||||
pieces[Rook].PopBit(rookSquare);
|
RemovePiece(Rook, rookSquare, attackerColor);
|
||||||
colors[attackerColor].PopBit(rookSquare);
|
|
||||||
occupied.PopBit(rookSquare);
|
|
||||||
|
|
||||||
// Setting rook on new position
|
// Setting rook on new position
|
||||||
pieces[Rook].SetBit(rookSquare + 3);
|
SetPiece(Rook, rookSquare + 3, attackerColor);
|
||||||
colors[attackerColor].SetBit(rookSquare + 3);
|
|
||||||
occupied.SetBit(rookSquare + 3);
|
|
||||||
} else if (moveType == kingCastle) {
|
} else if (moveType == kingCastle) {
|
||||||
int rookSquare = attackerColor ? h8 : h1;
|
int rookSquare = attackerColor ? h8 : h1;
|
||||||
std::cout << squareCoords[rookSquare - 2] << '\n';
|
|
||||||
|
|
||||||
// Removing rook from old position
|
// Removing rook from old position
|
||||||
pieces[Rook].PopBit(rookSquare);
|
RemovePiece(Rook, rookSquare, attackerColor);
|
||||||
colors[attackerColor].PopBit(rookSquare);
|
|
||||||
occupied.PopBit(rookSquare);
|
|
||||||
|
|
||||||
// Setting rook on new position
|
// Setting rook on new position
|
||||||
pieces[Rook].SetBit(rookSquare - 2);
|
SetPiece(Rook, rookSquare - 2, attackerColor);
|
||||||
colors[attackerColor].SetBit(rookSquare - 2);
|
|
||||||
occupied.SetBit(rookSquare - 2);
|
|
||||||
} else if (moveType == knightPromotion) {
|
} else if (moveType == knightPromotion) {
|
||||||
Promote(move.moveTo, Knight, attackerColor, false);
|
Promote(move.moveTo, Knight, attackerColor, false);
|
||||||
} else if (moveType == bishopPromotion) {
|
} else if (moveType == bishopPromotion) {
|
||||||
@@ -369,6 +356,11 @@ void Board::DoMove(Move move) {
|
|||||||
Promote(move.moveTo, Queen, attackerColor, true);
|
Promote(move.moveTo, Queen, attackerColor, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Setting attacker on the target square if its not a promotion
|
||||||
|
if (moveType < knightPromotion) {
|
||||||
|
SetPiece(attackerType, move.moveTo, attackerColor);
|
||||||
|
}
|
||||||
|
|
||||||
// Removing the right to castle on king movement
|
// Removing the right to castle on king movement
|
||||||
if (attackerType == King) {
|
if (attackerType == King) {
|
||||||
castlingRights[attackerColor * 2] = false;
|
castlingRights[attackerColor * 2] = false;
|
||||||
@@ -387,7 +379,7 @@ void Board::DoMove(Move move) {
|
|||||||
halfMoves++;
|
halfMoves++;
|
||||||
if (halfMoves & 2 == 0) fullMoves++;
|
if (halfMoves & 2 == 0) fullMoves++;
|
||||||
sideToMove = !attackerColor;
|
sideToMove = !attackerColor;
|
||||||
lastMove = LastMove(move, targetType, !attackerColor, oldEPTarget, castlingRights, attackerType);
|
lastMove = LastMove(move, targetType, !attackerColor, oldEPTarget, oldCastlingRights, attackerType);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Board::UnmakeMove() {
|
void Board::UnmakeMove() {
|
||||||
@@ -395,60 +387,43 @@ void Board::UnmakeMove() {
|
|||||||
uint16_t moveType = lastMove.moveFlags;
|
uint16_t moveType = lastMove.moveFlags;
|
||||||
|
|
||||||
// Setting back the attacker
|
// Setting back the attacker
|
||||||
pieces[lastMove.attackerPiece].SetBit(lastMove.moveFrom);
|
SetPiece(lastMove.attackerPiece, lastMove.moveFrom, !lastMove.capturedColor);
|
||||||
colors[!lastMove.capturedColor].SetBit(lastMove.moveFrom);
|
|
||||||
occupied.SetBit(lastMove.moveFrom);
|
|
||||||
|
|
||||||
colors[!lastMove.capturedColor].PopBit(lastMove.moveTo);
|
|
||||||
|
|
||||||
// if the move was NOT a promotion
|
|
||||||
if (moveType < knightPromotion) {
|
|
||||||
pieces[lastMove.attackerPiece].PopBit(lastMove.moveTo);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Setting back the target
|
// Setting back the target
|
||||||
if (moveType == quiet || moveType == doublePawnPush) {
|
if (moveType == quiet || moveType == doublePawnPush) {
|
||||||
occupied.PopBit(lastMove.moveTo);
|
RemovePiece(lastMove.attackerPiece, lastMove.moveTo, !lastMove.capturedColor);
|
||||||
} else if (moveType == capture || moveType == epCapture) {
|
} else if (moveType == capture || moveType == epCapture) {
|
||||||
pieces[lastMove.capturedPiece].SetBit(lastMove.moveTo);
|
SetPiece(lastMove.capturedPiece, lastMove.moveTo, lastMove.capturedColor);
|
||||||
colors[lastMove.capturedColor].SetBit(lastMove.moveTo);
|
|
||||||
} else if (moveType == queenCastle) {
|
} else if (moveType == queenCastle) {
|
||||||
int rookSquare = !lastMove.capturedColor ? a8 : a1;
|
int rookSquare = !lastMove.capturedColor ? a8 : a1;
|
||||||
|
|
||||||
// Removing rook from new position
|
// Removing rook from new position
|
||||||
pieces[Rook].PopBit(rookSquare + 3);
|
RemovePiece(Rook, rookSquare + 3, !lastMove.capturedColor);
|
||||||
colors[!lastMove.capturedColor].PopBit(rookSquare + 3);
|
|
||||||
occupied.PopBit(rookSquare + 3);
|
|
||||||
|
|
||||||
// Setting rook back on old position
|
// Setting rook back on old position
|
||||||
pieces[Rook].SetBit(rookSquare);
|
SetPiece(Rook, rookSquare, !lastMove.capturedColor);
|
||||||
colors[!lastMove.capturedColor].SetBit(rookSquare);
|
|
||||||
occupied.SetBit(rookSquare);
|
|
||||||
} else if (moveType == kingCastle) {
|
} else if (moveType == kingCastle) {
|
||||||
int rookSquare = !lastMove.capturedColor ? a8 : a1;
|
int rookSquare = !lastMove.capturedColor ? h8 : h1;
|
||||||
|
|
||||||
// Removing rook from new position
|
// Removing rook from new position
|
||||||
pieces[Rook].PopBit(rookSquare - 2);
|
RemovePiece(Rook, rookSquare - 2, !lastMove.capturedColor);
|
||||||
colors[!lastMove.capturedColor].PopBit(rookSquare - 2);
|
|
||||||
occupied.PopBit(rookSquare - 2);
|
|
||||||
|
|
||||||
// Setting rook back on old position
|
// Setting rook back to old position
|
||||||
pieces[Rook].SetBit(rookSquare);
|
SetPiece(Rook, rookSquare, !lastMove.capturedColor);
|
||||||
colors[!lastMove.capturedColor].SetBit(rookSquare);
|
} else if (moveType >= knightPromotion && moveType <= queenPromotion) {
|
||||||
occupied.SetBit(rookSquare);
|
|
||||||
} else if (moveType == rookPromotion) {
|
|
||||||
for (int piece = Pawn; piece <= King; piece++) {
|
for (int piece = Pawn; piece <= King; piece++) {
|
||||||
pieces[piece].PopBit(lastMove.moveTo);
|
RemovePiece(piece, lastMove.moveTo, !lastMove.capturedColor);
|
||||||
}
|
}
|
||||||
occupied.PopBit(lastMove.moveTo);
|
|
||||||
} else if (moveType >= knightPromoCapture) {
|
} else if (moveType >= knightPromoCapture) {
|
||||||
for (int piece = Pawn; piece <= King; piece++) {
|
for (int piece = Pawn; piece <= King; piece++) {
|
||||||
pieces[piece].PopBit(lastMove.moveTo);
|
RemovePiece(piece, lastMove.moveTo, !lastMove.capturedColor);
|
||||||
}
|
}
|
||||||
colors[lastMove.capturedColor].SetBit(lastMove.moveTo);
|
SetPiece(lastMove.capturedPiece, lastMove.moveTo, lastMove.capturedColor);
|
||||||
pieces[lastMove.capturedPiece].SetBit(lastMove.moveTo);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Removing attacker piece from new square
|
||||||
|
RemovePiece(lastMove.attackerPiece, lastMove.moveTo, !lastMove.capturedColor);
|
||||||
|
|
||||||
castlingRights = lastMove.castlingRights;
|
castlingRights = lastMove.castlingRights;
|
||||||
if (halfMoves & 2 == 0) fullMoves--;
|
if (halfMoves & 2 == 0) fullMoves--;
|
||||||
halfMoves--;
|
halfMoves--;
|
||||||
|
|||||||
+4
-1
@@ -40,7 +40,10 @@ public:
|
|||||||
|
|
||||||
bool InCheck(bool side);
|
bool InCheck(bool side);
|
||||||
|
|
||||||
|
void SetPiece(int piece, int square, bool color);
|
||||||
|
void RemovePiece(int piece, int square, bool color);
|
||||||
|
|
||||||
void Promote(int square, int pieceType, int color, bool isCapture);
|
void Promote(int square, int pieceType, int color, bool isCapture);
|
||||||
void DoMove(Move move);
|
void MakeMove(Move move);
|
||||||
void UnmakeMove();
|
void UnmakeMove();
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user