This commit is contained in:
Baptiste Fouques 2023-01-11 18:20:30 +01:00
parent c125ce3dfc
commit 44c0a23252
2 changed files with 25 additions and 7 deletions

View File

@ -7,11 +7,15 @@ use log::{info, warn};
use std::io::*; use std::io::*;
use crate::uci_command::*; use crate::uci_command::*;
use chess::{Game, Board, ChessMove};
pub struct UciEngine<Fi: BufRead, Fo: Write> { pub struct UciEngine<Fi: BufRead, Fo: Write> {
source: Fi, source: Fi,
destination: Fo, destination: Fo,
uciok: bool, uciok: bool,
id: Id, id: Id,
initial: Board,
game: Game,
} }
impl<Fi: BufRead, Fo: Write> UciEngine<Fi, Fo> { impl<Fi: BufRead, Fo: Write> UciEngine<Fi, Fo> {
@ -19,7 +23,10 @@ impl<Fi: BufRead, Fo: Write> UciEngine<Fi, Fo> {
UciEngine::<Fi, Fo>{ UciEngine::<Fi, Fo>{
source, destination, source, destination,
id: Id::new(), id: Id::new(),
uciok: false} uciok: false,
initial: Board::default(),
game: Game::new()
}
} }
pub fn init(&mut self){ pub fn init(&mut self){
@ -32,7 +39,14 @@ impl<Fi: BufRead, Fo: Write> UciEngine<Fi, Fo> {
} }
pub fn new_game(&mut self) { pub fn new_game(&mut self) {
unimplemented!(); self.initial = Board::default();
self.game = Game::new_with_board(self.initial);
self.push(GuiCommand::UciNewGame);
}
pub fn make_move(&mut self, chess_move: ChessMove) {
self.game.make_move(chess_move);
self.push(GuiCommand::Position { position: Some(self.initial), moves: self.game.actions().to_vec() })
} }
pub fn terminate(&mut self, reason: &str) { pub fn terminate(&mut self, reason: &str) {
@ -132,7 +146,7 @@ impl<Fi: BufRead, Fo: Write> UciEngine<Fi, Fo> {
pub fn push(&mut self, command: GuiCommand) { pub fn push(&mut self, command: GuiCommand) {
let command_str = command.to_string(); let command_str = command.to_string();
match self.destination.write(&command_str.as_bytes()){ match self.destination.write(&command_str.as_bytes()){
Ok(n) if n == command_str.len() => info!(" gui: {command_str} "), Ok(n) if n == command_str.len() => info!(" gui: {command_str} "),
Ok(n) => warn!("⚠ gui: {command_str} truncated at {n}"), Ok(n) => warn!("⚠ gui: {command_str} truncated at {n}"),
Err(reason) => warn!("Unable to send command {command_str}: {reason}"), Err(reason) => warn!("Unable to send command {command_str}: {reason}"),
} }

View File

@ -2,7 +2,7 @@ use itertools::join;
use std::time::Duration; use std::time::Duration;
use std::fmt; use std::fmt;
use chess::ChessMove; use chess::{ChessMove, Board, Action};
mod uci_command{} mod uci_command{}
@ -30,9 +30,10 @@ impl Id {
self.author.clone() self.author.clone()
} }
} }
#[derive(Debug)]
pub struct Info {} pub struct Info {}
#[derive(Debug)]
pub struct Opt{} pub struct Opt{}
pub struct Position{}
pub enum EngineCommand { pub enum EngineCommand {
Id{id: Id}, Id{id: Id},
@ -45,6 +46,8 @@ pub enum EngineCommand {
Opt{options: Vec<Opt>}, Opt{options: Vec<Opt>},
} }
#[derive(Debug)]
pub enum GuiCommand { pub enum GuiCommand {
Uci, Uci,
Debug{mode: bool}, Debug{mode: bool},
@ -52,7 +55,7 @@ pub enum GuiCommand {
SetOption{option: Opt}, SetOption{option: Opt},
Register, // unimplemented Register, // unimplemented
UciNewGame, UciNewGame,
Position{position: Option<Position>, moves: Vec<ChessMove>}, Position{position: Option<Board>, moves: Vec<Action>},
Go, Go,
SearchMoves{moves: Vec<ChessMove>}, SearchMoves{moves: Vec<ChessMove>},
Ponder, Ponder,
@ -101,7 +104,8 @@ impl fmt::Display for GuiCommand {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result{
match self { match self {
GuiCommand::Uci => write!(f, "uci\n"), GuiCommand::Uci => write!(f, "uci\n"),
_ => unimplemented!(), GuiCommand::UciNewGame => write!(f, "ucinewgame\n"),
a => unimplemented!("{:?}", a),
} }
} }