From 70315e4652bb49981108a9b920b95ab82a53edb7 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sat, 28 Jan 2023 01:01:15 +0300 Subject: feat: interactively play in main loop --- src/board/mod.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'src/board/mod.rs') diff --git a/src/board/mod.rs b/src/board/mod.rs index ce8c076..a3d1fd9 100644 --- a/src/board/mod.rs +++ b/src/board/mod.rs @@ -148,20 +148,28 @@ impl Board { Self::from_FEN(default_fen) } - pub fn read_move(&self) -> Move { + pub fn read_move(&self) -> Result { let mut s = String::new(); stdin().read_line(&mut s).unwrap(); let chars = &mut s.chars(); - let source = Square::from_notation(chars); - let target = Square::from_notation(chars); + + let source = match Square::from_notation(chars) { + Ok(s) => s, + Err(e) => return Err(e), + }; + let target = match Square::from_notation(chars) { + Ok(s) => s, + Err(e) => return Err(e), + }; let moves = self.generate_pseudolegal_moves(self.color()); let mov = match moves.iter().find(|m| m.source == source && m.target == target) { Some(m) => *m, - None => panic!("Move is not valid"), + None => return Err(String::from("Move is not valid")), }; - mov + + Ok(mov) } /// Color to move at this ply -- cgit v1.2.3