added enPassant capturing + validating all moves
This commit is contained in:
1 parent
e0444257b3
commit
915b93ad5a
3 files changed
+34
-2
No files matched your search
+12
-2
@@ -241,8 +241,11 @@ void Board::ResetMoves() {
|
||||
}
|
||||
|
||||
void Board::ListMoves() {
|
||||
int count = 1;
|
||||
for (Move move : moveList) {
|
||||
std::cout << count << ". ";
|
||||
move.PrintMove();
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -320,8 +323,11 @@ void Board::MakeMove(Move move) {
|
||||
|
||||
if (moveType == doublePawnPush) {
|
||||
enPassantTarget = move.moveFrom + direction;
|
||||
} else if (moveType == capture || moveType == epCapture) {
|
||||
} else if (moveType == capture) {
|
||||
RemovePiece(targetType, move.moveTo, !attackerColor);
|
||||
} else if (moveType == epCapture) {
|
||||
targetType = Pawn;
|
||||
RemovePiece(targetType, move.moveTo - direction, !attackerColor);
|
||||
} else if (moveType == queenCastle) {
|
||||
int rookSquare = attackerColor ? a8 : a1;
|
||||
|
||||
@@ -386,14 +392,18 @@ void Board::UnmakeMove() {
|
||||
enPassantTarget = lastMove.enPassantTarget;
|
||||
uint16_t moveType = lastMove.moveFlags;
|
||||
|
||||
int direction = lastMove.capturedColor ? south : north;
|
||||
|
||||
// Setting back the attacker
|
||||
SetPiece(lastMove.attackerPiece, lastMove.moveFrom, !lastMove.capturedColor);
|
||||
|
||||
// Setting back the target
|
||||
if (moveType == quiet || moveType == doublePawnPush) {
|
||||
RemovePiece(lastMove.attackerPiece, lastMove.moveTo, !lastMove.capturedColor);
|
||||
} else if (moveType == capture || moveType == epCapture) {
|
||||
} else if (moveType == capture) {
|
||||
SetPiece(lastMove.capturedPiece, lastMove.moveTo, lastMove.capturedColor);
|
||||
} else if (moveType == epCapture) {
|
||||
SetPiece(lastMove.capturedPiece, lastMove.moveTo + direction, lastMove.capturedColor);
|
||||
} else if (moveType == queenCastle) {
|
||||
int rookSquare = !lastMove.capturedColor ? a8 : a1;
|
||||
|
||||
|
||||
@@ -9,6 +9,8 @@ int main() {
|
||||
|
||||
Board board;
|
||||
GenerateMoves(board);
|
||||
board.ListMoves();
|
||||
board.PrintBoard();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -503,6 +503,16 @@ static void GenKingMoves(Board &board, bool color) {
|
||||
}
|
||||
}
|
||||
|
||||
bool isLegalMove(Board &board, Move move) {
|
||||
board.MakeMove(move);
|
||||
if (board.InCheck(!board.sideToMove)) {
|
||||
board.UnmakeMove();
|
||||
return false;
|
||||
}
|
||||
board.UnmakeMove();
|
||||
return true;
|
||||
}
|
||||
|
||||
void GenerateMoves(Board &board) {
|
||||
board.moveList.clear();
|
||||
|
||||
@@ -523,4 +533,14 @@ void GenerateMoves(Board &board) {
|
||||
|
||||
GenKingMoves(board, White);
|
||||
GenKingMoves(board, Black);
|
||||
|
||||
int listSize = sizeof(board.moveList);
|
||||
// Filtering illegal moves
|
||||
for (int i = 0; i < listSize; i++) {
|
||||
int removedCount = 0;
|
||||
if (!isLegalMove(board, board.moveList[i])) {
|
||||
board.moveList.erase(board.moveList.begin() + i - removedCount);
|
||||
removedCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user