]> git.cworth.org Git - dvonn/commitdiff
Eliminate stacks disconnected from red DVONN pieces
authorCarl Worth <cworth@cworth.org>
Thu, 5 Mar 2009 18:03:13 +0000 (10:03 -0800)
committerCarl Worth <cworth@cworth.org>
Thu, 5 Mar 2009 18:03:13 +0000 (10:03 -0800)
At this point, the game is actually playable for the first time.
There's still very little actual UI here, (no indication of who
the current player is, no buttons for passing, starting a new
game, etc.). But with two people who know how to right-click
and have out-of-band communication to say when its the
opponent's turn, it's possible to play now.

dvonn-board.c

index 4aaac54aadc19bcb65f3a5f360c967206e417e51..5b7c106c311909b30a47d96dc7ff3c6e7ecaf418 100644 (file)
@@ -239,6 +239,49 @@ dvonn_board_place (dvonn_board_t *board,
     return TRUE;
 }
 
+static void
+fill_living(dvonn_board_t *board,
+           int living[DVONN_BOARD_X_SIZE][DVONN_BOARD_Y_SIZE],
+           int x, int y)
+{
+    if (dvonn_board_cell_occupied (board, x, y) && ! living[x][y])
+    {
+       living[x][y] = 1;
+
+       fill_living (board, living, x - 1, y);
+       fill_living (board, living, x + 1, y);
+       fill_living (board, living, x, y - 1);
+       fill_living (board, living, x, y + 1);
+       fill_living (board, living, x + 1, y - 1);
+       fill_living (board, living, x - 1, y + 1);
+    }
+}
+
+static void
+eliminate_disconnected_stacks (dvonn_board_t *board)
+{
+    int x, y;
+    int living[DVONN_BOARD_X_SIZE][DVONN_BOARD_Y_SIZE];
+
+    for (x = 0; x < DVONN_BOARD_X_SIZE; x++)
+       for (y = 0; y < DVONN_BOARD_Y_SIZE; y++)
+           living[x][y] = 0;
+
+    for (x = 0; x < DVONN_BOARD_X_SIZE; x++)
+       for (y = 0; y < DVONN_BOARD_Y_SIZE; y++)
+           if (board->cells[x][y].contains_red)
+               fill_living (board, living, x, y);
+
+    for (x = 0; x < DVONN_BOARD_X_SIZE; x++)
+       for (y = 0; y < DVONN_BOARD_Y_SIZE; y++)
+           if (dvonn_board_cell_occupied (board, x, y) &&
+               ! living[x][y])
+           {
+               board->cells[x][y].type = DVONN_CELL_EMPTY;
+               board->cells[x][y].height = 0;
+           }
+}
+
 int
 dvonn_board_move (dvonn_board_t *board,
                  int x1, int y1,
@@ -261,6 +304,8 @@ dvonn_board_move (dvonn_board_t *board,
     board->cells[x1][y1].height = 0;
     board->cells[x1][y1].contains_red = FALSE;
 
+    eliminate_disconnected_stacks (board);
+
     dvonn_board_next_player (board);
 
     return TRUE;