[gnome-2048] Block loading grids of disabled sizes.



commit bf15f510e9758ca430298dab985e97e0a205e56c
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Mon Jan 28 21:31:15 2019 +0100

    Block loading grids of disabled sizes.
    
    The grids sized 2 by 1 are not allowed,
    so do not restore such grid at startup.

 src/application.vala |  7 ++++++-
 src/game.vala        |  2 +-
 src/grid.vala        | 22 ++++++++++++----------
 3 files changed, 19 insertions(+), 12 deletions(-)
---
diff --git a/src/application.vala b/src/application.vala
index 91c8359..a6c3f64 100644
--- a/src/application.vala
+++ b/src/application.vala
@@ -391,7 +391,7 @@ public class Application : Gtk.Application
 
         int rows = _settings.get_int ("rows");
         int cols = _settings.get_int ("cols");
-        bool disallowed_grid = (rows == 1 && cols == 2) || (rows == 2 && cols == 1);
+        bool disallowed_grid = is_disallowed_grid_size (ref rows, ref cols);
         if (disallowed_grid)
             warning (_("Grids of size 1 by 2 are disallowed."));
 
@@ -409,6 +409,11 @@ public class Application : Gtk.Application
         menu.append (label, "app.new-game-sized(" + variant.print (/* annotate types */ true) + ")");
     }
 
+    public static bool is_disallowed_grid_size (ref int rows, ref int cols)
+    {
+        return (rows == 1 && cols == 2) || (rows == 2 && cols == 1);
+    }
+
     /*\
     * * window management callbacks
     \*/
diff --git a/src/game.vala b/src/game.vala
index efc07a9..dbae0c9 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -182,7 +182,7 @@ public class Game : Object
         }
 
         lines = contents.split ("\n");
-        score = (uint)int.parse (lines[lines.length-2]);
+        score = (uint) int.parse (lines [lines.length - 2]);
 
         if (_background != null)
             _clear_background ();
diff --git a/src/grid.vala b/src/grid.vala
index e6b1c13..6a0cca6 100644
--- a/src/grid.vala
+++ b/src/grid.vala
@@ -481,14 +481,14 @@ public class Grid : Object
     {
         int rows = 0;
         int cols = 0;
-        string[] lines;
-        string[] tokens;
-        uint[,] grid;
+        string [] lines;
+        string [] tokens;
+        uint [,] grid;
 
         lines = contents.split ("\n");
 
-        // check that at least it contains 2 rows
-        if (lines.length < 3)
+        // check that at least it contains 3 rows: size, content, score
+        if (lines.length < 4)
             return false;
 
         tokens = lines[0].split (" ");
@@ -498,23 +498,25 @@ public class Grid : Object
         rows = int.parse (tokens[0]);
         cols = int.parse (tokens[1]);
 
-        if ((rows < 2) || (cols < 2))
+        if ((rows < 1) || (cols < 1))
+            return false;
+        if (Application.is_disallowed_grid_size (ref rows, ref cols))
             return false;
         // we don't need to be strict here
-        if (lines.length < (rows+1))
+        if (lines.length < rows + 1)
             return false;
 
-        grid = new uint[rows, cols];
+        grid = new uint [rows, cols];
 
         for (int i = 0; i < rows; i++)
         {
-            tokens = lines[i+1].split (" ");
+            tokens = lines [i + 1].split (" ");
             // we do need to be strict here
             if (tokens.length != cols)
                 return false;
 
             for (int j = 0; j < cols; j++)
-                grid[i,j] = int.parse (tokens[j]);
+                grid [i, j] = int.parse (tokens [j]);
         }
 
         _rows = rows;


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