[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

@ -2,15 +2,16 @@
use crate::{r#move::Castle, Move, MoveBuilder};
use chessfriend_bitboard::BitBoard;
use chessfriend_core::{Color, Piece, PlacedPiece, Shape};
use chessfriend_core::{Color, Piece, PlacedPiece, Shape, Square};
/// A set of bitboards defining the moves for a single piece on the board.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
struct BitBoardSet {
quiet: BitBoard,
captures: BitBoard,
}
/// A set of moves for a piece on the board.
/// A set of moves for a single piece on the board.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct MoveSet {
piece: PlacedPiece,
@ -27,6 +28,17 @@ impl MoveSet {
}
}
pub(crate) fn can_move_to_square(&self, to: Square) -> bool {
self.bitboard().is_set(to)
}
pub(crate) fn can_castle(&self, castle: Castle) -> bool {
match castle {
Castle::KingSide => (self.special & 0b1) != 0,
Castle::QueenSide => (self.special & 0b10) != 0,
}
}
pub(super) fn quiet_moves(mut self, bitboard: BitBoard) -> MoveSet {
self.bitboards.quiet = bitboard;
self
@ -52,7 +64,7 @@ impl MoveSet {
self.bitboards.captures | self.bitboards.quiet
}
pub(super) fn moves(&self) -> impl Iterator<Item = Move> + '_ {
pub(crate) fn moves(&self) -> impl Iterator<Item = Move> + '_ {
let piece = self.piece.piece();
let from_square = self.piece.square();

View file

@ -3,7 +3,7 @@
use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet};
use crate::Position;
use chessfriend_bitboard::BitBoard;
use chessfriend_core::{Piece, PlacedPiece, Rank, Shape, Square};
use chessfriend_core::{Color, PlacedPiece, Rank, Shape, Square};
#[derive(Debug)]
struct MoveIterator(usize, usize);
@ -32,18 +32,31 @@ impl MoveGeneratorInternal for PawnMoveGenerator {
impl PawnMoveGenerator {
fn pushes(position: &Position, piece: PlacedPiece) -> BitBoard {
let color = piece.color();
let square = piece.square();
let bitboard: BitBoard = square.into();
let starting_rank = Rank::PAWN_STARTING_RANKS[piece.color() as usize];
let starting_rank = Rank::PAWN_STARTING_RANKS[color as usize];
let empty_squares = position.empty_squares();
let mut moves = bitboard.shift_north_one() & empty_squares;
if !(bitboard & BitBoard::rank(starting_rank.as_index())).is_empty() {
moves |= moves.shift_north_one() & empty_squares;
}
moves
match color {
Color::White => {
let mut moves = bitboard.shift_north_one() & empty_squares;
if !(bitboard & BitBoard::rank(starting_rank.as_index())).is_empty() {
moves |= moves.shift_north_one() & empty_squares;
}
moves
}
Color::Black => {
let mut moves = bitboard.shift_south_one() & empty_squares;
if !(bitboard & BitBoard::rank(starting_rank.as_index())).is_empty() {
moves |= moves.shift_south_one() & empty_squares;
}
moves
}
}
}
fn attacks(position: &Position, piece: PlacedPiece) -> BitBoard {
@ -63,7 +76,7 @@ impl PawnMoveGenerator {
mod tests {
use super::*;
use crate::{assert_move_list, position::DiagramFormatter, test_position, Move, MoveBuilder};
use chessfriend_core::{piece, Color, Square};
use chessfriend_core::{piece, Color, Piece, Square};
use std::collections::HashSet;
#[test]