From 5390756aa46b1ae2cf1e274af8f2fdc24ed8009c Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 4 Sep 2023 04:57:02 +0300 Subject: fix: never panic in search, return option instead Will Result type be better? --- src/grossmeister/search.rs | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'src/grossmeister/search.rs') diff --git a/src/grossmeister/search.rs b/src/grossmeister/search.rs index cad2b99..d3dc0b4 100644 --- a/src/grossmeister/search.rs +++ b/src/grossmeister/search.rs @@ -233,7 +233,7 @@ impl Grossmeister { alpha } - pub fn iterative_deepening(&mut self, max_depth: u8) -> (f32, Vec) { + pub fn iterative_deepening(&mut self, max_depth: u8) -> Option<(f32, Vec)> { let mut result = None; let mut depth = 1; let mut alpha = -INFINITY; @@ -310,17 +310,17 @@ impl Grossmeister { println!("info hashfull {}", 1000 * self.transposition_table.iter().filter(|item| item.is_some()).count() / self.transposition_table.len()); - match result { - Some(r) => { + if let Some(r) = result.clone() { + if !r.1.is_empty() { print!("bestmove {}", r.1[0]); - if r.1.len() > 1 { - print!(" ponder {}", r.1[1]) - } - println!(); - r } - None => panic!("No move found by search") + if r.1.len() > 1 { + print!(" ponder {}", r.1[1]) + } + println!(); } + + result } pub fn perft(&mut self, depth: u8, print: bool) -> PerftResult { @@ -436,7 +436,7 @@ mod tests { let fen = String::from("2kr1b1r/pp1npppp/2p1bn2/7q/5B2/2NB1Q1P/PPP1N1P1/2KR3R w - - 0 1"); let board = Board::from_FEN(fen); let mut gm = Grossmeister::new(board); - let (score, pv) = gm.iterative_deepening(8); + let (score, pv) = gm.iterative_deepening(8).unwrap(); assert_eq!(score, SCORE_MATE - 3.0); assert_eq!(pv, vec![ @@ -452,7 +452,7 @@ mod tests { let board = Board::from_FEN(fen); let mut gm = Grossmeister::new(board); - let (_, pv) = gm.iterative_deepening(6); + let (_, pv) = gm.iterative_deepening(6).unwrap(); assert_eq!( pv[0], Move { source: Square::F5, target: Square::H4, kind: MoveKind::Quiet }, @@ -466,7 +466,7 @@ mod tests { let board = Board::from_FEN(fen); let mut gm = Grossmeister::new(board); - let (_, pv) = gm.iterative_deepening(5); + let (_, pv) = gm.iterative_deepening(5).unwrap(); assert_eq!( pv[0], Move { source: Square::C2, target: Square::C3, kind: MoveKind::Quiet }, @@ -481,7 +481,7 @@ mod tests { let mut gm = Grossmeister::new(board); gm.debug = true; - let (score, pv) = gm.iterative_deepening(5); + let (score, pv) = gm.iterative_deepening(5).unwrap(); dbg!(score, pv); assert_eq!(SCORE_MATE - score, 3.0); // Mate in 3 plies } @@ -493,7 +493,7 @@ mod tests { let mut gm = Grossmeister::new(board); gm.debug = true; - let (score, pv) = gm.iterative_deepening(5); + let (score, pv) = gm.iterative_deepening(5).unwrap(); dbg!(score, pv); assert_eq!(score + SCORE_MATE, 4.0); // Mate in 4 plies } @@ -505,7 +505,7 @@ mod tests { let mut gm = Grossmeister::new(board); gm.debug = true; - let (score, pv) = gm.iterative_deepening(5); + let (score, pv) = gm.iterative_deepening(5).unwrap(); dbg!(score, pv); assert_eq!(SCORE_MATE - score, 3.0); // Mate in 3 plies } @@ -517,7 +517,7 @@ mod tests { let mut gm = Grossmeister::new(board); gm.debug = true; - let (score, pv) = gm.iterative_deepening(5); + let (score, pv) = gm.iterative_deepening(5).unwrap(); dbg!(score, pv); assert!(score <= 0.0); } -- cgit v1.2.3