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_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)
dvonn_bool_t
dvonn_board_cell_surrounded (dvonn_board_t *board,
int x, int y)
- 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;
}
*error = "You cannot move your opponent's stack";
return FALSE;
}
dvonn_board_cell_occupied (dvonn_board_t *board,
int x, int y);
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
/* 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
}
if (! game->has_selected) {
}
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;
! dvonn_board_cell_surrounded (&game->board, x, y))
{
game->has_selected = TRUE;