]> git.cworth.org Git - dvonn/blobdiff - dvonn-board.c
Implement automatic pass (when there is no legal move) and end-of-game
[dvonn] / dvonn-board.c
index eeab83076294649ebe0ccdaa846ca59307eef1e0..c78905ee3910b521ae851dd49a9fa265ce9970e9 100644 (file)
@@ -80,6 +80,8 @@ dvonn_board_init (dvonn_board_t *board)
     board->phase = DVONN_PHASE_PLACEMENT;
     board->player = DVONN_PLAYER_WHITE;
     board->moves = 0;
+    board->score[DVONN_PLAYER_BLACK] = 0;
+    board->score[DVONN_PLAYER_WHITE] = 0;
 }
 
 dvonn_bool_t
@@ -196,24 +198,84 @@ dvonn_board_move_legal (dvonn_board_t *board,
     return TRUE;
 }
 
-static void
+static dvonn_bool_t
+dvonn_board_player_has_legal_move (dvonn_board_t *board)
+{
+    int x, y, height;
+    char *error;
+
+    for (x = 0; x < DVONN_BOARD_X_SIZE; x++) {
+       for (y = 0; y < DVONN_BOARD_X_SIZE; y++) {
+           if (! dvonn_board_cell_occupied (board, x, y))
+               continue;
+           if (! (board->cells[x][y].type == (dvonn_cell_type_t) board->player))
+               continue;
+           height = board->cells[x][y].height;
+           if (dvonn_board_move_legal (board, x, y,
+                                       x + height, y, &error))
+               return TRUE;
+           if (dvonn_board_move_legal (board, x, y,
+                                       x - height, y, &error))
+               return TRUE;
+           if (dvonn_board_move_legal (board, x, y,
+                                       x, y + height, &error))
+               return TRUE;
+           if (dvonn_board_move_legal (board, x, y,
+                                       x, y - height, &error))
+               return TRUE;
+           if (dvonn_board_move_legal (board, x, y,
+                                       x + height, y - height, &error))
+               return TRUE;
+           if (dvonn_board_move_legal (board, x, y,
+                                       x - height, y + height, &error))
+               return TRUE;
+       }
+    }
+    return FALSE;
+}
+
+
+static dvonn_phase_t
 dvonn_board_next_player (dvonn_board_t *board)
 {
     if (board->player == DVONN_PLAYER_BLACK)
        board->player = DVONN_PLAYER_WHITE;
     else
        board->player = DVONN_PLAYER_BLACK;
-}
 
-int
-dvonn_board_pass (dvonn_board_t *board)
-{
-    /* XXX: Should check here and only allow a pass if there are
-     * no legal moves. */
-
-    dvonn_board_next_player (board);
+    /* Only look for legal moves during the movement phase. */
+    if (board->phase != DVONN_PHASE_MOVEMENT)
+       return board->phase;
+
+    if (! dvonn_board_player_has_legal_move (board)) {
+       if (board->player == DVONN_PLAYER_BLACK)
+           board->player = DVONN_PLAYER_WHITE;
+       else
+           board->player = DVONN_PLAYER_BLACK;
+
+       if (! dvonn_board_player_has_legal_move (board))
+       {
+           int x, y;
+           dvonn_cell_t *cell;
+
+           board->phase = DVONN_PHASE_GAME_OVER;
+
+           /* Compute final score. */
+           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))
+                   {
+                       cell = &board->cells[x][y];
+                       if (cell->type == DVONN_CELL_RED)
+                           continue;
+                       board->score[cell->type] += cell->height;
+                   }
+               }
+           }
+       }
+    }
 
-    return TRUE;
+    return board->phase;
 }
 
 int