Spaces:
Runtime error
Runtime error
| // timeman.rs | |
| use std::time::{Instant, Duration}; | |
| pub struct TimeManager { | |
| start_time: Instant, | |
| time_limit: Duration, | |
| nodes_searched: u64, | |
| } | |
| impl TimeManager { | |
| pub fn new(wtime: u64, btime: u64, winc: u64, binc: u64, side: crate::types::Color) -> Self { | |
| let (time, inc) = if side == crate::types::Color::White { | |
| (wtime, winc) | |
| } else { | |
| (btime, binc) | |
| }; | |
| // Basic time allocation: 1/40th of remaining time + half increment | |
| let allocated = (time / 40) + (inc / 2); | |
| Self { | |
| start_time: Instant::now(), | |
| time_limit: Duration::from_millis(allocated), | |
| nodes_searched: 0, | |
| } | |
| } | |
| pub fn tick(&mut self) -> bool { | |
| self.nodes_searched += 1; | |
| if self.nodes_searched & 2047 == 0 { | |
| if self.start_time.elapsed() >= self.time_limit { | |
| return false; // Out of time | |
| } | |
| } | |
| true | |
| } | |
| } | |