1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-18 18:15:08 +00:00

LibChess: Early check of legal move

One of the conditions for legal move is that the target square is not
occupied by a piece of the same color as the moving piece.

Instead of checking this for each piece separately at the end, we can
check this at the beginning and avoid more expensive checks.
This commit is contained in:
Martin Blicha 2021-07-25 00:34:12 +02:00 committed by Andreas Kling
parent d5fdb97a81
commit 0505a815f7

View file

@ -421,6 +421,27 @@ bool Board::is_legal_no_check(const Move& move, Color color) const
// attempted move outside of board // attempted move outside of board
return false; return false;
// Check castling first to allow dragging king onto the rook.
if (piece.type == Type::King) {
if (color == Color::White) {
if ((move.to == Square("a1") || move.to == Square("c1")) && m_white_can_castle_queenside && get_piece(Square("b1")).type == Type::None && get_piece(Square("c1")).type == Type::None && get_piece(Square("d1")).type == Type::None) {
return true;
} else if ((move.to == Square("h1") || move.to == Square("g1")) && m_white_can_castle_kingside && get_piece(Square("f1")).type == Type::None && get_piece(Square("g1")).type == Type::None) {
return true;
}
} else {
if ((move.to == Square("a8") || move.to == Square("c8")) && m_black_can_castle_queenside && get_piece(Square("b8")).type == Type::None && get_piece(Square("c8")).type == Type::None && get_piece(Square("d8")).type == Type::None) {
return true;
} else if ((move.to == Square("h8") || move.to == Square("g8")) && m_black_can_castle_kingside && get_piece(Square("f8")).type == Type::None && get_piece(Square("g8")).type == Type::None) {
return true;
}
}
}
if (piece.color == get_piece(move.to).color)
// Attempted move to a square occupied by a piece of the same color.
return false;
if (piece.type == Type::Pawn) { if (piece.type == Type::Pawn) {
int dir = (color == Color::White) ? +1 : -1; int dir = (color == Color::White) ? +1 : -1;
int start_rank = (color == Color::White) ? 1 : 6; int start_rank = (color == Color::White) ? 1 : 6;
@ -459,7 +480,7 @@ bool Board::is_legal_no_check(const Move& move, Color color) const
} else if (piece.type == Type::Knight) { } else if (piece.type == Type::Knight) {
int rank_delta = abs(move.to.rank - move.from.rank); int rank_delta = abs(move.to.rank - move.from.rank);
int file_delta = abs(move.to.file - move.from.file); int file_delta = abs(move.to.file - move.from.file);
if (get_piece(move.to).color != color && max(rank_delta, file_delta) == 2 && min(rank_delta, file_delta) == 1) { if (max(rank_delta, file_delta) == 2 && min(rank_delta, file_delta) == 1) {
return true; return true;
} }
} else if (piece.type == Type::Bishop) { } else if (piece.type == Type::Bishop) {
@ -473,11 +494,8 @@ bool Board::is_legal_no_check(const Move& move, Color color) const
return false; return false;
} }
} }
if (get_piece(move.to).color != color) {
return true; return true;
} }
}
} else if (piece.type == Type::Rook) { } else if (piece.type == Type::Rook) {
int rank_delta = move.to.rank - move.from.rank; int rank_delta = move.to.rank - move.from.rank;
int file_delta = move.to.file - move.from.file; int file_delta = move.to.file - move.from.file;
@ -489,11 +507,8 @@ bool Board::is_legal_no_check(const Move& move, Color color) const
return false; return false;
} }
} }
if (get_piece(move.to).color != color) {
return true; return true;
} }
}
} else if (piece.type == Type::Queen) { } else if (piece.type == Type::Queen) {
int rank_delta = move.to.rank - move.from.rank; int rank_delta = move.to.rank - move.from.rank;
int file_delta = move.to.file - move.from.file; int file_delta = move.to.file - move.from.file;
@ -505,39 +520,16 @@ bool Board::is_legal_no_check(const Move& move, Color color) const
return false; return false;
} }
} }
if (get_piece(move.to).color != color) {
return true; return true;
} }
}
} else if (piece.type == Type::King) { } else if (piece.type == Type::King) {
int rank_delta = move.to.rank - move.from.rank; int rank_delta = move.to.rank - move.from.rank;
int file_delta = move.to.file - move.from.file; int file_delta = move.to.file - move.from.file;
if (abs(rank_delta) <= 1 && abs(file_delta) <= 1) { if (abs(rank_delta) <= 1 && abs(file_delta) <= 1) {
if (get_piece(move.to).color != color) {
return true; return true;
} }
} }
if (color == Color::White) {
if ((move.to == Square("a1") || move.to == Square("c1")) && m_white_can_castle_queenside && get_piece(Square("b1")).type == Type::None && get_piece(Square("c1")).type == Type::None && get_piece(Square("d1")).type == Type::None) {
return true;
} else if ((move.to == Square("h1") || move.to == Square("g1")) && m_white_can_castle_kingside && get_piece(Square("f1")).type == Type::None && get_piece(Square("g1")).type == Type::None) {
return true;
}
} else {
if ((move.to == Square("a8") || move.to == Square("c8")) && m_black_can_castle_queenside && get_piece(Square("b8")).type == Type::None && get_piece(Square("c8")).type == Type::None && get_piece(Square("d8")).type == Type::None) {
return true;
} else if ((move.to == Square("h8") || move.to == Square("g8")) && m_black_can_castle_kingside && get_piece(Square("f8")).type == Type::None && get_piece(Square("g8")).type == Type::None) {
return true;
}
}
}
return false; return false;
} }