[position] Fix all the unit tests

The pawn move generator only generated pushes for white pawns.
The is_king_in_check returned an inverted flag.
MoveSet needed a couple more validation methods: can_move_to_square and can_castle.
The MakeMoveBuilder also needed a little more move validation using the above methods.
This commit is contained in:
Eryn Wells 2024-02-03 10:04:41 -08:00
parent 63758a2edd
commit 4a601c2b81
6 changed files with 82 additions and 31 deletions

View file

@ -1,6 +1,10 @@
// Eryn Wells <eryn@erynwells.me>
use crate::{position::flags::Flags, r#move::Castle, MakeMoveError, Move, Position};
use crate::{
position::flags::Flags,
r#move::{AlgebraicMoveFormatter, Castle},
MakeMoveError, Move, Position,
};
use chessfriend_bitboard::BitBoard;
use chessfriend_core::{Color, Direction, Piece, PlacedPiece, Shape, Square};
@ -60,9 +64,22 @@ where
let to_square = mv.to_square();
let sight = self.position.sight_of_piece(&piece);
if !sight.is_set(to_square) {
return Err(MakeMoveError::IllegalSquare(to_square));
let moves = self
.position
.moves_for_piece(&piece)
.ok_or(MakeMoveError::NoLegalMoves)?;
match mv.castle() {
Some(castle) => {
if !moves.can_castle(castle) {
return Err(MakeMoveError::IllegalCastle);
}
}
None => {
if !moves.can_move_to_square(to_square) {
return Err(MakeMoveError::IllegalSquare(to_square));
}
}
}
let player = self.position.player_to_move();
@ -324,7 +341,7 @@ mod tests {
assert!(black_pawn_move.is_double_push());
assert!(!black_pawn_move.is_en_passant());
let en_passant_position = Builder::<NoMove>::new(&pos).make(&black_pawn_move)?.build();
let en_passant_position = Builder::new(&pos).make(&black_pawn_move)?.build();
println!("{en_passant_position}");
assert_eq!(

View file

@ -2,8 +2,8 @@
use super::{flags::Flags, piece_sets::PieceBitBoards, Pieces};
use crate::{
check::{self, CheckingPieces},
move_generator::Moves,
check::CheckingPieces,
move_generator::{MoveSet, Moves},
position::DiagramFormatter,
r#move::Castle,
sight::SightExt,
@ -216,6 +216,10 @@ impl Position {
.fold(BitBoard::empty(), |acc, sight| acc | sight)
}
pub(crate) fn moves_for_piece(&self, piece: &PlacedPiece) -> Option<&MoveSet> {
self.moves().moves_for_piece(piece)
}
pub(crate) fn sight_of_piece(&self, piece: &PlacedPiece) -> BitBoard {
piece.sight(&self.pieces, self.en_passant_square)
}
@ -236,7 +240,7 @@ impl Position {
pub(crate) fn is_king_in_check(&self) -> bool {
let danger_squares = self.king_danger(self.color_to_move);
(danger_squares & self.king_bitboard(self.color_to_move)).is_empty()
!(danger_squares & self.king_bitboard(self.color_to_move)).is_empty()
}
fn king_bitboard(&self, player: Color) -> &BitBoard {