[four-in-a-row] Do not mix enums.



commit a0ce2fa5f0e1d440a15f068ee7aa7c4b50396581
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Mon Dec 23 03:22:26 2019 +0100

    Do not mix enums.

 src/four-in-a-row.vala   | 43 ++++++++++++++++---------------------------
 src/game-board-view.vala | 25 +++++++++++++++++--------
 src/game-board.vala      | 38 +++++++++++++++++++-------------------
 3 files changed, 52 insertions(+), 54 deletions(-)
---
diff --git a/src/four-in-a-row.vala b/src/four-in-a-row.vala
index 18f3395..60e5298 100644
--- a/src/four-in-a-row.vala
+++ b/src/four-in-a-row.vala
@@ -86,7 +86,7 @@ private class FourInARow : Gtk.Application
     private static AnimID anim = AnimID.NONE;
     private uint8 [,] blink_lines = {{}};
     private uint8 blink_line = 0;   // index of currenly blinking line in blink_lines
-    private Tile blink_t = Tile.PLAYER1;    // garbage
+    private PlayerID blink_t = PlayerID.NOBODY;    // garbage
     private uint8 blink_n = 0;
     private bool blink_on = false;
     private uint timeout = 0;
@@ -365,12 +365,12 @@ private class FourInARow : Gtk.Application
     private void blink_winner (uint8 n)   /* blink the winner's line(s) n times */
      // requires (n < 128)
     {
-        if (winner == NOBODY)
+        if (winner == PlayerID.NOBODY)
             return;
 
-        blink_t = (Tile) winner;    // FIXME converting a PlayerID in Tile, bad
+        blink_t = winner;
 
-        if (game_board.is_line_at ((Tile) winner, row, column, out blink_lines))
+        if (game_board.is_line_at (winner, row, column, out blink_lines))
         {
             anim = AnimID.BLINK;
             blink_on = false;
@@ -383,7 +383,7 @@ private class FourInARow : Gtk.Application
         }
     }
 
-    private inline void draw_line (uint8 _r1, uint8 _c1, uint8 _r2, uint8 _c2, int tile)
+    private inline void draw_line (uint8 _r1, uint8 _c1, uint8 _r2, uint8 _c2, PlayerID owner)
     {
         /* draw a line of 'tile' from r1,c1 to r2,c2 */
 
@@ -407,7 +407,7 @@ private class FourInARow : Gtk.Application
         do
         {
             done = (r1 == r2 && c1 == c2);
-            game_board [r1, c1] = (Tile) tile;
+            game_board [r1, c1] = owner;
             game_board_view.draw_tile (r1, c1);
             if (r1 != r2)
                 r1 += d_row;
@@ -519,7 +519,7 @@ private class FourInARow : Gtk.Application
     }
     private inline void check_game_state ()
     {
-        if (game_board.is_line_at ((Tile) player, row, column))
+        if (game_board.is_line_at (player, row, column))
         {
             gameover = true;
             winner = player;
@@ -577,9 +577,9 @@ private class FourInARow : Gtk.Application
 
     private inline void drop ()
     {
-        Tile tile = player == PLAYER1 ? Tile.PLAYER1 : Tile.PLAYER2;
+        PlayerID tile = player == PlayerID.PLAYER1 ? PlayerID.PLAYER1 : PlayerID.PLAYER2;
 
-        game_board [row, column] = Tile.CLEAR;
+        game_board [row, column] = PlayerID.NOBODY;
         game_board_view.draw_tile (row, column);
 
         row++;
@@ -589,11 +589,11 @@ private class FourInARow : Gtk.Application
 
     private inline void move (uint8 c)
     {
-        game_board [0, column] = Tile.CLEAR;
+        game_board [0, column] = PlayerID.NOBODY;
         game_board_view.draw_tile (0, column);
 
         column = c;
-        game_board [0, c] = player == PlayerID.PLAYER1 ? Tile.PLAYER1 : Tile.PLAYER2;
+        game_board [0, c] = player == PlayerID.PLAYER1 ? PlayerID.PLAYER1 : PlayerID.PLAYER2;
 
         game_board_view.draw_tile (0, c);
     }
@@ -649,7 +649,7 @@ private class FourInARow : Gtk.Application
         moves = 0;
     }
 
-    private inline void blink_tile (uint8 row, uint8 col, Tile tile, uint8 n)
+    private inline void blink_tile (uint8 row, uint8 col, PlayerID tile, uint8 n)
     {
         if (timeout != 0)
             return;
@@ -720,7 +720,7 @@ private class FourInARow : Gtk.Application
                                            /* col 1 */ application.blink_lines [application.blink_line, 1],
                                            /* row 2 */ application.blink_lines [application.blink_line, 2],
                                            /* col 2 */ application.blink_lines [application.blink_line, 3],
-                                           /* tile */  application.blink_on ? application.blink_t : 
Tile.CLEAR);
+                                           /* tile */  application.blink_on ? application.blink_t : 
PlayerID.NOBODY);
                     application.blink_n--;
                     if (application.blink_n == 0 && application.blink_on)
                     {
@@ -817,7 +817,7 @@ private class FourInARow : Gtk.Application
             swap_player ();
         move_cursor (c);
 
-        game_board [r, c] = Tile.CLEAR;
+        game_board [r, c] = PlayerID.NOBODY;
         game_board_view.draw_tile (r, c);
 
         if (one_player_game
@@ -831,7 +831,7 @@ private class FourInARow : Gtk.Application
             moves--;
             swap_player ();
             move_cursor (c);
-            game_board [r, c] = Tile.CLEAR;
+            game_board [r, c] = PlayerID.NOBODY;
             game_board_view.draw_tile (r, c);
         }
     }
@@ -1110,19 +1110,8 @@ private class FourInARow : Gtk.Application
     }
 }
 
