[gnome-chess] Add a delay before the computer moves



commit 5acf31a13ecd201c2c57f8c90beb8b73119901ef
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Sun Feb 16 14:06:54 2014 -0600

    Add a delay before the computer moves
    
    Slowing down the game makes it less overwhelming.

 data/engines.conf          |    1 +
 src/ai-profile.vala        |    4 ++++
 src/chess-engine-cecp.vala |    5 +++--
 src/chess-engine-uci.vala  |    5 +++--
 src/chess-engine.vala      |   19 ++++++++++++++++---
 src/gnome-chess.vala       |    6 +++---
 6 files changed, 30 insertions(+), 10 deletions(-)
---
diff --git a/data/engines.conf b/data/engines.conf
index 1cb41c3..507b5c4 100644
--- a/data/engines.conf
+++ b/data/engines.conf
@@ -3,6 +3,7 @@
 [Amy]
 protocol=cecp
 binary=Amy
+delay-before-move=0
 option-easy-0=easy
 option-normal-0=easy
 option-hard-0=hard
diff --git a/src/ai-profile.vala b/src/ai-profile.vala
index 3df6251..e5e315d 100644
--- a/src/ai-profile.vala
+++ b/src/ai-profile.vala
@@ -16,6 +16,7 @@ public class AIProfile
     public string protocol { get; private set; }
     public string binary { get; private set; }
     public string path { get; private set; }
+    public uint delay_seconds { get; private set; default = 2; }
     public string[] easy_args { get; private set; }
     public string[] normal_args { get; private set; }
     public string[] hard_args { get; private set; }
@@ -64,6 +65,9 @@ public class AIProfile
                profile.easy_uci_go_options = load_array (file, name, "uci-go-option", "easy");
                profile.normal_uci_go_options = load_array (file, name, "uci-go-option", "normal");
                profile.hard_uci_go_options = load_array (file, name, "uci-go-option", "hard");
+
+               if (file.has_key (name, "delay-before-move"))
+                   profile.delay_seconds = file.get_integer (name, "delay-before-move");
            }
            catch (KeyFileError e)
            {
diff --git a/src/chess-engine-cecp.vala b/src/chess-engine-cecp.vala
index a4daf87..3070c2d 100644
--- a/src/chess-engine-cecp.vala
+++ b/src/chess-engine-cecp.vala
@@ -1,6 +1,7 @@
 /* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  *
  * Copyright (C) 2010-2013 Robert Ancell
+ * Copyright (C) 2013-2014 Michael Catanzaro
  *
  * This program is free software: you can redistribute it and/or modify it under
  * the terms of the GNU General Public License as published by the Free Software
@@ -15,9 +16,9 @@ public class ChessEngineCECP : ChessEngine
     private bool moving = false;
     private string[] options;
 
-    public ChessEngineCECP (string binary, string[] args, string[] options)
+    public ChessEngineCECP (string binary, string[] args, uint delay_seconds, string[] options)
     {
-        base (binary, args);
+        base (binary, args, delay_seconds);
         this.options = options;
         starting.connect (start_cb);
     }
diff --git a/src/chess-engine-uci.vala b/src/chess-engine-uci.vala
index 2152e90..9a67663 100644
--- a/src/chess-engine-uci.vala
+++ b/src/chess-engine-uci.vala
@@ -1,6 +1,7 @@
 /* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  *
  * Copyright (C) 2010-2013 Robert Ancell
+ * Copyright (C) 2013-2014 Michael Catanzaro
  *
  * This program is free software: you can redistribute it and/or modify it under
  * the terms of the GNU General Public License as published by the Free Software
@@ -17,9 +18,9 @@ public class ChessEngineUCI : ChessEngine
     private string go_options;
     private bool waiting_for_move;
 
-    public ChessEngineUCI (string binary, string[] args, string[] options, string[] go_options)
+    public ChessEngineUCI (string binary, string[] args, uint delay_seconds, string[] options, string[] 
go_options)
     {
-        base (binary, args);
+        base (binary, args, delay_seconds);
         this.options = options;
         this.go_options = string.joinv (" ", go_options);
         buffer = new char[0];
diff --git a/src/chess-engine.vala b/src/chess-engine.vala
index 34b3115..6f83b65 100644
--- a/src/chess-engine.vala
+++ b/src/chess-engine.vala
@@ -1,6 +1,7 @@
 /* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  *
  * Copyright (C) 2010-2013 Robert Ancell
+ * Copyright (C) 2013-2014 Michael Catanzaro
  *
  * This program is free software: you can redistribute it and/or modify it under
  * the terms of the GNU General Public License as published by the Free Software
@@ -14,6 +15,8 @@ public abstract class ChessEngine : Object
     private string binary;
     private string[] args;
 
+    private uint delay_seconds;
+
     private Pid pid;
     private int stdin_fd;
     private int stderr_fd;
@@ -46,10 +49,11 @@ public abstract class ChessEngine : Object
         }
     }
 
-    public ChessEngine (string binary, string[] args)
+    public ChessEngine (string binary, string[] args, uint delay_seconds)
     {
         this.binary = binary;
         this.args = args;
+        this.delay_seconds = delay_seconds;
     }
 
     public bool start ()
@@ -106,12 +110,21 @@ public abstract class ChessEngine : Object
 
     public abstract void start_game ();
 
-    public abstract void request_move ();
-
     public abstract void report_move (ChessMove move);
 
     public abstract void undo ();
 
+    protected abstract void request_move ();
+
+    public void move ()
+    {
+        Timeout.add_seconds (delay_seconds, () => {
+            request_move ();
+            /* Disconnect from main loop */
+            return false;
+        });
+    }
+
     public void stop ()
     {
         if (!started)
diff --git a/src/gnome-chess.vala b/src/gnome-chess.vala
index 5bdd078..ee7140d 100644
--- a/src/gnome-chess.vala
+++ b/src/gnome-chess.vala
@@ -629,11 +629,11 @@ public class Application : Gtk.Application
         if (profile.protocol == "cecp")
         {
             warn_if_fail (uci_go_options.length == 0);
-            engine = new ChessEngineCECP (profile.binary, args, options);
+            engine = new ChessEngineCECP (profile.binary, args, profile.delay_seconds, options);
         }
         else if (profile.protocol == "uci")
         {
-            engine = new ChessEngineUCI (profile.binary, args, options, uci_go_options);
+            engine = new ChessEngineUCI (profile.binary, args, profile.delay_seconds, options, 
uci_go_options);
         }
         else
         {
@@ -728,7 +728,7 @@ public class Application : Gtk.Application
             enable_window_action (PAUSE_RESUME_ACTION_NAME);
         
         if (game.is_started && opponent_engine != null && player == opponent)
-            opponent_engine.request_move ();
+            opponent_engine.move ();
     }
 
     private void set_move_text (Gtk.TreeIter iter, ChessMove move)


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