[gnome-chess] Fix "engine error" on new game after engine moves during pause



commit 39a6a2fe86a27062de55ac0130aea82d36b1f981
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Fri Dec 4 18:25:17 2020 -0600

    Fix "engine error" on new game after engine moves during pause
    
    When we replay the engine's move later, we should make sure we are still
    playing the same game AND that the state of the game is still the same.
    Otherwise, discard the engine's move. The user has done something to
    advance the state of the game beyond what the engine knows about, so
    it's totally expected that the engine's move may no longer apply.
    
    The state of the game ought to always be the same as it was before. Add
    an assert just to be sure.
    
    Halfway fixes #49

 src/gnome-chess.vala | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
---
diff --git a/src/gnome-chess.vala b/src/gnome-chess.vala
index 1a361d5..b34fd90 100644
--- a/src/gnome-chess.vala
+++ b/src/gnome-chess.vala
@@ -814,6 +814,9 @@ Copyright © 2015–2016 Sahil Sareen""";
 
     private void engine_move_cb (ChessEngine engine, string move)
     {
+        var original_game = game;
+        var original_state = game.current_state;
+
         if (!game.is_paused)
         {
             do_engine_move (move);
@@ -827,7 +830,15 @@ Copyright © 2015–2016 Sahil Sareen""";
                 }
                 else
                 {
-                    do_engine_move (move);
+                    /* If the user has started a new game, then the state of the game is no longer
+                     * what it was at the time.
+                     * the engine moved, we need to discard the move. The engine will move again.
+                     */
+                    if (game == original_game)
+                    {
+                        assert (game.current_state == original_state);
+                        do_engine_move (move);
+                    }
                     return Source.REMOVE;
                 }
             });


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]