From 9b447ca0392ca46b3c91678abfbce376185220de Mon Sep 17 00:00:00 2001 From: dogeystamp Date: Fri, 1 Nov 2024 21:58:38 -0400 Subject: [PATCH] refactor: simplify move gen public interface --- src/bin/engine.rs | 2 +- src/bin/movegen_tool.rs | 4 ++-- src/movegen.rs | 26 +++++++++++++++++++------- src/search.rs | 6 +++--- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/bin/engine.rs b/src/bin/engine.rs index cdd203c..1316d32 100644 --- a/src/bin/engine.rs +++ b/src/bin/engine.rs @@ -14,7 +14,7 @@ Copyright © 2024 dogeystamp //! Main UCI engine binary. use chess_inator::fen::FromFen; -use chess_inator::movegen::{FromUCIAlgebraic, Move, MoveGen, MoveGenType, ToUCIAlgebraic}; +use chess_inator::movegen::{FromUCIAlgebraic, Move, ToUCIAlgebraic}; use chess_inator::search::best_move; use chess_inator::Board; use std::io; diff --git a/src/bin/movegen_tool.rs b/src/bin/movegen_tool.rs index a1ba3ef..a2cca2b 100644 --- a/src/bin/movegen_tool.rs +++ b/src/bin/movegen_tool.rs @@ -1,14 +1,14 @@ //! Generates moves from the FEN in the argv. use chess_inator::fen::FromFen; -use chess_inator::movegen::{MoveGen, MoveGenType}; +use chess_inator::movegen::MoveGen; use chess_inator::Board; use std::env; fn main() { let fen = env::args().nth(1).unwrap(); let mut board = Board::from_fen(&fen).unwrap(); - let mvs = board.gen_moves(MoveGenType::Legal); + let mvs = board.gen_moves(); for mv in mvs.into_iter() { println!("{mv:?}") } diff --git a/src/movegen.rs b/src/movegen.rs index a96febc..77234a1 100644 --- a/src/movegen.rs +++ b/src/movegen.rs @@ -444,15 +444,27 @@ impl ToUCIAlgebraic for Move { } #[derive(Debug, Clone, Copy)] -pub enum MoveGenType { +enum MoveGenType { /// Legal move generation. Legal, /// Allow capturing friendly pieces, moving into check, but not castling through check. Pseudo, } +/// Internal, slightly more general movegen interface +trait MoveGenInternal { + fn gen_moves_general(&mut self, gen_type: MoveGenType) -> impl IntoIterator; +} + pub trait MoveGen { - fn gen_moves(&mut self, gen_type: MoveGenType) -> impl IntoIterator; + /// Legal move generation. + fn gen_moves(&mut self) -> impl IntoIterator; +} + +impl MoveGen for T { + fn gen_moves(&mut self) -> impl IntoIterator { + self.gen_moves_general(MoveGenType::Legal) + } } pub const DIRS_STRAIGHT: [(isize, isize); 4] = [(0, 1), (1, 0), (-1, 0), (0, -1)]; @@ -561,8 +573,8 @@ fn is_legal(board: &mut Board, mv: Move) -> bool { true } -impl MoveGen for Board { - fn gen_moves(&mut self, gen_type: MoveGenType) -> impl IntoIterator { +impl MoveGenInternal for Board { + fn gen_moves_general(&mut self, gen_type: MoveGenType) -> impl IntoIterator { let mut ret = Vec::new(); let pl = self[self.turn]; macro_rules! squares { @@ -759,7 +771,7 @@ pub fn perft(depth: usize, pos: &mut Board) -> usize { let mut ans = 0; - let moves: Vec = pos.gen_moves(MoveGenType::Legal).into_iter().collect(); + let moves: Vec = pos.gen_moves().into_iter().collect(); for mv in moves { let anti_move = mv.make(pos); ans += perft(depth - 1, pos); @@ -1081,7 +1093,7 @@ mod tests { let all_cases = [augmented_test_cases, test_cases].concat(); for (mut board, expected_moves) in all_cases { - let mut moves: Vec = board.gen_moves(MoveGenType::Pseudo).into_iter().collect(); + let mut moves: Vec = board.gen_moves_general(MoveGenType::Pseudo).into_iter().collect(); moves.sort_unstable(); let moves = moves; @@ -1236,7 +1248,7 @@ mod tests { expected_moves.sort_unstable(); let expected_moves = expected_moves; - let mut moves: Vec = board.gen_moves(MoveGenType::Legal).into_iter().collect(); + let mut moves: Vec = board.gen_moves().into_iter().collect(); moves.sort_unstable(); let moves = moves; diff --git a/src/search.rs b/src/search.rs index a7f422f..0168f57 100644 --- a/src/search.rs +++ b/src/search.rs @@ -14,7 +14,7 @@ Copyright © 2024 dogeystamp //! Game-tree search. use crate::eval::{Eval, EvalInt}; -use crate::movegen::{Move, MoveGen, MoveGenType, ToUCIAlgebraic}; +use crate::movegen::{Move, MoveGen, ToUCIAlgebraic}; use crate::Board; use std::cmp::max; @@ -56,7 +56,7 @@ fn minmax(board: &mut Board, depth: usize, alpha: Option, beta: Option< } } - let mvs: Vec<_> = board.gen_moves(MoveGenType::Legal).into_iter().collect(); + let mvs: Vec<_> = board.gen_moves().into_iter().collect(); let mut abs_best = EVAL_WORST; @@ -92,7 +92,7 @@ fn minmax(board: &mut Board, depth: usize, alpha: Option, beta: Option< /// Find the best move for a position (internal interface). fn search(board: &mut Board) -> Option { const DEPTH: usize = 4; - let mvs: Vec<_> = board.gen_moves(MoveGenType::Legal).into_iter().collect(); + let mvs: Vec<_> = board.gen_moves().into_iter().collect(); // absolute eval value let mut best_eval = EVAL_WORST;