refactor: simplify move gen public interface
This commit is contained in:
parent
60d084886f
commit
9b447ca039
@ -14,7 +14,7 @@ Copyright © 2024 dogeystamp <dogeystamp@disroot.org>
|
||||
//! 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;
|
||||
|
@ -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:?}")
|
||||
}
|
||||
|
@ -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<Item = Move>;
|
||||
}
|
||||
|
||||
pub trait MoveGen {
|
||||
fn gen_moves(&mut self, gen_type: MoveGenType) -> impl IntoIterator<Item = Move>;
|
||||
/// Legal move generation.
|
||||
fn gen_moves(&mut self) -> impl IntoIterator<Item = Move>;
|
||||
}
|
||||
|
||||
impl<T: MoveGenInternal> MoveGen for T {
|
||||
fn gen_moves(&mut self) -> impl IntoIterator<Item = Move> {
|
||||
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<Item = Move> {
|
||||
impl MoveGenInternal for Board {
|
||||
fn gen_moves_general(&mut self, gen_type: MoveGenType) -> impl IntoIterator<Item = Move> {
|
||||
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<Move> = pos.gen_moves(MoveGenType::Legal).into_iter().collect();
|
||||
let moves: Vec<Move> = 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<Move> = board.gen_moves(MoveGenType::Pseudo).into_iter().collect();
|
||||
let mut moves: Vec<Move> = 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<Move> = board.gen_moves(MoveGenType::Legal).into_iter().collect();
|
||||
let mut moves: Vec<Move> = board.gen_moves().into_iter().collect();
|
||||
moves.sort_unstable();
|
||||
let moves = moves;
|
||||
|
||||
|
@ -14,7 +14,7 @@ Copyright © 2024 dogeystamp <dogeystamp@disroot.org>
|
||||
//! 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<EvalInt>, 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<EvalInt>, beta: Option<
|
||||
/// Find the best move for a position (internal interface).
|
||||
fn search(board: &mut Board) -> Option<Move> {
|
||||
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;
|
||||
|
Loading…
Reference in New Issue
Block a user