[iagno] Allow to log game moves.



commit b7f8bbe275355f2fdfdb9466df73edad119cdefa
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Mon Oct 14 10:49:58 2019 +0200

    Allow to log game moves.

 src/game.vala  | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 src/iagno.vala |  6 +++++-
 2 files changed, 55 insertions(+), 5 deletions(-)
---
diff --git a/src/game.vala b/src/game.vala
index 3053b57..c189585 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -631,14 +631,24 @@ private class Game : Object
     [CCode (notify = false)] public Opening         opening                 { internal get; protected 
construct set; }
     [CCode (notify = false)] public GameStateObject current_state           { internal get; protected 
construct set; }
     [CCode (notify = false)] public uint8           initial_number_of_tiles { internal get; protected 
construct;     }
+    [CCode (notify = false)] public bool            print_logs              { internal get; protected 
construct;     }
 
     construct
     {
         undo_stack.append (current_state);
         update_possible_moves ();
+
+        if (print_logs)
+        {
+            string e_or_i = reverse ? "e" : "i";
+            if (initial_number_of_tiles == 0)
+                print (@"\nnew two-player revers$e_or_i game\n");
+            else
+                print (@"\nnew one-player revers$e_or_i game ($opening opening)\n");    // TODO is human 
Dark or Light?
+        }
     }
 
-    internal Game (bool _reverse, Opening _opening = Opening.REVERSI, uint8 _size = 8)
+    internal Game (bool _reverse, Opening _opening = Opening.REVERSI, uint8 _size = 8, bool _print_logs = 
false)
         requires (_size >= 4)
         requires (_size <= 16)
     {
@@ -673,7 +683,8 @@ private class Game : Object
                 reverse                 : _reverse,
                 opening                 : _opening,
                 current_state           : _current_state,
-                initial_number_of_tiles : _initial_number_of_tiles);
+                initial_number_of_tiles : _initial_number_of_tiles,
+                print_logs              : _print_logs);
         neighbor_tiles = (owned) _neighbor_tiles;
     }
     private static inline void setup_even_board (uint8 size, Opening opening, ref Player [,] tiles, out 
uint8 initial_number_of_tiles)
@@ -736,7 +747,7 @@ private class Game : Object
         tiles [mid_board + 1, mid_board + 1] = start_position [2, 2];
     }
 
-    internal Game.from_strings (string [] setup, Player to_move, bool _reverse = false, uint8 _size = 8)
+    internal Game.from_strings (string [] setup, Player to_move, bool _reverse = false, uint8 _size = 8, 
bool _print_logs = false)
         requires (_size >= 4)
         requires (_size <= 16)
         requires (to_move != Player.NONE)
@@ -761,7 +772,8 @@ private class Game : Object
                 reverse                 : _reverse,
                 opening                 : /* garbage */ Opening.REVERSI,
                 current_state           : _current_state,
-                initial_number_of_tiles : (_size % 2 == 0) ? 4 : 7);
+                initial_number_of_tiles : (_size % 2 == 0) ? 4 : 7,
+                print_logs              : _print_logs);
         neighbor_tiles = (owned) _neighbor_tiles;
 
         warn_if_fail (string.joinv ("\n", (string? []) setup).strip () == to_string ().strip ());
@@ -825,6 +837,11 @@ private class Game : Object
                     else                                                                opening = 
Opening.INVERTED;
                 }
             }
+            if (print_logs)
+            {
+                string current_color_string = current_color == Player.DARK ? "dark :" : "light:";
+                print (@"$current_color_string ($x, $y)\n");
+            }
             current_state = new GameStateObject.copy_and_add (current_state, x, y);
 
             if (n_light_tiles == 2)
@@ -841,6 +858,12 @@ private class Game : Object
         if (!current_state.test_placing_tile (x, y, out move))
             return false;
 
+        if (print_logs)
+        {
+            string current_color_string = current_color == Player.DARK ? "dark :" : "light:";
+            print (@"$current_color_string ($x, $y)\n");
+        }
+
         current_state = new GameStateObject.copy_and_move (current_state, move);
         undo_stack.append (current_state);
         end_of_turn (/* undoing */ false, /* no_draw */ false);
@@ -852,6 +875,14 @@ private class Game : Object
         if (current_player_can_move)
             return false;
 
+        if (print_logs)
+        {
+            if (current_color == Player.DARK)
+                print ("dark : pass\n");
+            else
+                print ("light: pass\n");
+        }
+
         current_state = new GameStateObject.copy_and_pass (current_state);
         undo_stack.append (current_state);
         end_of_turn (/* undoing */ false, /* no_draw */ true);
@@ -1025,4 +1056,19 @@ private enum Opening {
     ALTER_LEFT,
     ALTER_RIGHT,
     ALTER_BOTTOM;
+
+    internal string to_string ()
+    {
+        switch (this)
+        {
+            case HUMANS:        return "humans";
+            case REVERSI:       return "reversi";
+            case INVERTED:      return "inverted";
+            case ALTER_TOP:     return "alter-top";
+            case ALTER_LEFT:    return "alter-left";
+            case ALTER_RIGHT:   return "alter-right";
+            case ALTER_BOTTOM:  return "alter-bottom";
+            default:            assert_not_reached ();
+        }
+    }
 }
diff --git a/src/iagno.vala b/src/iagno.vala
index a90d662..a0d1562 100644
--- a/src/iagno.vala
+++ b/src/iagno.vala
@@ -30,6 +30,7 @@ private class Iagno : Gtk.Application, BaseApplication
     /* Application settings */
     private GLib.Settings settings;
     private static bool fast_mode;
+    private static bool print_logs;
     private static bool alternative_start;
     private static bool random_start;
     private static bool usual_start;
@@ -84,6 +85,9 @@ private class Iagno : Gtk.Application, BaseApplication
         /* Translators: command-line option description, see 'iagno --help' */
         { "mute", 0, OptionFlags.NONE, OptionArg.NONE, null,                               N_("Turn off the 
sound"), null },
 
+        /* Translators: command-line option description, currently hidden; might appear one day in 'iagno 
--help' */
+        { "print-logs", 0, OptionFlags.HIDDEN, OptionArg.NONE, ref print_logs,             N_("Log the game 
moves"), null },
+
         /* Translators: command-line option description, see 'iagno --help' */
         { "random-start", 0, OptionFlags.NONE, OptionArg.NONE, ref random_start,           N_("Start with a 
random position"), null },
 
@@ -661,7 +665,7 @@ private class Iagno : Gtk.Application, BaseApplication
             opening = Opening.REVERSI;
 
         bool reverse = settings.get_string ("type") == "reverse";
-        game = new Game (reverse, opening, (uint8) size /* 4 <= size <= 16 */);
+        game = new Game (reverse, opening, (uint8) size /* 4 <= size <= 16 */, print_logs);
         game_is_set = true;
         game.turn_ended.connect (turn_ended_cb);
         view.game = game;


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