Add minimax and adapt UCI tests
This commit is contained in:
@@ -3,7 +3,7 @@ use std::{
|
||||
str::SplitWhitespace,
|
||||
};
|
||||
|
||||
use crate::{board::game::Game, board::square::Square, movegen::r#move::Move, search::search};
|
||||
use crate::{board::game::Game, movegen::r#move::Move, search};
|
||||
|
||||
#[derive(PartialEq, Eq, Debug)]
|
||||
pub enum Command {
|
||||
@@ -91,7 +91,11 @@ pub fn uci_position(position: &mut SplitWhitespace) -> Result<Game, String> {
|
||||
let state = position.next().ok_or("Expected startpos or fen")?;
|
||||
let mut game = match state {
|
||||
"startpos" => Game::new(),
|
||||
"fen" => Game::new_from_fen(position.next().ok_or("Expected fen string")?)?,
|
||||
"fen" => {
|
||||
let fen_parts: Vec<&str> = position.take_while(|&part| part != "moves").collect();
|
||||
let fen = fen_parts.join(" ");
|
||||
Game::new_from_fen(&fen)?
|
||||
}
|
||||
_ => return Err("Expected startpos or fen".to_string()),
|
||||
};
|
||||
|
||||
@@ -128,9 +132,11 @@ pub fn uci_go(go: &mut SplitWhitespace, game: &mut Game) -> Result<Move, String>
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
search::search(game, params.depth.unwrap_or(MAX_DEPTH));
|
||||
|
||||
Ok(Move::new(Square::B8, Square::C6))
|
||||
Ok(
|
||||
search::minimax::minimax(game, params.depth.unwrap_or(MAX_DEPTH))
|
||||
.0
|
||||
.expect("No move selected"),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn uci_loop<R: BufRead, W: Write>(input: R, mut output: W) -> Result<(), String> {
|
||||
@@ -171,9 +177,9 @@ mod tests {
|
||||
use std::io::Cursor;
|
||||
|
||||
use crate::{
|
||||
board::{fen::from_fen, game::Game, square::Square},
|
||||
board::{fen::from_fen, square::Square},
|
||||
interface::uci::{parse_command, Command},
|
||||
movegen::r#move::Move,
|
||||
movegen::{attack::init_attacks, r#move::Move},
|
||||
};
|
||||
|
||||
use super::uci_go;
|
||||
@@ -204,22 +210,27 @@ mod tests {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
const FEN_MATE_IN_1: &str = "8/8/8/8/8/4q1k1/8/5K2 b - - 0 1";
|
||||
|
||||
#[test]
|
||||
fn test_uci_go() -> Result<(), String> {
|
||||
let mut game = Game::new();
|
||||
init_attacks();
|
||||
let mut game = from_fen(FEN_MATE_IN_1)?;
|
||||
let command_go = "go depth 4";
|
||||
let mut parts = command_go.split_whitespace();
|
||||
let response = uci_go(&mut parts, &mut game)?;
|
||||
|
||||
assert_eq!(Move::new(Square::B8, Square::C6), response);
|
||||
assert_eq!(Move::new(Square::E3, Square::F2), response);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_uci_loop() -> Result<(), String> {
|
||||
init_attacks();
|
||||
let commands = b"uci\n\
|
||||
ucinewgame\n\
|
||||
position startpos moves e2e4 e7e5\n\
|
||||
position fen 8/8/8/8/8/4q1k1/8/5K2 b - - 0 1\n\
|
||||
go\n\
|
||||
quit";
|
||||
let input = Cursor::new(commands);
|
||||
@@ -232,7 +243,7 @@ mod tests {
|
||||
uciok\n\
|
||||
Clear cache\n\
|
||||
Initialized position\n\
|
||||
bestmove b8c6\n";
|
||||
bestmove e3f2\n";
|
||||
let actual_response = String::from_utf8(output).expect("Invalid UTF-8 in output");
|
||||
|
||||
assert_eq!(expected_response, actual_response);
|
||||
|
||||
Reference in New Issue
Block a user