Repository: thomas-mauran/chess-tui (language: rust)

Issue:
Bot move freezes player move.
## Description
This bug occurs in a game agains a bot. After the player confirms its move the piece is not drawn on the board and the game waits for the bot's move. Only after the bot's move is received both the player and the bots moves are displayed simultaneously as opposed to being drawn in turn.

This can be observed for bots that take some time to compute the next move.

## To Reproduce
Set up a game against a bot. Make sure that the bot is not extremely optimised (as is [Stockfish](https://stockfishchess.org), for example). Make a move (as a player).

## Expected behaviour

The game will freeze until the bot move is computed. Until that the player move won't be displayed.

## Environment

- OS: MacOS
- Terminal Emulator: Terminal
- Font: ASCII
- Crate version: 1.2.1
- Backend: termion



Discussion / hints:

Fix patch:
diff --git a/src/board.rs b/src/board.rs
index 050fd84..edf39e1 100644
--- a/src/board.rs
+++ b/src/board.rs
@@ -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,
@@ -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,
@@ -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,
@@ -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;
                             }
                         }
                     }
@@ -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);
@@ -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,
diff --git a/src/main.rs b/src/main.rs
index 0feef37..84d2770 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -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.


Test patch: