diff --git a/src/lib.rs b/src/lib.rs index 829d878..ee621f1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,11 +7,15 @@ use log::{info, warn}; use std::io::*; use crate::uci_command::*; +use chess::{Game, Board, ChessMove}; + pub struct UciEngine { source: Fi, destination: Fo, uciok: bool, id: Id, + initial: Board, + game: Game, } impl UciEngine { @@ -19,7 +23,10 @@ impl UciEngine { UciEngine::{ source, destination, id: Id::new(), - uciok: false} + uciok: false, + initial: Board::default(), + game: Game::new() + } } pub fn init(&mut self){ @@ -32,7 +39,14 @@ impl UciEngine { } 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) { @@ -132,7 +146,7 @@ impl UciEngine { pub fn push(&mut self, command: GuiCommand) { let command_str = command.to_string(); 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}"), Err(reason) => warn!("Unable to send command {command_str}: {reason}"), } diff --git a/src/uci_command.rs b/src/uci_command.rs index b9e0d78..4c7e20c 100644 --- a/src/uci_command.rs +++ b/src/uci_command.rs @@ -2,7 +2,7 @@ use itertools::join; use std::time::Duration; use std::fmt; -use chess::ChessMove; +use chess::{ChessMove, Board, Action}; mod uci_command{} @@ -30,9 +30,10 @@ impl Id { self.author.clone() } } +#[derive(Debug)] pub struct Info {} +#[derive(Debug)] pub struct Opt{} -pub struct Position{} pub enum EngineCommand { Id{id: Id}, @@ -45,6 +46,8 @@ pub enum EngineCommand { Opt{options: Vec}, } + +#[derive(Debug)] pub enum GuiCommand { Uci, Debug{mode: bool}, @@ -52,7 +55,7 @@ pub enum GuiCommand { SetOption{option: Opt}, Register, // unimplemented UciNewGame, - Position{position: Option, moves: Vec}, + Position{position: Option, moves: Vec}, Go, SearchMoves{moves: Vec}, Ponder, @@ -101,7 +104,8 @@ impl fmt::Display for GuiCommand { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result{ match self { GuiCommand::Uci => write!(f, "uci\n"), - _ => unimplemented!(), + GuiCommand::UciNewGame => write!(f, "ucinewgame\n"), + a => unimplemented!("{:?}", a), } }