[gnome-2048] Store values as power of 2.



commit 0f3b7582f37c1fbcbf15dba87ecacb8e683da064
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Wed Feb 6 12:01:13 2019 +0100

    Store values as power of 2.

 src/grid.vala | 62 +++++++++++++++++++++++++++++++++++------------------------
 src/view.vala | 48 ++++++++++++++++++++++-----------------------
 2 files changed, 61 insertions(+), 49 deletions(-)
---
diff --git a/src/grid.vala b/src/grid.vala
index d18c1e8..09ec927 100644
--- a/src/grid.vala
+++ b/src/grid.vala
@@ -51,8 +51,8 @@ private class Grid : Object
         do { _generate_random_position (rows, cols, out pos); }
         while (_grid [pos.row, pos.col] != 0);
 
-        _grid [pos.row, pos.col] = 2;
-        tile = { pos, /* tile value */ 2 };
+        _grid [pos.row, pos.col] = 1;
+        tile = { pos, /* tile value */ 1 };
     }
 
     private static inline void _generate_random_position (int rows, int cols, out GridPosition pos)
@@ -88,7 +88,7 @@ private class Grid : Object
             case MoveRequest.RIGHT:
                 _move_right (_cols, _rows, ref max_changed, ref _grid, ref to_move, ref to_hide, ref 
to_show); break;
         }
-        if (max_changed >= target_value)
+        if (Math.pow (2, max_changed) >= target_value)
             target_value_reached = true;
     }
 
@@ -352,15 +352,15 @@ private class Grid : Object
         mov = { match, free };
         to_hide.add (mov);
 
-        uint new_val = 2 * val;
-        Tile tile = { free, new_val };
+        val++;
+        Tile tile = { free, val };
         to_show.add (tile);
 
         grid [cur.row, cur.col] = 0;
         grid [match.row, match.col] = 0;
