From 7e10cab22cd0cd20ff48bf5665f0f7536f1ad032 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Fri, 24 Apr 2020 14:27:24 -0700 Subject: [PATCH] Add a new dvonn_board_cell_owned_by function MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 | 15 ++++++++++++++- dvonn-board.h | 7 +++++++ dvonn.c | 2 +- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/dvonn-board.c b/dvonn-board.c index f8f9b93..eeab830 100644 --- a/dvonn-board.c +++ b/dvonn-board.c @@ -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; } diff --git a/dvonn-board.h b/dvonn-board.h index 7a361c3..7192301 100644 --- a/dvonn-board.h +++ b/dvonn-board.h @@ -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 709d12a..99cadf7 100644 --- 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; -- 2.43.0