-// WARNING PlayerID and Tile should be synced
 private enum PlayerID {
-    PLAYER1 = 0,
+    PLAYER1,
     PLAYER2,
     NOBODY;
 }
-
-// WARNING PlayerID and Tile should be synced
-private enum Tile {
-    PLAYER1 = 0,
-    PLAYER2,
-    CLEAR,
-    CLEAR_CURSOR,
-    PLAYER1_CURSOR,
-    PLAYER2_CURSOR;
-}
diff --git a/src/game-board-view.vala b/src/game-board-view.vala
index 285c63a..46d3b43 100644
--- a/src/game-board-view.vala
+++ b/src/game-board-view.vala
@@ -20,6 +20,15 @@
 
 private class GameBoardView : Gtk.DrawingArea
 {
+    private enum Tile {
+        PLAYER1,
+        PLAYER2,
+        CLEAR,
+        CLEAR_CURSOR,
+        PLAYER1_CURSOR,
+        PLAYER2_CURSOR;
+    }
+
     [CCode (notify = false)] public Board game_board { private get; protected construct; }
 
     private int _theme_id = 0;
@@ -115,24 +124,24 @@ private class GameBoardView : Gtk.DrawingArea
 
     private inline void paint_tile (Cairo.Context cr, uint8 row, uint8 col)
     {
-        int tile = game_board [row, col];
-        if (tile == Tile.CLEAR && row != 0)
+        PlayerID tile = game_board [row, col];
+        if (tile == PlayerID.NOBODY && row != 0)
             return;
 
         int os = 0;
         if (row == 0)
             switch (tile)
             {
-                case Tile.PLAYER1 : os = offset [Tile.PLAYER1_CURSOR]; break;
-                case Tile.PLAYER2 : os = offset [Tile.PLAYER2_CURSOR]; break;
-                case Tile.CLEAR   : os = offset [Tile.CLEAR_CURSOR];   break;
+                case PlayerID.PLAYER1 : os = offset [Tile.PLAYER1_CURSOR]; break;
+                case PlayerID.PLAYER2 : os = offset [Tile.PLAYER2_CURSOR]; break;
+                case PlayerID.NOBODY  : os = offset [Tile.CLEAR_CURSOR];   break;
             }
         else
             switch (tile)
             {
-                case Tile.PLAYER1 : os = offset [Tile.PLAYER1]; break;
-                case Tile.PLAYER2 : os = offset [Tile.PLAYER2]; break;
-                case Tile.CLEAR   : assert_not_reached ();
+                case PlayerID.PLAYER1 : os = offset [Tile.PLAYER1]; break;
+                case PlayerID.PLAYER2 : os = offset [Tile.PLAYER2]; break;
+                case PlayerID.NOBODY  : assert_not_reached ();
             }
 
         cr.save ();
diff --git a/src/game-board.vala b/src/game-board.vala
index 0ec7350..b5bf172 100644
--- a/src/game-board.vala
+++ b/src/game-board.vala
@@ -20,19 +20,19 @@
 
 private class Board : Object
 {
-    private static Tile [,] gboard;
+    private static PlayerID [,] gboard;
 
     internal Board ()
     {
-        gboard = new Tile [BOARD_COLUMNS, BOARD_ROWS_PLUS_ONE];
+        gboard = new PlayerID [BOARD_COLUMNS, BOARD_ROWS_PLUS_ONE];
     }
 
-    internal new void @set (uint8 x, uint8 y, Tile tile)
+    internal new void @set (uint8 x, uint8 y, PlayerID tile)
     {
         gboard [x, y] = tile;
     }
 
-    internal new Tile @get (uint8 x, uint8 y)
+    internal new PlayerID @get (uint8 x, uint8 y)
     {
         return gboard [x, y];
     }
@@ -41,14 +41,14 @@ private class Board : Object
     {
         for (uint8 row = 0; row < BOARD_ROWS_PLUS_ONE; row++)
             for (uint8 col = 0; col < BOARD_COLUMNS; col++)
-                gboard [row, col] = Tile.CLEAR;
+                gboard [row, col] = PlayerID.NOBODY;
     }
 
     internal uint8 first_empty_row (uint8 col)
     {
         uint8 row = 1;
 
-        while (row < BOARD_ROWS_PLUS_ONE && gboard [row, col] == Tile.CLEAR)
+        while (row < BOARD_ROWS_PLUS_ONE && gboard [row, col] == PlayerID.NOBODY)
             row++;
         return row - 1;
     }
@@ -57,7 +57,7 @@ private class Board : Object
     * * check if there is a line passing by a given point
     \*/
 
-    internal bool is_line_at (Tile tile, uint8 row, uint8 col, out uint8 [,] lines = null)
+    internal bool is_line_at (PlayerID tile, uint8 row, uint8 col, out uint8 [,] lines = null)
     {
         uint8 n_lines = 0;
         uint8 [,] lines_tmp = new uint8 [4, 4];
@@ -90,9 +90,9 @@ private class Board : Object
         return n_lines != 0;
     }
 
-    private inline bool is_hline_at (Tile tile,     uint8 row,       uint8 col,
-                                                out uint8 row_1, out uint8 col_1,
-                                                out uint8 row_2, out uint8 col_2)
+    private inline bool is_hline_at (PlayerID tile,     uint8 row,       uint8 col,
+                                                    out uint8 row_1, out uint8 col_1,
+                                                    out uint8 row_2, out uint8 col_2)
     {
         row_1 = row;
         row_2 = row;
@@ -107,9 +107,9 @@ private class Board : Object
         return false;
     }
 
-    private inline bool is_vline_at (Tile tile,     uint8 row,       uint8 col,
-                                                out uint8 row_1, out uint8 col_1,
-                                                out uint8 row_2, out uint8 col_2)
+    private inline bool is_vline_at (PlayerID tile,     uint8 row,       uint8 col,
+                                                    out uint8 row_1, out uint8 col_1,
+                                                    out uint8 row_2, out uint8 col_2)
     {
         row_1 = row;
         row_2 = row;
@@ -122,9 +122,9 @@ private class Board : Object
         return row_2 - row_1 >= 3;
     }
 
-    private inline bool is_dline1_at (Tile tile,     uint8 row,       uint8 col,
-                                                 out uint8 row_1, out uint8 col_1,
-                                                 out uint8 row_2, out uint8 col_2)
+    private inline bool is_dline1_at (PlayerID tile,     uint8 row,       uint8 col,
+                                                     out uint8 row_1, out uint8 col_1,
+                                                     out uint8 row_2, out uint8 col_2)
     {
         /* upper left to lower right */
         row_1 = row;
@@ -144,9 +144,9 @@ private class Board : Object
         return row_2 - row_1 >= 3;
     }
 
-    private inline bool is_dline2_at (Tile tile,     uint8 row,       uint8 col,
-                                                 out uint8 row_1, out uint8 col_1,
-                                                 out uint8 row_2, out uint8 col_2)
+    private inline bool is_dline2_at (PlayerID tile,     uint8 row,       uint8 col,
+                                                     out uint8 row_1, out uint8 col_1,
+                                                     out uint8 row_2, out uint8 col_2)
     {
         /* upper right to lower left */
         row_1 = row;


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