Skip to content

Commit

Permalink
Add checkmate check
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusCDE committed Oct 4, 2020
1 parent dafb693 commit 78c9a1e
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/scene/game_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ pub struct GameScene {
pub back_button_pressed: bool,
/// Do a full screen refresh on next draw
force_full_refresh: Option<SystemTime>,
last_checkmate_check: SystemTime,
}

impl GameScene {
Expand Down Expand Up @@ -221,6 +222,7 @@ impl GameScene {
full_refresh_button_hitbox: None,
back_button_pressed: false,
force_full_refresh: None,
last_checkmate_check: SystemTime::now(),
}
}

Expand Down Expand Up @@ -588,5 +590,28 @@ impl Scene for GameScene {
canvas.update_full();
self.force_full_refresh = None;
}

// Check periodicially for checkmate.
// The function pleco::Board::checkmate() is supposed to be compuationally
// expensive. I measured 2-3us at the beginning on the rM1 but who knows.
// This more a compromize between development speed and correctness.
let checkmate_check_elapsed = self.last_checkmate_check.elapsed();
if checkmate_check_elapsed.is_ok()
&& checkmate_check_elapsed.unwrap() > Duration::from_millis(3000)
&& self.board.checkmate()
{
self.last_checkmate_check = SystemTime::now();
canvas.draw_text(
Point2 {
x: None,
y: Some(DISPLAYHEIGHT as i32 - 100),
},
"Checkmate!",
100.0,
);
unsafe {
self.board.apply_null_move(); // Allow the other to go on
}
}
}
}

0 comments on commit 78c9a1e

Please sign in to comment.