[gnome-2048] Fix bug when n_rows != n_cols.



commit 6cf4722abfb70de500cb9e8c301f8fbb6e866368
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Mon Jan 28 09:09:31 2019 +0100

    Fix bug when n_rows != n_cols.
    
    GNOME 2048 always allowed to play with a
    non-square grid, but there was a bug due
    to an iteration on the rows number for a
    left move. Fix it and some other things.

 data/org.gnome.2048.gschema.xml |   2 +
 src/game.vala                   |   2 +-
 src/grid.vala                   | 114 ++++++++++++++--------------------------
 3 files changed, 42 insertions(+), 76 deletions(-)
---
diff --git a/data/org.gnome.2048.gschema.xml b/data/org.gnome.2048.gschema.xml
index 2b84e27..94d87ca 100644
--- a/data/org.gnome.2048.gschema.xml
+++ b/data/org.gnome.2048.gschema.xml
@@ -23,6 +23,7 @@
     </key>
     <key name="rows" type="i">
       <default>4</default>
+      <range min="1" max="9"/>
       <!-- Translators: summary of a settings key, see 'dconf-editor /org/gnome/2048/rows' -->
       <summary>Number of rows</summary>
       <!-- Translators: description of a settings key, see 'dconf-editor /org/gnome/2048/rows' -->
@@ -30,6 +31,7 @@
     </key>
     <key name="cols" type="i">
       <default>4</default>
+      <range min="1" max="9"/>
       <!-- Translators: summary of a settings key, see 'dconf-editor /org/gnome/2048/cols' -->
       <summary>Number of columns</summary>
       <!-- Translators: description of a settings key, see 'dconf-editor /org/gnome/2048/cols' -->
diff --git a/src/game.vala b/src/game.vala
index 7045e61..efc07a9 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -486,7 +486,7 @@ public class Game : Object
 
         if ((_to_move.size > 0) || (_to_hide.size > 0) || (_to_show.size > 0))
         {
-            _state = GameState.MOVING_LEFT;
+            _state = GameState.MOVING_RIGHT;
             _move_trans.start ();
             _store_movement (clone);
         }
