Skip to content

Commit

Permalink
Bug fix: Bot move freezes player move (#84)
Browse files Browse the repository at this point in the history
* Bug fix: Bot move freezes player move
---------
Signed-off-by: Thomas Mauran <[email protected]>
  • Loading branch information
LucaSain authored Nov 23, 2024
1 parent d07506d commit 433709e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ pub struct Board {
pub is_game_against_bot: bool,
// the display mode
pub display_mode: DisplayMode,
/// Used to indicate if a bot move is following
pub bot_will_move: bool,
// coordinates of the interactable part of the screen (either normal chess board or promotion screen)
pub top_x: u16,
pub top_y: u16,
Expand Down Expand Up @@ -215,6 +217,7 @@ impl Default for Board {
engine: None,
is_game_against_bot: false,
display_mode: DisplayMode::DEFAULT,
bot_will_move: false,
top_x: 0,
top_y: 0,
width: 0,
Expand Down Expand Up @@ -246,6 +249,7 @@ impl Board {
engine: None,
is_game_against_bot: false,
display_mode: DisplayMode::DEFAULT,
bot_will_move: false,
top_x: 0,
top_y: 0,
width: 0,
Expand Down Expand Up @@ -452,13 +456,13 @@ impl Board {
}
// If we play against a bot we will play his move and switch the player turn again
if self.is_game_against_bot {
// do this in background
self.is_promotion = self.is_latest_move_promotion();
if !self.is_promotion {
self.is_checkmate = self.is_checkmate();
self.is_promotion = self.is_latest_move_promotion();
if !self.is_checkmate {
self.bot_move();
self.switch_player_turn();
self.bot_will_move = true;
}
}
}
Expand Down Expand Up @@ -1169,7 +1173,6 @@ impl Board {

pieces.push_str(&format!("{utf_icon_white} "));
}

let white_material_paragraph = Paragraph::new(pieces)
.alignment(Alignment::Center)
.add_modifier(Modifier::BOLD);
Expand All @@ -1180,7 +1183,6 @@ impl Board {
.direction(Direction::Vertical)
.constraints([Constraint::Length(height - 1), Constraint::Length(1)].as_ref())
.split(area);

frame.render_widget(white_block.clone(), right_panel_layout[0]);
frame.render_widget(
white_material_paragraph,
Expand Down
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ fn main() -> AppResult<()> {
Event::Mouse(mouse_event) => handle_mouse_events(mouse_event, &mut app)?,
Event::Resize(_, _) => {}
}
if app.board.bot_will_move {
app.board.bot_move();
app.board.switch_player_turn();
app.board.bot_will_move = false;
// need to be centralised
app.board.is_checkmate = app.board.is_checkmate();
app.board.is_draw = app.board.is_draw();
tui.draw(&mut app)?;
}
}

// Exit the user interface.
Expand Down

0 comments on commit 433709e

Please sign in to comment.