-        grid [free.row, free.col] = new_val;
-        if (max_changed < new_val)
-            max_changed = new_val;
+        grid [free.row, free.col] = val;
+        if (max_changed < val)
+            max_changed = val;
     }
 
     private static void _move_to_end (ref Gee.LinkedList<TileMovement?> to_move,
@@ -476,8 +476,17 @@ private class Grid : Object
         uint cols = grid.length [1];
 
         for (uint i = 0; i < rows; i++)
+        {
             for (uint j = 0; j < cols; j++)
-                ret_string += "%u%s".printf (grid [i, j], (j == (cols - 1)) ? "\n" : " ");
+            {
+                uint64 val;
+                if (grid [i, j] == 0)
+                    val = 0;
+                else
+                    val = (uint64) Math.pow (2, grid [i, j]);
+                ret_string += "%llu%s".printf (val, (j == (cols - 1)) ? "\n" : " ");
+            }
+        }
     }
 
     /*\
@@ -528,42 +537,45 @@ private class Grid : Object
 
         grid = new uint [rows, cols];
 
-        for (int i = 0; i < rows; i++)
+        for (uint i = 0; i < rows; i++)
         {
             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++)
+            for (uint j = 0; j < cols; j++)
             {
                 if (!int64.try_parse (tokens [j], out cols_64))
                     return false;
-                if (_bad_tile_number (ref cols_64))
+                uint number;
+                if (!_convert_tile_number (ref cols_64, out number))
                     return false;
-                grid [i, j] = (int) cols_64;
+                grid [i, j] = number;
             }
         }
 
         return true;
     }
 
-    private static inline bool _bad_tile_number (ref int64 number)
+    private static inline bool _convert_tile_number (ref int64 number_64,
+                                                     out uint  number)
     {
-        if (number < 0)
-            return true;
-        if (number == 0)
+        if (number_64 < 0)
+        {
+            number = 0; // garbage
             return false;
-        if (number == 1)
-            return true;
-        for (int64 i = 2; i < (int64) int.MAX; i *= 2)
+        }
+        if (number_64 == 0)
         {
-            if (number == i)
-                return false;
-            if (number < i)
-                return true;
+            number = 0;
+            return true;
         }
-        return true;
+        for (number = 1; number <= 81; number++)
+            if (Math.pow (2, number) == (double) number_64)
+                return true;
+
+        return false;
     }
 }
 
diff --git a/src/view.vala b/src/view.vala
index 76bde54..06f2968 100644
--- a/src/view.vala
+++ b/src/view.vala
@@ -56,7 +56,7 @@ private class RoundedRectangle : Object
 
     internal void idle_resize ()
     {
-        if (!canvas.set_size ((int)Math.ceilf (actor.width), (int)Math.ceilf (actor.height)))
+        if (!canvas.set_size ((int) Math.ceilf (actor.width), (int) Math.ceilf (actor.height)))
             canvas.invalidate ();
     }
 
@@ -71,10 +71,10 @@ private class RoundedRectangle : Object
         ctx.restore ();
 
         ctx.new_sub_path ();
-        ctx.arc (width - radius, radius, radius, -90 * degrees, 0 * degrees);
-        ctx.arc (width - radius, height - radius, radius, 0 * degrees, 90 * degrees);
-        ctx.arc (radius, height - radius, radius, 90 * degrees, 180 * degrees);
-        ctx.arc (radius, radius, radius, 180 * degrees, 270 * degrees);
+        ctx.arc (width - radius, radius,          radius,  -90 * degrees,   0 * degrees);
+        ctx.arc (width - radius, height - radius, radius,    0 * degrees,  90 * degrees);
+        ctx.arc (radius,         height - radius, radius,   90 * degrees, 180 * degrees);
+        ctx.arc (radius,         radius,          radius,  180 * degrees, 270 * degrees);
         ctx.close_path ();
 
         Clutter.cairo_set_source_color (ctx, (!) _color);
@@ -86,7 +86,7 @@ private class RoundedRectangle : Object
 
 private class TileView : RoundedRectangle
 {
-    internal uint tile_value { internal get; private set; default = 2; }
+    internal uint tile_value { internal get; private set; default = 1; }
 
     internal TileView (float x, float y, float width, float height, uint val)
     {
@@ -108,7 +108,7 @@ private class TileView : RoundedRectangle
         font_desc = Pango.FontDescription.from_string ("Sans Bold %dpx".printf (height / 4));
         layout.set_font_description (font_desc);
 
-        layout.set_text (tile_value.to_string (), -1);
+        layout.set_text (Math.pow (2, tile_value).to_string (), -1);
 
         layout.get_extents (null, out logical_rect);
         ctx.move_to ((width / 2) - (logical_rect.width / 2 / Pango.SCALE),
@@ -124,42 +124,42 @@ private class TileView : RoundedRectangle
 
     private static Clutter.Color _pick_color (uint tile_value)
     {
-        if (tile_value <= 2048)
+        if (tile_value <= 11)
             return _pick_palette_color (tile_value);
         else
             return _calculate_color (tile_value);
     }
 
     private static Clutter.Color _pick_palette_color (uint tile_value)
+        requires (tile_value != 0)
+        requires (tile_value <= 11)
     {
         switch (tile_value)
         {
-            case 2:    return Clutter.Color.from_string ("#fce94f"); // Butter 1
-            case 4:    return Clutter.Color.from_string ("#8ae234"); // Chameleon 1
-            case 8:    return Clutter.Color.from_string ("#fcaf3e"); // Orange 1
-            case 16:   return Clutter.Color.from_string ("#729fcf"); // Sky blue 1
-            case 32:   return Clutter.Color.from_string ("#ad7fa8"); // Plum 1
-            case 64:   return Clutter.Color.from_string ("#c17d11"); // Chocolate 2
-            case 128:  return Clutter.Color.from_string ("#ef2929"); // Scarlet red 1
-            case 256:  return Clutter.Color.from_string ("#c4a000"); // Butter 3
-            case 512:  return Clutter.Color.from_string ("#4e9a06"); // Chameleon 3
-            case 1024: return Clutter.Color.from_string ("#ce5c00"); // Orange 3
-            case 2048: return Clutter.Color.from_string ("#204a87"); // Sky blue 3
-            default:   assert_not_reached ();
+            case 1:  return Clutter.Color.from_string ("#fce94f"); // Butter 1
+            case 2:  return Clutter.Color.from_string ("#8ae234"); // Chameleon 1
+            case 3:  return Clutter.Color.from_string ("#fcaf3e"); // Orange 1
+            case 4:  return Clutter.Color.from_string ("#729fcf"); // Sky blue 1
+            case 5:  return Clutter.Color.from_string ("#ad7fa8"); // Plum 1
+            case 6:  return Clutter.Color.from_string ("#c17d11"); // Chocolate 2
+            case 7:  return Clutter.Color.from_string ("#ef2929"); // Scarlet red 1
+            case 8:  return Clutter.Color.from_string ("#c4a000"); // Butter 3
+            case 9:  return Clutter.Color.from_string ("#4e9a06"); // Chameleon 3
+            case 10: return Clutter.Color.from_string ("#ce5c00"); // Orange 3
+            case 11: return Clutter.Color.from_string ("#204a87"); // Sky blue 3
+            default: assert_not_reached ();
         }
     }
 
     private static Clutter.Color _calculate_color (uint tile_value)
     {
-        uint norm_val = tile_value / 2048;  /* 2^12 to 2^22 */
-        if (norm_val > 2048)
-            norm_val /= 2048;               /* 2^23 to 2^31 */
+        uint norm_val = (tile_value - 1) % 11 + 1;
         Clutter.Color? nullable_color = _pick_palette_color (norm_val);
         if (nullable_color == null)
             assert_not_reached ();
         Clutter.Color color = (!) nullable_color;
 
-        uint8 sbits = (uint8) (tile_value % 7);
+        uint8 sbits = (uint8) (Math.pow (2, tile_value) % 7);
         color.red   <<= sbits;
         color.green <<= sbits;
         color.blue  <<= sbits;


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