diff --git a/src/grid.vala b/src/grid.vala
index 0787ede..e6b1c13 100644
--- a/src/grid.vala
+++ b/src/grid.vala
@@ -92,28 +92,19 @@ public class Grid : Object
                            Gee.LinkedList<TileMovement?> to_hide,
                            Gee.LinkedList<Tile?> to_show)
     {
-        GridPosition free;
-        GridPosition cur;
-        GridPosition match;
-        bool has_match;
-        int row;
-        uint val;
-        TileMovement mov;
-        Tile tile;
-
         to_move.clear ();
         to_hide.clear ();
         to_show.clear ();
 
         for (int i = 0; i < _cols; i++)
         {
-            free = { _rows, i };
+            GridPosition free = { _rows, i };
 
             for (int j = 0; j < _rows; j++)
             {
-                row = _rows - j - 1;
-                cur = { row, i };
-                val = _grid[cur.row,cur.col];
+                int row = _rows - j - 1;
+                GridPosition cur = { row, i };
+                uint val = _grid[cur.row,cur.col];
 
                 if (val == 0)
                 {
@@ -123,8 +114,8 @@ public class Grid : Object
                 }
 
                 // search for matches
-                match = { 0, 0 };
-                has_match = false;
+                GridPosition match = { 0, 0 };
+                bool has_match = false;
                 for (int k = row - 1; k >= 0; k--)
                 {
                     uint k_val = _grid[k,cur.col];
@@ -147,12 +138,12 @@ public class Grid : Object
                     if (free.row == _rows)
                         free.row = row; // temporarily
 
-                    mov = { cur, free };
+                    TileMovement mov = { cur, free };
                     to_hide.add (mov);
                     mov = { match, free };
                     to_hide.add (mov);
 
-                    tile = { free, val*2 };
+                    Tile tile = { free, val*2 };
                     to_show.add (tile);
 
                     _grid[cur.row,cur.col] = 0;
@@ -166,7 +157,7 @@ public class Grid : Object
                 {
                     debug (@"moving $cur to $free");
 
-                    mov = { cur, free };
+                    TileMovement mov = { cur, free };
                     to_move.add (mov);
 
                     _grid[cur.row,cur.col] = 0;
@@ -182,28 +173,19 @@ public class Grid : Object
                          Gee.LinkedList<TileMovement?> to_hide,
                          Gee.LinkedList<Tile?> to_show)
     {
-        GridPosition free;
-        GridPosition cur;
-        GridPosition match;
-        bool has_match;
-        int row;
-        uint val;
-        TileMovement mov;
-        Tile tile;
-
         to_move.clear ();
         to_hide.clear ();
         to_show.clear ();
 
         for (int i = 0; i < _cols; i++)
         {
-            free = { -1, i };
+            GridPosition free = { -1, i };
 
             for (int j = 0; j < _rows; j++)
             {
-                row = j;
-                cur = { row, i };
-                val = _grid[cur.row,cur.col];
+                int row = j;
+                GridPosition cur = { row, i };
+                uint val = _grid[cur.row,cur.col];
 
                 if (val == 0)
                 {
@@ -213,8 +195,8 @@ public class Grid : Object
                 }
 
                 // search for matches
-                match = { 0, 0 };
-                has_match = false;
+                GridPosition match = { 0, 0 };
+                bool has_match = false;
                 for (int k = row + 1; k < _rows; k++)
                 {
                     uint k_val = _grid[k,cur.col];
@@ -237,12 +219,12 @@ public class Grid : Object
                     if (free.row == -1)
                         free.row = row; // temporarily
 
-                    mov = { cur, free };
+                    TileMovement mov = { cur, free };
                     to_hide.add (mov);
                     mov = { match, free };
                     to_hide.add (mov);
 
-                    tile = { free, val*2 };
+                    Tile tile = { free, val*2 };
                     to_show.add (tile);
 
                     _grid[cur.row,cur.col] = 0;
@@ -256,7 +238,7 @@ public class Grid : Object
                 {
                     debug (@"moving $cur to $free");
 
-                    mov = { cur, free };
+                    TileMovement mov = { cur, free };
                     to_move.add (mov);
 
                     _grid[cur.row,cur.col] = 0;
@@ -272,28 +254,19 @@ public class Grid : Object
                            Gee.LinkedList<TileMovement?> to_hide,
                            Gee.LinkedList<Tile?> to_show)
     {
-        GridPosition free;
-        GridPosition cur;
-        GridPosition match;
-        bool has_match;
-        int col;
-        uint val;
-        TileMovement mov;
-        Tile tile;
-
         to_move.clear ();
         to_hide.clear ();
         to_show.clear ();
 
         for (int i = 0; i < _rows; i++)
         {
-            free = { i, -1 };
+            GridPosition free = { i, -1 };
 
             for (int j = 0; j < _cols; j++)
             {
-                col = j;
-                cur = { i, col };
-                val = _grid[cur.row,cur.col];
+                int col = j;
+                GridPosition cur = { i, col };
+                uint val = _grid[cur.row,cur.col];
 
                 if (val == 0)
                 {
@@ -303,9 +276,9 @@ public class Grid : Object
                 }
 
                 // search for matches
-                match = { 0, 0 };
-                has_match = false;
-                for (int k = col + 1; k < _rows; k++)
+                GridPosition match = { 0, 0 };
+                bool has_match = false;
+                for (int k = col + 1; k < _cols; k++)
                 {
                     uint k_val = _grid[cur.row,k];
 
@@ -327,12 +300,12 @@ public class Grid : Object
                     if (free.col == -1)
                         free.col = col; // temporarily
 
-                    mov = { cur, free };
+                    TileMovement mov = { cur, free };
                     to_hide.add (mov);
                     mov = { match, free };
                     to_hide.add (mov);
 
-                    tile = { free, val*2 };
+                    Tile tile = { free, val*2 };
                     to_show.add (tile);
 
                     _grid[cur.row,cur.col] = 0;
@@ -346,7 +319,7 @@ public class Grid : Object
                 {
                     debug (@"moving $cur to $free");
 
-                    mov = { cur, free };
+                    TileMovement mov = { cur, free };
                     to_move.add (mov);
 
                     _grid[cur.row,cur.col] = 0;
@@ -362,28 +335,19 @@ public class Grid : Object
                             Gee.LinkedList<TileMovement?> to_hide,
                             Gee.LinkedList<Tile?> to_show)
     {
-        GridPosition free;
-        GridPosition cur;
-        GridPosition match;
-        bool has_match;
-        int col;
-        uint val;
-        TileMovement mov;
-        Tile tile;
-
         to_move.clear ();
         to_hide.clear ();
         to_show.clear ();
 
         for (int i = 0; i < _rows; i++)
         {
-            free = { i, _cols };
+            GridPosition free = { i, _cols };
 
             for (int j = 0; j < _cols; j++)
             {
-                col = _cols - j - 1;
-                cur = { i, col };
-                val = _grid[cur.row,cur.col];
+                int col = _cols - j - 1;
+                GridPosition cur = { i, col };
+                uint val = _grid[cur.row,cur.col];
 
                 if (val == 0)
                 {
@@ -393,8 +357,8 @@ public class Grid : Object
                 }
 
                 // search for matches
-                match = { 0, 0 };
-                has_match = false;
+                GridPosition match = { 0, 0 };
+                bool has_match = false;
                 for (int k = col - 1; k >= 0; k--)
                 {
                     uint k_val = _grid[cur.row,k];
@@ -417,12 +381,12 @@ public class Grid : Object
                     if (free.col == _cols)
                         free.col = col; // temporarily
 
-                    mov = { cur, free };
+                    TileMovement mov = { cur, free };
                     to_hide.add (mov);
                     mov = { match, free };
                     to_hide.add (mov);
 
-                    tile = { free, val*2 };
+                    Tile tile = { free, val*2 };
                     to_show.add (tile);
 
                     _grid[cur.row,cur.col] = 0;
@@ -436,7 +400,7 @@ public class Grid : Object
                 {
                     debug (@"moving $cur to $free");
 
-                    mov = { cur, free };
+                    TileMovement mov = { cur, free };
                     to_move.add (mov);
 
                     _grid[cur.row,cur.col] = 0;
@@ -587,8 +551,8 @@ public class Grid : Object
 
 public struct GridPosition
 {
-    public uint row;
-    public uint col;
+    public int row;
+    public int col;
 
     public string to_string ()
     {


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