diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/board/mod.rs | 18 | ||||
-rw-r--r-- | src/main.rs | 22 | ||||
-rw-r--r-- | src/square.rs | 12 |
3 files changed, 37 insertions, 15 deletions
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<Move, String> { 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 diff --git a/src/main.rs b/src/main.rs index 3e8e461..cc087b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,33 @@ use std::{f32::INFINITY, time::{Duration, Instant}}; -use chessnost::board::Board; +use chessnost::board::{Board, Color}; fn main() { - let mut board = Board::new(); + let fen = String::from("5k2/p2Q4/2p5/4q3/8/2P2Pp1/PP4P1/1K3N2 w - - 1 29"); + let mut board = Board::from_FEN(fen); + + board.print(); loop { + let mov = match board.read_move() { + Ok(m) => m, + Err(e) => { + println!("Error: {}", e); + continue; + } + }; + print!("{:?}", mov); + board.make_move(mov); + board.print(); + let start = Instant::now(); - let (score, pv) = board.iterative_deepening(Duration::from_secs(4)); + let (score, pv) = board.iterative_deepening(10, Duration::from_secs(10)); println!("Finished in {:?}: score={:?}", start.elapsed(), score); let mov = pv[0]; println!("{:?}", mov); board.make_move(mov); board.print(); - println!("Score for {:?} is now: {} (material advantage={})", board.color(), board.quiscence(-INFINITY, INFINITY), board.material_advantage()); + println!("Score for {:?} is now: {}", board.color(), board.quiscence(-INFINITY, INFINITY)); } } diff --git a/src/square.rs b/src/square.rs index f26ed4d..e518a92 100644 --- a/src/square.rs +++ b/src/square.rs @@ -52,7 +52,7 @@ impl Square { Self::from(*self as u8 + 1) } - pub fn from_notation(chars: &mut Chars) -> Self { + pub fn from_notation(chars: &mut Chars) -> Result<Self, String> { let file = match chars.next() { Some(ch) => { match ch { @@ -64,22 +64,22 @@ impl Square { 'f' => 5, 'g' => 6, 'h' => 7, - _ => panic!("Incorrect file!") + _ => return Err(String::from("Incorrect file!")) } }, - None => panic!("Missing file"), + None => return Err(String::from("Missing file")) }; let rank = match chars.next() { Some(ch) => { match ch.to_digit(10) { Some(digit) => digit - 1, - None => panic!("Incorrect rank") + None => return Err(String::from("Incorrect rank")) } } - None => panic!("Missing rank") + None => return Err(String::from("Missing rank")) }; - Self::from_coords(rank as u8, file) + Ok(Self::from_coords(rank as u8, file)) } } |