2023-12-31 16:33:47 -08:00
|
|
|
// Eryn Wells <eryn@erynwells.me>
|
|
|
|
|
|
|
|
//! Declares the KingMoveGenerator type. This struct is responsible for
|
|
|
|
//! generating the possible moves for the king in the given position.
|
|
|
|
|
2024-01-06 17:01:16 -08:00
|
|
|
use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet};
|
|
|
|
use crate::{
|
|
|
|
piece::{Color, Piece, PlacedPiece},
|
2024-01-08 14:41:54 -08:00
|
|
|
position::BoardSide,
|
2024-01-17 08:43:48 -08:00
|
|
|
BitBoard, Move, MoveBuilder, Position,
|
2024-01-06 17:01:16 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
move_generator_declaration!(KingMoveGenerator, struct);
|
|
|
|
move_generator_declaration!(KingMoveGenerator, new);
|
|
|
|
move_generator_declaration!(KingMoveGenerator, getters);
|
2023-12-31 16:33:47 -08:00
|
|
|
|
|
|
|
impl<'a> KingMoveGenerator<'a> {
|
2024-01-06 17:01:16 -08:00
|
|
|
#[allow(unused_variables)]
|
2024-01-08 14:41:54 -08:00
|
|
|
fn king_side_castle(position: &Position, color: Color) -> Option<Move> {
|
2024-01-11 08:37:22 -08:00
|
|
|
if !position.player_has_right_to_castle(color, BoardSide::King) {
|
2024-01-08 14:41:54 -08:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2024-01-06 17:01:16 -08:00
|
|
|
// TODO: Implement king side castle.
|
|
|
|
None
|
2023-12-31 16:33:47 -08:00
|
|
|
}
|
|
|
|
|
2024-01-06 17:01:16 -08:00
|
|
|
#[allow(unused_variables)]
|
2024-01-08 14:41:54 -08:00
|
|
|
fn queen_side_castle(position: &Position, color: Color) -> Option<Move> {
|
2024-01-11 08:37:22 -08:00
|
|
|
if !position.player_has_right_to_castle(color, BoardSide::Queen) {
|
2024-01-08 14:41:54 -08:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2024-01-06 17:01:16 -08:00
|
|
|
// TODO: Implement queen side castle.
|
|
|
|
None
|
2023-12-31 16:33:47 -08:00
|
|
|
}
|
2024-01-06 17:01:16 -08:00
|
|
|
}
|
2023-12-31 16:33:47 -08:00
|
|
|
|
2024-01-06 17:01:16 -08:00
|
|
|
impl<'pos> MoveGeneratorInternal for KingMoveGenerator<'pos> {
|
|
|
|
fn piece(color: Color) -> Piece {
|
|
|
|
Piece::king(color)
|
2023-12-31 16:33:47 -08:00
|
|
|
}
|
|
|
|
|
2024-01-06 17:01:16 -08:00
|
|
|
fn move_set_for_piece(position: &Position, placed_piece: PlacedPiece) -> MoveSet {
|
|
|
|
let piece = placed_piece.piece();
|
|
|
|
let color = piece.color();
|
|
|
|
let square = placed_piece.square();
|
2023-12-31 16:33:47 -08:00
|
|
|
|
2024-01-06 17:01:16 -08:00
|
|
|
let empty_squares = position.empty_squares();
|
|
|
|
let opposing_pieces = position.bitboard_for_color(color.other());
|
2023-12-31 16:33:47 -08:00
|
|
|
|
2024-01-06 17:01:16 -08:00
|
|
|
let all_moves = BitBoard::king_moves(square);
|
|
|
|
let quiet_moves_bb = all_moves & empty_squares;
|
|
|
|
let capture_moves_bb = all_moves & opposing_pieces;
|
2023-12-31 16:33:47 -08:00
|
|
|
|
|
|
|
// TODO: Handle checks. Prevent moving a king to a square attacked by a
|
|
|
|
// piece of the opposite color.
|
|
|
|
|
2024-01-17 08:43:48 -08:00
|
|
|
let map_to_move = |sq| MoveBuilder::new(piece, square, sq).build();
|
2023-12-31 16:33:47 -08:00
|
|
|
|
2024-01-08 14:41:54 -08:00
|
|
|
let king_side_castle = Self::king_side_castle(position, color);
|
|
|
|
let queen_side_castle = Self::queen_side_castle(position, color);
|
2024-01-06 17:01:16 -08:00
|
|
|
let quiet_moves = quiet_moves_bb
|
2023-12-31 16:33:47 -08:00
|
|
|
.occupied_squares()
|
2024-01-06 17:01:16 -08:00
|
|
|
.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)
|
|
|
|
.quiet_moves(quiet_moves_bb, quiet_moves)
|
|
|
|
.capture_moves(capture_moves_bb, capture_moves)
|
2023-12-31 16:33:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2024-01-17 08:43:48 -08:00
|
|
|
use crate::{piece, piece::Piece, Position, Square};
|
2023-12-31 16:33:47 -08:00
|
|
|
use std::collections::HashSet;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn one_king() {
|
|
|
|
let mut pos = Position::empty();
|
2024-01-06 16:40:21 -08:00
|
|
|
pos.place_piece(Piece::king(Color::White), Square::E4)
|
2023-12-31 16:33:47 -08:00
|
|
|
.expect("Failed to place king on e4");
|
|
|
|
|
|
|
|
let generator = KingMoveGenerator::new(&pos, Color::White);
|
|
|
|
|
|
|
|
let bb = generator.bitboard();
|
|
|
|
assert_eq!(
|
|
|
|
bb,
|
|
|
|
BitBoard::new(
|
|
|
|
0b00000000_00000000_00000000_00111000_00101000_00111000_00000000_00000000
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
let expected_moves = [
|
2024-01-17 08:43:48 -08:00
|
|
|
MoveBuilder::new(piece!(White King), Square::E4, Square::D5).build(),
|
|
|
|
MoveBuilder::new(piece!(White King), Square::E4, Square::E5).build(),
|
|
|
|
MoveBuilder::new(piece!(White King), Square::E4, Square::F5).build(),
|
|
|
|
MoveBuilder::new(piece!(White King), Square::E4, Square::F4).build(),
|
|
|
|
MoveBuilder::new(piece!(White King), Square::E4, Square::F3).build(),
|
|
|
|
MoveBuilder::new(piece!(White King), Square::E4, Square::E3).build(),
|
|
|
|
MoveBuilder::new(piece!(White King), Square::E4, Square::D3).build(),
|
|
|
|
MoveBuilder::new(piece!(White King), Square::E4, Square::D4).build(),
|
2023-12-31 16:33:47 -08:00
|
|
|
];
|
|
|
|
|
2024-01-01 09:33:33 -08:00
|
|
|
let mut generated_moves: HashSet<Move> = generator.iter().cloned().collect();
|
2023-12-31 16:33:47 -08:00
|
|
|
|
|
|
|
for ex_move in expected_moves {
|
|
|
|
assert!(
|
|
|
|
generated_moves.remove(&ex_move),
|
|
|
|
"{:#?} was not generated",
|
|
|
|
&ex_move
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
generated_moves.is_empty(),
|
|
|
|
"Moves unexpectedly present: {:#?}",
|
|
|
|
generated_moves
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn one_king_corner() {
|
|
|
|
let mut pos = Position::empty();
|
2024-01-06 16:40:21 -08:00
|
|
|
pos.place_piece(Piece::king(Color::White), Square::A1)
|
2023-12-31 16:33:47 -08:00
|
|
|
.expect("Failed to place king on a1");
|
|
|
|
|
|
|
|
let generator = KingMoveGenerator::new(&pos, Color::White);
|
|
|
|
|
|
|
|
let bb = generator.bitboard();
|
|
|
|
assert_eq!(
|
|
|
|
bb,
|
|
|
|
BitBoard::new(
|
|
|
|
0b00000000_00000000_00000000_00000000_00000000_00000000_00000011_00000010
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
let expected_moves = [
|
2024-01-17 08:43:48 -08:00
|
|
|
MoveBuilder::new(piece!(White King), Square::A1, Square::A2).build(),
|
|
|
|
MoveBuilder::new(piece!(White King), Square::A1, Square::B1).build(),
|
|
|
|
MoveBuilder::new(piece!(White King), Square::A1, Square::B2).build(),
|
2023-12-31 16:33:47 -08:00
|
|
|
];
|
|
|
|
|
2024-01-01 09:33:33 -08:00
|
|
|
let mut generated_moves: HashSet<Move> = generator.iter().cloned().collect();
|
2023-12-31 16:33:47 -08:00
|
|
|
|
|
|
|
for ex_move in expected_moves {
|
|
|
|
assert!(
|
|
|
|
generated_moves.remove(&ex_move),
|
|
|
|
"{:#?} was not generated",
|
|
|
|
&ex_move
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
generated_moves.is_empty(),
|
|
|
|
"Moves unexpectedly present: {:#?}",
|
|
|
|
generated_moves
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|