81 lines
2.6 KiB
Rust
81 lines
2.6 KiB
Rust
// Eryn Wells <eryn@erynwells.me>
|
|
|
|
use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet};
|
|
use crate::{MoveBuilder, Position};
|
|
use chessfriend_bitboard::BitBoard;
|
|
use chessfriend_core::{Color, Piece, PlacedPiece};
|
|
|
|
move_generator_declaration!(KnightMoveGenerator);
|
|
|
|
impl MoveGeneratorInternal for KnightMoveGenerator {
|
|
fn piece(color: Color) -> Piece {
|
|
Piece::knight(color)
|
|
}
|
|
|
|
fn move_set_for_piece(position: &Position, placed_piece: PlacedPiece) -> MoveSet {
|
|
let opposing_pieces = position.bitboard_for_color(placed_piece.piece().color().other());
|
|
let empty_squares = position.empty_squares();
|
|
let knight_moves = BitBoard::knight_moves(placed_piece.square());
|
|
|
|
let quiet_moves = knight_moves & empty_squares;
|
|
let capture_moves = knight_moves & opposing_pieces;
|
|
|
|
MoveSet::new(placed_piece)
|
|
.quiet_moves(quiet_moves)
|
|
.capture_moves(capture_moves)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::{position, Move};
|
|
use chessfriend_core::{piece, Square};
|
|
use std::collections::HashSet;
|
|
|
|
#[test]
|
|
fn one_knight() {
|
|
let pos = position![
|
|
White Knight on E4,
|
|
];
|
|
|
|
let generator = KnightMoveGenerator::new(&pos, Color::White);
|
|
|
|
/*
|
|
let bb = generator.bitboard();
|
|
assert_eq!(
|
|
bb,
|
|
BitBoard::new(
|
|
0b00000000_00000000_00000000_00111000_00101000_00111000_00000000_00000000
|
|
)
|
|
);
|
|
*/
|
|
|
|
let expected_moves = [
|
|
MoveBuilder::new(piece!(White Knight), Square::E4, Square::C3).build(),
|
|
MoveBuilder::new(piece!(White Knight), Square::E4, Square::D2).build(),
|
|
MoveBuilder::new(piece!(White Knight), Square::E4, Square::F2).build(),
|
|
MoveBuilder::new(piece!(White Knight), Square::E4, Square::G3).build(),
|
|
MoveBuilder::new(piece!(White Knight), Square::E4, Square::C5).build(),
|
|
MoveBuilder::new(piece!(White Knight), Square::E4, Square::D6).build(),
|
|
MoveBuilder::new(piece!(White Knight), Square::E4, Square::G5).build(),
|
|
MoveBuilder::new(piece!(White Knight), Square::E4, Square::F6).build(),
|
|
];
|
|
|
|
let mut generated_moves: HashSet<Move> = generator.iter().collect();
|
|
|
|
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
|
|
);
|
|
}
|
|
}
|