[four-in-a-row] Make more generic.



commit b5472d6e05d45c1d6eb2f72c138fe5bcd831a7f8
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Mon Dec 23 02:35:47 2019 +0100

    Make more generic.

 src/four-in-a-row.vala   | 16 +++++++++++-----
 src/game-board-view.vala | 20 ++++++++++----------
 src/game-board.vala      | 19 +++++++++----------
 3 files changed, 30 insertions(+), 25 deletions(-)
---
diff --git a/src/four-in-a-row.vala b/src/four-in-a-row.vala
index f1386c7..18f3395 100644
--- a/src/four-in-a-row.vala
+++ b/src/four-in-a-row.vala
@@ -21,6 +21,12 @@
 
 using Gtk;
 
+private const uint8 BOARD_COLUMNS = 7;
+private const uint8 BOARD_COLUMNS_MINUS_ONE = 6;
+private const uint8 BOARD_ROWS = 6;
+private const uint8 BOARD_ROWS_PLUS_ONE = 7;
+private const uint8 BOARD_SIZE = 7; // as long as that is needed, impossible to have n_rows != n_cols - 1
+
 private class FourInARow : Gtk.Application
 {
     private GLib.Settings settings = new GLib.Settings ("org.gnome.Four-in-a-row");
@@ -345,7 +351,7 @@ private class FourInARow : Gtk.Application
             vstr [0] = vlevel [ai_level];
             playgame_timeout = Timeout.add (COMPUTER_INITIAL_DELAY, () => {
                     int c = playgame ((string) vstr) - 1;
-                    if (c > 6)
+                    if (c > BOARD_COLUMNS_MINUS_ONE)
                         assert_not_reached ();
                     if (c < 0)
                         return Source.REMOVE;
@@ -501,7 +507,7 @@ private class FourInARow : Gtk.Application
                         int col = playgame ((string) vstr) - 1;
                         if (col < 0)
                             gameover = true;
-                        else if (col > 6)
+                        else if (col > BOARD_COLUMNS_MINUS_ONE)
                             assert_not_reached ();
                         var nm = new NextMove ((uint8) col, this);
                         Timeout.add (SPEED_DROP, nm.exec);
@@ -766,7 +772,7 @@ private class FourInARow : Gtk.Application
 
         vstr [0] = vlevel [/* strong */ 3];
         int _c = playgame ((string) vstr) - 1;
-        if (_c < 0 || _c > 6)
+        if (_c < 0 || _c > BOARD_COLUMNS_MINUS_ONE)
             assert_not_reached ();
         c = (uint8) _c;
 
@@ -777,7 +783,7 @@ private class FourInARow : Gtk.Application
         var temp = new Animate (0, this);
         timeout = Timeout.add (SPEED_MOVE, temp.exec);
 
-        blink_tile (0, c, game_board [0, c], 3);
+        blink_tile (0, c, game_board [0, c], /* blink n times */ 3);
 
         /* Translators: text displayed in the headerbar/actionbar, when a hint is requested; the %d is 
replaced by the number of the suggested column */
         s = _("Hint: Column %d").printf (c + 1);
@@ -915,7 +921,7 @@ private class FourInARow : Gtk.Application
         }
         else if (key == "Right" || event.keyval == keypress_right)
         {
-            if (column >= 6)
+            if (column >= BOARD_COLUMNS_MINUS_ONE)
                 return false;
             column_moveto++;
             move_cursor (column_moveto);
diff --git a/src/game-board-view.vala b/src/game-board-view.vala
index 62a2177..285c63a 100644
--- a/src/game-board-view.vala
+++ b/src/game-board-view.vala
@@ -69,8 +69,8 @@ private class GameBoardView : Gtk.DrawingArea
         int allocated_width  = get_allocated_width ();
         int allocated_height = get_allocated_height ();
         int size = int.min (allocated_width, allocated_height);
-        tile_size = size / 7;
-        board_size = tile_size * 7;
+        tile_size = size / BOARD_SIZE;
+        board_size = tile_size * BOARD_SIZE;
         board_x = (allocated_width  - board_size) / 2;
         board_y = (allocated_height - board_size) / 2;
 
@@ -100,8 +100,8 @@ private class GameBoardView : Gtk.DrawingArea
         cr.restore ();
 
         /* tiles */
-        for (uint8 row = 0; row < 7; row++)
-            for (uint8 col = 0; col < 7; col++)
+        for (uint8 row = 0; row < BOARD_ROWS_PLUS_ONE; row++)
+            for (uint8 col = 0; col < BOARD_COLUMNS; col++)
                 paint_tile (cr, row, col);
 
         /* grid */
@@ -160,7 +160,7 @@ private class GameBoardView : Gtk.DrawingArea
         cr.set_dash (dashes, /* offset */ 0.0);
 
         /* draw the grid on the background pixmap */
-        for (uint8 i = 1; i < 7; i++)
+        for (uint8 i = 1; i < BOARD_SIZE; i++)
         {
             double line_offset = i * tile_size + 0.5;
             // vertical lines
@@ -244,15 +244,15 @@ private class GameBoardView : Gtk.DrawingArea
     {
         int raw_tile_size = pb_tileset_raw.get_height ();
 
-        pb_bground_raw = new Gdk.Pixbuf (Gdk.Colorspace.RGB, /* alpha */ true, /* bits per sample */ 8, 
raw_tile_size * 7, raw_tile_size * 7);
-        for (int i = 0; i < 7; i++)
+        pb_bground_raw = new Gdk.Pixbuf (Gdk.Colorspace.RGB, /* alpha */ true, /* bits per sample */ 8, 
raw_tile_size * BOARD_COLUMNS, raw_tile_size * BOARD_ROWS_PLUS_ONE);
+        for (int i = 0; i < BOARD_COLUMNS; i++)
         {
             pb_tileset_raw.copy_area (raw_tile_size * 3, 0,
                                       raw_tile_size, raw_tile_size,
                                       pb_bground_raw,
                                       i * raw_tile_size, 0);
 
-            for (int j = 1; j < 7; j++)
+            for (int j = 1; j < BOARD_ROWS_PLUS_ONE; j++)
                 pb_tileset_raw.copy_area (raw_tile_size * 2, 0,
                                           raw_tile_size, raw_tile_size,
                                           pb_bground_raw,
@@ -294,7 +294,7 @@ private class GameBoardView : Gtk.DrawingArea
     private inline bool get_column (int x, int y, out uint8 col)
     {
         int _col = (x - board_x) / tile_size;
-        if (x < board_x || y < board_y || _col < 0 || _col > 6)
+        if (x < board_x || y < board_y || _col < 0 || _col > BOARD_COLUMNS_MINUS_ONE)
         {
             col = 0;
             return false;
@@ -302,7 +302,7 @@ private class GameBoardView : Gtk.DrawingArea
         col = (uint8) _col;
 
         int row = (y - board_y) / tile_size;
-        if (row < 0 || row > 6)
+        if (row < 0 || row > BOARD_ROWS)
             return false;
 
         return true;
diff --git a/src/game-board.vala b/src/game-board.vala
index 699a851..0ec7350 100644
--- a/src/game-board.vala
+++ b/src/game-board.vala
@@ -21,11 +21,10 @@
 private class Board : Object
 {
     private static Tile [,] gboard;
-    private const uint8 BOARD_SIZE = 7;
 
     internal Board ()
     {
-        gboard = new Tile [BOARD_SIZE, BOARD_SIZE];
+        gboard = new Tile [BOARD_COLUMNS, BOARD_ROWS_PLUS_ONE];
     }
 
     internal new void @set (uint8 x, uint8 y, Tile tile)
@@ -40,8 +39,8 @@ private class Board : Object
 
     internal void clear ()
     {
-        for (uint8 row = 0; row < BOARD_SIZE; row++)
-            for (uint8 col = 0; col < BOARD_SIZE; col++)
+        for (uint8 row = 0; row < BOARD_ROWS_PLUS_ONE; row++)
+            for (uint8 col = 0; col < BOARD_COLUMNS; col++)
                 gboard [row, col] = Tile.CLEAR;
     }
 
@@ -49,7 +48,7 @@ private class Board : Object
     {
         uint8 row = 1;
 
-        while (row < BOARD_SIZE && gboard [row, col] == Tile.CLEAR)
+        while (row < BOARD_ROWS_PLUS_ONE && gboard [row, col] == Tile.CLEAR)
             row++;
         return row - 1;
     }
@@ -101,7 +100,7 @@ private class Board : Object
         col_2 = col;
         while (col_1 > 0 && gboard [row, col_1 - 1] == tile)
             col_1 = col_1 - 1;
-        while (col_2 < 6 && gboard [row, col_2 + 1] == tile)
+        while (col_2 < BOARD_ROWS && gboard [row, col_2 + 1] == tile)
             col_2 = col_2 + 1;
         if (col_2 - col_1 >= 3)
             return true;
@@ -118,7 +117,7 @@ private class Board : Object
         col_2 = col;
         while (row_1 > 1 && gboard [row_1 - 1, col] == tile)
             row_1 = row_1 - 1;
-        while (row_2 < 6 && gboard [row_2 + 1, col] == tile)
+        while (row_2 < BOARD_ROWS && gboard [row_2 + 1, col] == tile)
             row_2 = row_2 + 1;
         return row_2 - row_1 >= 3;
     }
@@ -137,7 +136,7 @@ private class Board : Object
             row_1 = row_1 - 1;
             col_1 = col_1 - 1;
         }
-        while (col_2 < 6 && row_2 < 6 && gboard [row_2 + 1, col_2 + 1] == tile)
+        while (col_2 < BOARD_COLUMNS_MINUS_ONE && row_2 < BOARD_ROWS && gboard [row_2 + 1, col_2 + 1] == 
tile)
         {
             row_2 = row_2 + 1;
             col_2 = col_2 + 1;
@@ -154,12 +153,12 @@ private class Board : Object
         row_2 = row;
         col_1 = col;
         col_2 = col;
-        while (col_1 < 6 && row_1 > 1 && gboard [row_1 - 1, col_1 + 1] == tile)
+        while (col_1 < BOARD_COLUMNS_MINUS_ONE && row_1 > 1 && gboard [row_1 - 1, col_1 + 1] == tile)
         {
             row_1 = row_1 - 1;
             col_1 = col_1 + 1;
         }
-        while (col_2 > 0 && row_2 < 6 && gboard [row_2 + 1, col_2 - 1] == tile)
+        while (col_2 > 0 && row_2 < BOARD_ROWS && gboard [row_2 + 1, col_2 - 1] == tile)
         {
             row_2 = row_2 + 1;
             col_2 = col_2 - 1;


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