From 8b6323efba32da9e3651ce882c02a6e7a6671f23 Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 23 Oct 2026 19:40:00 +1300 Subject: feat: Optimisation Added benchmarking capabilities --- src/game.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'src/game.rs') diff --git a/src/game.rs b/src/game.rs index fcfc243..11a77fd 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,4 +1,4 @@ -use std::io; +use std::{io, time::Duration}; use ratatui::{ crossterm::{event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind, KeyModifiers, MouseButton, MouseEventKind}, execute}, @@ -111,7 +111,9 @@ impl GameHandler let player = &mut *players[idx(turn)]; let action = match (!over).then(|| player.pick_move(board, turn)).flatten() { - Some(m) => Action::Play(m), + // Input that came in during the search goes first, so a quit or a revert isn't + // lost when both players are computers and nothing ever waits for input + Some(m) => self.pending_action(player, board, turn)?.unwrap_or(Action::Play(m)), None => self.handle_event(event::read()?, player, board, turn, over), }; @@ -129,6 +131,20 @@ impl GameHandler } } + /// Handle the events already waiting, without blocking, until one of them does something. + fn pending_action(&self, player: &mut dyn Player, board: GameBoard, turn: Turn) -> io::Result> + { + while event::poll(Duration::ZERO)? + { + let action = self.handle_event(event::read()?, player, board, turn, false); + if !matches!(action, Action::None) + { + return Ok(Some(action)); + } + } + Ok(None) + } + fn handle_event(&self, ev: Event, player: &mut dyn Player, board: GameBoard, turn: Turn, over: bool) -> Action { match ev -- cgit v1.3.1