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.
|
//! Main UCI engine binary.
|
||||||
|
|
||||||
use chess_inator::fen::FromFen;
|
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::search::best_move;
|
||||||
use chess_inator::Board;
|
use chess_inator::Board;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
//! Generates moves from the FEN in the argv.
|
//! Generates moves from the FEN in the argv.
|
||||||
|
|
||||||
use chess_inator::fen::FromFen;
|
use chess_inator::fen::FromFen;
|
||||||
use chess_inator::movegen::{MoveGen, MoveGenType};
|
use chess_inator::movegen::MoveGen;
|
||||||
use chess_inator::Board;
|
use chess_inator::Board;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let fen = env::args().nth(1).unwrap();
|
let fen = env::args().nth(1).unwrap();
|
||||||
let mut board = Board::from_fen(&fen).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() {
|
for mv in mvs.into_iter() {
|
||||||
println!("{mv:?}")
|
println!("{mv:?}")
|
||||||
}
|
}
|
||||||
|
@ -444,15 +444,27 @@ impl ToUCIAlgebraic for Move {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum MoveGenType {
|
enum MoveGenType {
|
||||||
/// Legal move generation.
|
/// Legal move generation.
|
||||||
Legal,
|
Legal,
|
||||||
/// Allow capturing friendly pieces, moving into check, but not castling through check.
|
/// Allow capturing friendly pieces, moving into check, but not castling through check.
|
||||||
Pseudo,
|
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 {
|
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)];
|
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
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MoveGen for Board {
|
impl MoveGenInternal for Board {
|
||||||
fn gen_moves(&mut self, gen_type: MoveGenType) -> impl IntoIterator<Item = Move> {
|
fn gen_moves_general(&mut self, gen_type: MoveGenType) -> impl IntoIterator<Item = Move> {
|
||||||
let mut ret = Vec::new();
|
let mut ret = Vec::new();
|
||||||
let pl = self[self.turn];
|
let pl = self[self.turn];
|
||||||
macro_rules! squares {
|
macro_rules! squares {
|
||||||
@ -759,7 +771,7 @@ pub fn perft(depth: usize, pos: &mut Board) -> usize {
|
|||||||
|
|
||||||
let mut ans = 0;
|
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 {
|
for mv in moves {
|
||||||
let anti_move = mv.make(pos);
|
let anti_move = mv.make(pos);
|
||||||
ans += perft(depth - 1, pos);
|
ans += perft(depth - 1, pos);
|
||||||
@ -1081,7 +1093,7 @@ mod tests {
|
|||||||
let all_cases = [augmented_test_cases, test_cases].concat();
|
let all_cases = [augmented_test_cases, test_cases].concat();
|
||||||
|
|
||||||
for (mut board, expected_moves) in all_cases {
|
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();
|
moves.sort_unstable();
|
||||||
let moves = moves;
|
let moves = moves;
|
||||||
|
|
||||||
@ -1236,7 +1248,7 @@ mod tests {
|
|||||||
expected_moves.sort_unstable();
|
expected_moves.sort_unstable();
|
||||||
let expected_moves = expected_moves;
|
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();
|
moves.sort_unstable();
|
||||||
let moves = moves;
|
let moves = moves;
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ Copyright © 2024 dogeystamp <dogeystamp@disroot.org>
|
|||||||
//! Game-tree search.
|
//! Game-tree search.
|
||||||
|
|
||||||
use crate::eval::{Eval, EvalInt};
|
use crate::eval::{Eval, EvalInt};
|
||||||
use crate::movegen::{Move, MoveGen, MoveGenType, ToUCIAlgebraic};
|
use crate::movegen::{Move, MoveGen, ToUCIAlgebraic};
|
||||||
use crate::Board;
|
use crate::Board;
|
||||||
use std::cmp::max;
|
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;
|
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).
|
/// Find the best move for a position (internal interface).
|
||||||
fn search(board: &mut Board) -> Option<Move> {
|
fn search(board: &mut Board) -> Option<Move> {
|
||||||
const DEPTH: usize = 4;
|
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
|
// absolute eval value
|
||||||
let mut best_eval = EVAL_WORST;
|
let mut best_eval = EVAL_WORST;
|
||||||
|
Loading…
Reference in New Issue
Block a user