[gnome-klotski] Use uint8.



commit 3785ffa6e5f2638fbc7bc66c78ba3a0096bd3f73
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Wed Feb 5 16:12:18 2020 +0100

    Use uint8.

 src/klotski-window.vala | 71 +++++++++++++++++++--------------------
 src/puzzle-view.vala    | 34 ++++++++++---------
 src/puzzle.vala         | 88 +++++++++++++++++++++++++++----------------------
 3 files changed, 103 insertions(+), 90 deletions(-)
---
diff --git a/src/klotski-window.vala b/src/klotski-window.vala
index 701a695..76d0a90 100644
--- a/src/klotski-window.vala
+++ b/src/klotski-window.vala
@@ -19,19 +19,19 @@
 
 using Gtk;
 
-/* Puzzle Info */
-private struct LevelInfo
-{
-    string name;
-    int group;
-    int width;
-    int height;
-    string data;
-}
-
 [GtkTemplate (ui = "/org/gnome/Klotski/ui/klotski.ui")]
 private class KlotskiWindow : ApplicationWindow
 {
+    /* Puzzle Info */
+    private struct LevelInfo
+    {
+        string  name;
+        uint8   group;
+        uint8   width;
+        uint8   height;
+        string  data;
+    }
+
     /* Settings */
     private GLib.Settings settings;
     private bool is_tiled;
@@ -468,7 +468,7 @@ private class KlotskiWindow : ApplicationWindow
     class construct
     {
         score_categories = new Gee.ArrayList<Games.Scores.Category> ();
-        for (var i = 0; i < levels.length; i++)
+        for (uint8 i = 0; i < levels.length; i++)
         {
             score_categories.add (new Games.Scores.Category (normalize_map_name (levels[i].name),
                                                              _(levels[i].name)));
@@ -477,7 +477,7 @@ private class KlotskiWindow : ApplicationWindow
 
     private Games.Scores.Category? category_request (string key)
     {
-        for (int i = 0; i < levels.length; i++)
+        for (uint8 i = 0; i < levels.length; i++)
         {
             if (key == normalize_map_name (levels[i].name))
                 return score_categories[i];
@@ -490,21 +490,21 @@ private class KlotskiWindow : ApplicationWindow
         score = null;
         category = null;
 
-        var tokens = line.split (" ");
+        string [] tokens = line.split (" ");
         if (tokens.length != 3)
             return;
 
-        var date = Games.Scores.HistoryFileImporter.parse_date (tokens[0]);
+        int64 date = Games.Scores.HistoryFileImporter.parse_date (tokens [0]);
         if (date == 0)
             return;
 
-        var level = int.parse (tokens[1]);
-        if (level == 0 && tokens[1] != "0")
+        int level = int.parse (tokens [1]);
+        if (level == 0 && tokens [1] != "0")
             return;
         if (level < 0 || level > score_categories.size)
             return;
 
-        var moves = int.parse (tokens[2]);
+        int moves = int.parse (tokens [2]);
         if (moves <= 0)
             return;
 
@@ -514,7 +514,7 @@ private class KlotskiWindow : ApplicationWindow
 
     internal KlotskiWindow ()
     {
-        var css_provider = new CssProvider ();
+        CssProvider css_provider = new CssProvider ();
         css_provider.load_from_resource ("/org/gnome/Klotski/ui/klotski.css");
         StyleContext.add_provider_for_screen (Gdk.Screen.get_default (), css_provider, 
STYLE_PROVIDER_PRIORITY_APPLICATION);
 
@@ -540,12 +540,12 @@ private class KlotskiWindow : ApplicationWindow
              new Games.Scores.HistoryFileImporter (parse_old_score));
 
         // name, active, puzzle number (or -1), sensitive=false CSS hack
-        liststore_huarong = new Gtk.ListStore (4, typeof (string), typeof (bool), typeof (int), typeof 
(bool));
+        liststore_huarong   = new Gtk.ListStore (4, typeof (string), typeof (bool), typeof (int), typeof 
(bool));
         liststore_challenge = new Gtk.ListStore (4, typeof (string), typeof (bool), typeof (int), typeof 
(bool));
-        liststore_skill = new Gtk.ListStore (4, typeof (string), typeof (bool), typeof (int), typeof (bool));
+        liststore_skill     = new Gtk.ListStore (4, typeof (string), typeof (bool), typeof (int), typeof 
(bool));
 
         puzzles_items = new TreeIter[levels.length];
-        for (var i = 0; i < levels.length; i++)
+        for (uint8 i = 0; i < levels.length; i++)
         {
             switch (levels[i].group)
             {
@@ -573,6 +573,7 @@ private class KlotskiWindow : ApplicationWindow
                                      2, i,
                                      3, false);
                 break;
+            default: assert_not_reached ();
             }
         }
 
@@ -802,9 +803,9 @@ private class KlotskiWindow : ApplicationWindow
     private void game_score ()
     {
         /* Level is complete */
-        var key = get_level_key (current_level);
-        var keyfile = new KeyFile ();
-        var filename = Path.build_filename (Environment.get_user_data_dir (), "gnome-klotski", "levels");  
// filename:~/.local/share/gnome-klotski/levels
+        string key = get_level_key (current_level);
+        KeyFile keyfile = new KeyFile ();
+        string filename = Path.build_filename (Environment.get_user_data_dir (), "gnome-klotski", "levels"); 
 // filename:~/.local/share/gnome-klotski/levels
 
         try
         {
@@ -850,11 +851,11 @@ private class KlotskiWindow : ApplicationWindow
     {
         /* Calculate the CRC of the level data */
         uint32 result = 0xFFFFFFFFu;
-        var data = levels[level_number].data;
-        for (var i = 0; data[i] != '\0'; i++)
+        string data = levels [level_number].data;
+        for (uint i = 0; data [i] != '\0'; i++)
         {
-            var octet = data[i];
-            for (var j = 0; j < 8; j++)
+            char octet = data [i];
+            for (uint8 j = 0; j < 8; j++)
             {
                 if (((octet >> 7) ^ (result >> 31)) != 0)
                     result = (result << 1) ^ 0x04c11db7;
@@ -870,8 +871,8 @@ private class KlotskiWindow : ApplicationWindow
 
     private void load_solved_state ()
     {
-        var keyfile = new KeyFile ();
-        var filename = Path.build_filename (Environment.get_user_data_dir (), "gnome-klotski", "levels");
+        KeyFile keyfile = new KeyFile ();
+        string filename = Path.build_filename (Environment.get_user_data_dir (), "gnome-klotski", "levels");
         try
         {
             keyfile.load_from_file (filename, KeyFileFlags.NONE);
@@ -880,19 +881,19 @@ private class KlotskiWindow : ApplicationWindow
         {
         }
 
-        for (var i = 0; i < levels.length; i++)
+        for (uint8 i = 0; i < levels.length; i++)
         {
-            var key = get_level_key (i);
-            var value = false;
+            string key = get_level_key (i);
+            bool is_solved = false;
             try
             {
-                value = keyfile.get_boolean (key, "solved");
+                is_solved = keyfile.get_boolean (key, "solved");
             }
             catch (Error e)
             {
             }
 
-            puzzle_solved (puzzles_items[i], value);
+            puzzle_solved (puzzles_items [i], is_solved);
         }
     }
 
diff --git a/src/puzzle-view.vala b/src/puzzle-view.vala
index 605a634..5df279b 100644
--- a/src/puzzle-view.vala
+++ b/src/puzzle-view.vala
@@ -28,8 +28,8 @@ private class PuzzleView : Gtk.DrawingArea
 
     private int render_size = 0;
 
-    private int piece_x = 0;
-    private int piece_y = 0;
+    private uint8 piece_x = 0;
+    private uint8 piece_y = 0;
     private bool piece_unmoved = false;
 
     private char _piece_id = '\0';
@@ -153,8 +153,8 @@ private class PuzzleView : Gtk.DrawingArea
         cr.rectangle (kx, ky, kwidth, kheight);
         cr.stroke ();
 
-        for (var y = 0; y < puzzle.height; y++)
-            for (var x = 0; x < puzzle.width; x++)
+        for (uint8 y = 0; y < puzzle.height; y++)
+            for (uint8 x = 0; x < puzzle.width; x++)
             {
                 draw_square (cr, x, y, kx, ky);
 
@@ -169,7 +169,7 @@ private class PuzzleView : Gtk.DrawingArea
         return false;
     }
 
-    private void draw_square (Cairo.Context cr, int x, int y, double kx, double ky)
+    private void draw_square (Cairo.Context cr, uint8 x, uint8 y, double kx, double ky)
     {
         var rect = Gdk.Rectangle ();
         rect.x = x * tile_size + SPACE_OFFSET + (int)kx - 1;
@@ -219,8 +219,13 @@ private class PuzzleView : Gtk.DrawingArea
             if (puzzle.game_over ())
                 return false;
 
-            piece_x = (int) (event.x - kx) / tile_size;
-            piece_y = (int) (event.y - ky) / tile_size;
+            int new_piece_x = (int) (event.x - kx) / tile_size;
+            int new_piece_y = (int) (event.y - ky) / tile_size;
+            if (new_piece_x < 0 || new_piece_x >= (int) puzzle.width
+             || new_piece_y < 0 || new_piece_y >= (int) puzzle.height)
+                return false;
+            piece_x = (uint8) new_piece_x;
+            piece_y = (uint8) new_piece_y;
             char new_piece_id = puzzle.get_piece_id (puzzle.map, piece_x, piece_y);
 
             if (piece_id != '\0' && piece_unmoved)
@@ -276,19 +281,18 @@ private class PuzzleView : Gtk.DrawingArea
 
     protected override bool motion_notify_event (Gdk.EventMotion event)
     {
-        int new_piece_x, new_piece_y;
-
         if (piece_id != '\0')
         {
-            new_piece_x = (int) (event.x - kx) / tile_size;
-            new_piece_y = (int) (event.y - ky) / tile_size;
-            if (new_piece_x >= puzzle.width || event.x < 0 || new_piece_y >= puzzle.height || event.y < 0)
+            int new_piece_x = (int) (event.x - kx) / tile_size;
+            int new_piece_y = (int) (event.y - ky) / tile_size;
+            if (event.x < 0 || new_piece_x > (int) puzzle.width
+             || event.y < 0 || new_piece_y > (int) puzzle.height)
                 return false;
-            if (puzzle.move_piece (piece_id, piece_x, piece_y, new_piece_x, new_piece_y))
+            if (puzzle.move_piece (piece_id, piece_x, piece_y, (uint8) new_piece_x, (uint8) new_piece_y))
             {
                 piece_unmoved = false;
-                piece_x = new_piece_x;
-                piece_y = new_piece_y;
+                piece_x = (uint8) new_piece_x;
+                piece_y = (uint8) new_piece_y;
             }
             return true;
         }
diff --git a/src/puzzle.vala b/src/puzzle.vala
index 2dd8ac8..2e38ad0 100644
--- a/src/puzzle.vala
+++ b/src/puzzle.vala
@@ -102,8 +102,8 @@ private class Puzzle : Object
         -1,  -1
     };
 
-    [CCode (notify = false)] internal int width     { internal get; private set; }
-    [CCode (notify = false)] internal int height    { internal get; private set; }
+    [CCode (notify = false)] internal uint8 width   { internal get; private set; }
+    [CCode (notify = false)] internal uint8 height  { internal get; private set; }
 
     // Type `char []' can not be used for a GLib.Object property
     internal char [] map;
@@ -117,7 +117,7 @@ private class Puzzle : Object
     internal signal void changed ();
     internal signal void moved ();
 
-    internal Puzzle (int width, int height, string? data)
+    internal Puzzle (uint8 width, uint8 height, string? data)
     {
         this.width = width;
         this.height = height;
@@ -126,12 +126,12 @@ private class Puzzle : Object
         undomove_map = map;
         if (data != null)
         {
-            var i = 0;
-            for (var y = 0; y < height; y++)
+            uint16 i = 0;
+            for (uint8 y = 0; y < height; y++)
             {
-                for (var x = 0; x < width; x++)
+                for (uint8 x = 0; x < width; x++)
                 {
-                    set_piece_id (map, x, y, data[i]);
+                    set_piece_id (map, x, y, data [i]);
                     i++;
                 }
             }
@@ -140,22 +140,22 @@ private class Puzzle : Object
         lastmove_map = map;
     }
 
-    internal char get_piece_id (char[] src, int x, int y)
+    internal char get_piece_id (char [] src, uint8 x, uint8 y)
     {
-        return src[x + 1 + (y + 1) * (width + 2)];
+        return src [(uint16) x + 1 + ((uint16) y + 1) * ((uint16) width + 2)];
     }
 
-    private void set_piece_id (char[] src, int x, int y, char id)
+    private void set_piece_id (char [] src, uint8 x, uint8 y, char id)
     {
-        src[x + 1 + (y + 1) * (width + 2)] = id;
+        src [(uint16) x + 1 + ((uint16) y + 1) * ((uint16) width + 2)] = id;
     }
 
-    internal int get_piece_nr (int x, int y)
+    internal int get_piece_nr (uint8 x, uint8 y)
     {
         x++;
         y++;
 
-        var c = map[x + y * (width + 2)];
+        var c = map [(uint16) x + (uint16) y * ((uint16) width + 2)];
         if (c == '-')
             return 23;
         if (c == ' ')
@@ -163,45 +163,45 @@ private class Puzzle : Object
         if (c == '.')
             return 20;
 
-        var nr = 0;
-        if (map[(x - 1) + (y - 1) * (width + 2)] == c)
+        uint8 nr = 0;
+        if (map [((uint16) x - 1) + ((uint16) y - 1) * ((uint16) width + 2)] == c)
             nr += 1;
-        if (map[(x - 0) + (y - 1) * (width + 2)] == c)
+        if (map [((uint16) x    ) + ((uint16) y - 1) * ((uint16) width + 2)] == c)
             nr += 2;
-        if (map[(x + 1) + (y - 1) * (width + 2)] == c)
+        if (map [((uint16) x + 1) + ((uint16) y - 1) * ((uint16) width + 2)] == c)
             nr += 4;
-        if (map[(x - 1) + (y - 0) * (width + 2)] == c)
+        if (map [((uint16) x - 1) + ((uint16) y    ) * ((uint16) width + 2)] == c)
             nr += 8;
-        if (map[(x + 1) + (y - 0) * (width + 2)] == c)
+        if (map [((uint16) x + 1) + ((uint16) y    ) * ((uint16) width + 2)] == c)
             nr += 16;
-        if (map[(x - 1) + (y + 1) * (width + 2)] == c)
+        if (map [((uint16) x - 1) + ((uint16) y + 1) * ((uint16) width + 2)] == c)
             nr += 32;
-        if (map[(x - 0) + (y + 1) * (width + 2)] == c)
+        if (map [((uint16) x    ) + ((uint16) y + 1) * ((uint16) width + 2)] == c)
             nr += 64;
-        if (map[(x + 1) + (y + 1) * (width + 2)] == c)
+        if (map [((uint16) x + 1) + ((uint16) y + 1) * ((uint16) width + 2)] == c)
             nr += 128;
 
-        var i = 0;
-        while (nr != image_map[i] && image_map[i] != -1)
+        uint8 i = 0;
+        while (nr != image_map [i] && image_map [i] != -1)
             i += 2;
 
-        return image_map[i + 1];
+        return image_map [i + 1];
     }
 
     internal bool game_over ()
     {
-        var over = true;
-        for (var y = 0; y < height; y++)
-            for (var x = 0; x < width; x++)
+        bool over = true;
+        for (uint8 y = 0; y < height; y++)
+            for (uint8 x = 0; x < width; x++)
                 if (get_piece_id (map, x, y) == '*' && get_piece_id (orig_map, x, y) != '.')
-                    over = false;
+                    over = false;   // TODO return false?
         return over;
     }
 
-    internal bool mapcmp (char[] m1, char[] m2)
+    internal bool mapcmp (char [] m1, char [] m2)
     {
-        for (var y = 0; y < height; y++)
-            for (var x = 0; x < width; x++)
+        for (uint8 y = 0; y < height; y++)
+            for (uint8 x = 0; x < width; x++)
                 if (get_piece_id (m1, x, y) != get_piece_id (m2, x, y))
                     return true;
         return false;
@@ -214,7 +214,7 @@ private class Puzzle : Object
         return true;
     }
 
-    internal bool move_piece (char id, int x1, int y1, int x2, int y2)
+    internal bool move_piece (char id, uint8 x1, uint8 y1, uint8 x2, uint8 y2)
     {
         var return_value = false;
 
@@ -224,27 +224,35 @@ private class Puzzle : Object
         if (get_piece_id (map, x2, y2) == id)
             return_value = true;
 
-        if (!((y1 == y2 && (x1 - x2).abs () == 1) || (x1 == x2 && (y1 - y2).abs () == 1)))
+        if (!((y1 == y2 && ((int) x1 - (int) x2).abs () == 1) || (x1 == x2 && ((int) y1 - (int) y2).abs () 
== 1)))
             return false;
 
-        if ((y1 - y2).abs () == 1)
+        if (((int) y1 - (int) y2).abs () == 1)
         {
-            if (y1 - y2 < 0)
+            if (y1 < y2)
+            {
                 if (check_valid_move (id, 0, 1))
                     return do_move_piece (id, 0, 1);
-            if (y1 - y2 > 0)
+            }
+            else if (y1 > y2)
+            {
                 if (check_valid_move (id, 0, -1))
                     return do_move_piece (id, 0, -1);
+            }
         }
 
-        if ((x1 - x2).abs () == 1)
+        if (((int) x1 - (int) x2).abs () == 1)
         {
-            if (x1 - x2 < 0)
+            if (x1 < x2)
+            {
                 if (check_valid_move (id, 1, 0))
                     return do_move_piece (id, 1, 0);
-            if (x1 - x2 > 0)
+            }
+            else if (x1 > x2)
+            {
                 if (check_valid_move (id, -1, 0))
                     return do_move_piece (id, -1, 0);
+            }
         }
 
         return return_value;


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