diff options
| author | Dylan <boss@tehbox.org> | 2026-10-23 19:40:00 +1300 |
|---|---|---|
| committer | Dylan <boss@tehbox.org> | 2026-10-23 19:41:07 +1300 |
| commit | 8b6323efba32da9e3651ce882c02a6e7a6671f23 (patch) | |
| tree | 3b01f4d47176379df2aadbef0ffc621a2d9d6186 /src/game.rs | |
| parent | 50028fc708ef0cfea9fd589c90e07b413ecb570f (diff) | |
| download | tigers-and-sheep-8b6323efba32da9e3651ce882c02a6e7a6671f23.tar.gz tigers-and-sheep-8b6323efba32da9e3651ce882c02a6e7a6671f23.zip | |
feat: Optimisation
Added benchmarking capabilities
Diffstat (limited to 'src/game.rs')
| -rw-r--r-- | src/game.rs | 20 |
1 files changed, 18 insertions, 2 deletions
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<Option<Action>> + { + 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 |
