]> git.cworth.org Git - dvonn/commitdiff
Add a new dvonn_board_cell_owned_by function
authorCarl Worth <cworth@cworth.org>
Fri, 24 Apr 2020 21:27:24 +0000 (14:27 -0700)
committerCarl Worth <cworth@cworth.org>
Fri, 24 Apr 2020 21:27:24 +0000 (14:27 -0700)
The compiler was warning that we were comparing enums of two different
types. That's true—we are, but that's because the values for
dvonn_cell_t are declared by reference to dvonn_player_t values.

Anyway, rather than cluttering up the code with lots of casts to
silence this warning, here we add a new function that takes care of
that cast, leading to more readable code.

dvonn-board.c
dvonn-board.h
dvonn.c

index f8f9b93843556f3e20b482418c7ca75f768f28d2..eeab83076294649ebe0ccdaa846ca59307eef1e0 100644 (file)
@@ -101,6 +101,19 @@ dvonn_board_cell_occupied (dvonn_board_t *board,
     return TRUE;
 }
 
+dvonn_bool_t
+dvonn_board_cell_owned_by (dvonn_board_t *board,
+                          int x, int y,
+                          dvonn_player_t player)
+{
+    if (! dvonn_board_cell_occupied (board, x, y))
+       return FALSE;
+
+    /* Cast here to avoid compiler warning about mixing enum types in
+     * a comparison. */
+    return board->cells[x][y].type == (dvonn_cell_type_t) player;
+}
+
 dvonn_bool_t
 dvonn_board_cell_surrounded (dvonn_board_t *board,
                             int x, int y)
@@ -152,7 +165,7 @@ dvonn_board_move_legal (dvonn_board_t *board,
        return FALSE;
     }
 
-    if (board->cells[x1][y1].type != board->player) {
+    if (! dvonn_board_cell_owned_by (board, x1, y1, board->player)) {
        *error = "You cannot move your opponent's stack";
        return FALSE;
     }
index 7a361c3e6e3f3ebc2bdb415f9bae69352b096414..71923011e0ce07611de88a10454009e4d6f0137a 100644 (file)
@@ -100,6 +100,13 @@ dvonn_bool_t
 dvonn_board_cell_occupied (dvonn_board_t *board,
                           int x, int y);
 
+/* Is the cell at (x,y) occupied by a piece (or stack) with the given
+ * player's piece at the top. Returns FALSE for all out-of-bounds
+ * coordinates. */
+dvonn_bool_t
+dvonn_board_cell_owned_by (dvonn_board_t *board,
+                          int x, int y, dvonn_player_t player);
+
 /* Is the cell at (x,y) surrounded by other pieces, (such that it is
  * not legal for a piece at (x,y) to move. */
 dvonn_bool_t
diff --git a/dvonn.c b/dvonn.c
index 709d12a3b939aad5f07a6f645976b39cadf7b7c9..99cadf71d638af48d66f6fb8abf3bb3f9a29f21d 100644 (file)
--- a/dvonn.c
+++ b/dvonn.c
@@ -212,7 +212,7 @@ on_button_press_event (GtkWidget    *widget,
     }
 
     if (! game->has_selected) {
-       if (game->board.cells[x][y].type == game->board.player &&
+       if (dvonn_board_cell_owned_by (&game->board, x, y, game->board.player) &&
            ! dvonn_board_cell_surrounded (&game->board, x, y))
            {
                    game->has_selected = TRUE;