diff --git a/src/search/iterative_deepening.rs b/src/search/iterative_deepening.rs index c8affb8..78601c5 100644 --- a/src/search/iterative_deepening.rs +++ b/src/search/iterative_deepening.rs @@ -15,6 +15,7 @@ pub fn iterative_deepening( time: &TimeInfo, ) -> anyhow::Result> { let mut best_move = None; + let mut best_score = MIN_SCORE; for depth in 1..=max_depth { if time.exceed_soft_limit() { @@ -26,7 +27,45 @@ pub fn iterative_deepening( } let mut nodes = 0; - let score = negamax::negamax(game, MIN_SCORE, MAX_SCORE, depth, 0, time, &mut nodes, true); + + let mut score; + + if depth < 4 { + score = negamax::negamax(game, MIN_SCORE, MAX_SCORE, depth, 0, time, &mut nodes, true); + } else { + let mut window_size = 20; + let mut alpha = (best_score - window_size).max(MIN_SCORE); + let mut beta = (best_score + window_size).min(MAX_SCORE); + let mut break_from_aw = true; + + loop { + score = negamax::negamax(game, alpha, beta, depth, 0, time, &mut nodes, true); + + let score = match score { + Ok(score) => score, + Err(ref e) => { + write_response(&mut io::stdout(), &Response::Info(format!("{e}")))?; + break_from_aw = false; + break; + } + }; + + if score >= beta { + beta = MAX_SCORE.min(beta.saturating_add(window_size)); + window_size = window_size.saturating_mul(2); + } else if score <= alpha { + alpha = MIN_SCORE.max(alpha.saturating_sub(window_size)); + window_size = window_size.saturating_mul(2); + } else { + best_score = score; + break; + } + } + + if !break_from_aw { + break; + } + } if let Err(e) = score { write_response(&mut io::stdout(), &Response::Info(format!("{e}")))?;