[bitboard, core, position] Implement proper castle move generation
Add a field to MoveSet called special that flags special moves that should be generated in the iter() method. This field is a u8. It only tracks castles in the first and second bits (kingside and queenside, respectively). The move iterator chains two maps over Option<()> that produce the kingside and queenside castle moves. With that done, finish the implementation of Position::player_can_castle by adding checks for whether the squares between the rook and king are clear, and that the king would not pass through a check. This is done with BitBoards! Finally, implement some logic in PositionBuilder that updates the position's castling flags based on the positions of king and rooks. Supporting changes: - Add Color:ALL and iterate on that slice - Add Castle::ALL and iterator on that slice - Add a CastlingParameters struct that contains BitBoard properties that describe squares that should be clear of pieces and squares that should not be attacked.
This commit is contained in:
parent
83a4e47e56
commit
1d7dada987
6 changed files with 234 additions and 60 deletions
|
@ -12,28 +12,6 @@ move_generator_declaration!(KingMoveGenerator, struct);
|
|||
move_generator_declaration!(KingMoveGenerator, new);
|
||||
move_generator_declaration!(KingMoveGenerator, getters);
|
||||
|
||||
impl KingMoveGenerator {
|
||||
#[allow(unused_variables)]
|
||||
fn king_side_castle(position: &Position, color: Color) -> Option<Move> {
|
||||
if !position.player_has_right_to_castle(color, Castle::KingSide) {
|
||||
return None;
|
||||
}
|
||||
|
||||
// TODO: Implement king side castle.
|
||||
None
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
fn queen_side_castle(position: &Position, color: Color) -> Option<Move> {
|
||||
if !position.player_has_right_to_castle(color, Castle::QueenSide) {
|
||||
return None;
|
||||
}
|
||||
|
||||
// TODO: Implement queen side castle.
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl MoveGeneratorInternal for KingMoveGenerator {
|
||||
fn piece(color: Color) -> Piece {
|
||||
Piece::king(color)
|
||||
|
@ -44,7 +22,7 @@ impl MoveGeneratorInternal for KingMoveGenerator {
|
|||
let color = piece.color();
|
||||
let square = placed_piece.square();
|
||||
|
||||
let safe_squares = !position.king_danger();
|
||||
let safe_squares = !position.king_danger(color);
|
||||
let all_king_moves = BitBoard::king_moves(square);
|
||||
|
||||
let empty_squares = position.empty_squares();
|
||||
|
@ -56,30 +34,25 @@ impl MoveGeneratorInternal for KingMoveGenerator {
|
|||
let quiet_moves = all_king_moves & safe_empty_squares;
|
||||
let capture_moves = all_king_moves & opposing_pieces_on_safe_squares;
|
||||
|
||||
// TODO: Handle checks. Prevent moving a king to a square attacked by a
|
||||
// piece of the opposite color.
|
||||
|
||||
let map_to_move = |sq| MoveBuilder::new(*piece, square, sq).build();
|
||||
|
||||
let king_side_castle = Self::king_side_castle(position, color);
|
||||
let queen_side_castle = Self::queen_side_castle(position, color);
|
||||
let quiet_moves = quiet_moves_bb
|
||||
.occupied_squares()
|
||||
.map(map_to_move)
|
||||
.chain(king_side_castle.iter().cloned())
|
||||
.chain(queen_side_castle.iter().cloned());
|
||||
let capture_moves = capture_moves_bb.occupied_squares().map(map_to_move);
|
||||
|
||||
MoveSet::new(placed_piece)
|
||||
let mut move_set = MoveSet::new(placed_piece)
|
||||
.quiet_moves(quiet_moves)
|
||||
.capture_moves(capture_moves)
|
||||
.capture_moves(capture_moves);
|
||||
|
||||
if position.player_can_castle(color, Castle::KingSide) {
|
||||
move_set.kingside_castle();
|
||||
}
|
||||
if position.player_can_castle(color, Castle::QueenSide) {
|
||||
move_set.queenside_castle();
|
||||
}
|
||||
|
||||
move_set
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{assert_move_list, position, r#move::AlgebraicMoveFormatter, PositionBuilder};
|
||||
use crate::{assert_move_list, position, test_position, PositionBuilder};
|
||||
use chessfriend_bitboard::bitboard;
|
||||
use chessfriend_core::{piece, Square};
|
||||
use std::collections::HashSet;
|
||||
|
@ -166,4 +139,87 @@ mod tests {
|
|||
|
||||
assert_eq!(generated_moves, expected_moves);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn white_king_unobstructed_castles() {
|
||||
let pos = test_position!(
|
||||
White King on E1,
|
||||
White Rook on A1,
|
||||
White Rook on H1,
|
||||
);
|
||||
|
||||
assert!(pos.player_can_castle(Color::White, Castle::KingSide));
|
||||
assert!(pos.player_can_castle(Color::White, Castle::QueenSide));
|
||||
|
||||
let generator = KingMoveGenerator::new(&pos, Color::White);
|
||||
let generated_moves: HashSet<Move> = generator.iter().collect();
|
||||
|
||||
let king = piece!(White King);
|
||||
assert!(generated_moves.contains(
|
||||
&MoveBuilder::new(king, Square::E1, Square::G1)
|
||||
.castle(Castle::KingSide)
|
||||
.build()
|
||||
));
|
||||
assert!(generated_moves.contains(
|
||||
&MoveBuilder::new(king, Square::E1, Square::C1)
|
||||
.castle(Castle::QueenSide)
|
||||
.build()
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn white_king_obstructed_queenside_castle() {
|
||||
let pos = test_position!(
|
||||
White King on E1,
|
||||
White Knight on B1,
|
||||
White Rook on A1,
|
||||
White Rook on H1,
|
||||
);
|
||||
|
||||
assert!(pos.player_can_castle(Color::White, Castle::KingSide));
|
||||
assert!(!pos.player_can_castle(Color::White, Castle::QueenSide));
|
||||
|
||||
let generator = KingMoveGenerator::new(&pos, Color::White);
|
||||
let generated_moves: HashSet<Move> = generator.iter().collect();
|
||||
|
||||
let king = piece!(White King);
|
||||
assert!(generated_moves.contains(
|
||||
&MoveBuilder::new(king, Square::E1, Square::G1)
|
||||
.castle(Castle::KingSide)
|
||||
.build()
|
||||
));
|
||||
assert!(!generated_moves.contains(
|
||||
&MoveBuilder::new(king, Square::E1, Square::C1)
|
||||
.castle(Castle::QueenSide)
|
||||
.build()
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn white_king_obstructed_kingside_castle() {
|
||||
let pos = test_position!(
|
||||
White King on E1,
|
||||
White Rook on A1,
|
||||
White Knight on G1,
|
||||
White Rook on H1,
|
||||
);
|
||||
|
||||
assert!(!pos.player_can_castle(Color::White, Castle::KingSide));
|
||||
assert!(pos.player_can_castle(Color::White, Castle::QueenSide));
|
||||
|
||||
let generator = KingMoveGenerator::new(&pos, Color::White);
|
||||
let generated_moves: HashSet<Move> = generator.iter().collect();
|
||||
|
||||
let king = piece!(White King);
|
||||
assert!(!generated_moves.contains(
|
||||
&MoveBuilder::new(king, Square::E1, Square::G1)
|
||||
.castle(Castle::KingSide)
|
||||
.build()
|
||||
));
|
||||
assert!(generated_moves.contains(
|
||||
&MoveBuilder::new(king, Square::E1, Square::C1)
|
||||
.castle(Castle::QueenSide)
|
||||
.build()
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue