[explorer, moves, position] Implement a moves command in explorer

The moves command writes all possible moves to the terminal.

Move the previous implementation of the moves command, which marked squares that
a piece could move to, to a 'movement' command.
This commit is contained in:
Eryn Wells 2025-05-28 16:25:55 -07:00
parent 43abbe3fe2
commit 942d9fe47b
4 changed files with 124 additions and 16 deletions

View file

@ -11,4 +11,5 @@ mod macros;
mod testing;
pub use chessfriend_board::{fen, PlacePieceError, PlacePieceStrategy};
pub use chessfriend_moves::GeneratedMove;
pub use position::{Position, ValidateMove};

View file

@ -4,6 +4,13 @@ mod captures;
mod make_move;
mod unmake_move;
use chessfriend_moves::{
generators::{
AllPiecesMoveGenerator, BishopMoveGenerator, KingMoveGenerator, KnightMoveGenerator,
PawnMoveGenerator, QueenMoveGenerator, RookMoveGenerator,
},
GeneratedMove,
};
pub use make_move::{MakeMoveError, ValidateMove};
use crate::move_record::MoveRecord;
@ -12,7 +19,7 @@ use chessfriend_bitboard::BitBoard;
use chessfriend_board::{
display::DiagramFormatter, fen::ToFenStr, Board, PlacePieceError, PlacePieceStrategy,
};
use chessfriend_core::{Color, Piece, Square};
use chessfriend_core::{Color, Piece, Shape, Square};
use std::{cell::OnceCell, fmt};
#[must_use]
@ -79,6 +86,33 @@ impl Position {
}
}
impl Position {
pub fn all_moves(&self, color: Option<Color>) -> AllPiecesMoveGenerator {
AllPiecesMoveGenerator::new(&self.board, color)
}
#[must_use]
pub fn moves_for_piece(
&self,
square: Square,
) -> Option<Box<dyn Iterator<Item = GeneratedMove>>> {
self.get_piece(square)
.map(|piece| Self::generator(&self.board, piece))
}
#[must_use]
fn generator(board: &Board, piece: Piece) -> Box<dyn Iterator<Item = GeneratedMove>> {
match piece.shape {
Shape::Pawn => Box::new(PawnMoveGenerator::new(board, Some(piece.color))),
Shape::Knight => Box::new(KnightMoveGenerator::new(board, Some(piece.color))),
Shape::Bishop => Box::new(BishopMoveGenerator::new(board, Some(piece.color))),
Shape::Rook => Box::new(RookMoveGenerator::new(board, Some(piece.color))),
Shape::Queen => Box::new(QueenMoveGenerator::new(board, Some(piece.color))),
Shape::King => Box::new(KingMoveGenerator::new(board, Some(piece.color))),
}
}
}
impl Position {
pub fn active_sight(&self) -> BitBoard {
self.board.active_sight()