2 * Copyright (C) 2009 Carl Worth
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see http://www.gnu.org/licenses/ .
17 * Author: Carl Worth <cworth@cworth.org>
26 #include "dvonn-board.h"
28 /* Just a diagram to help start thinking about data structures:
33 * 2 ○ ● ● ○ ○ ○ ○ ○ ○ ○
34 * 3 ○ ● ○ ● ○ ○ ○ ○ ○ ○ ○
35 * 4 ○ ● ● ○ ○ ○ ○ ○ ○ ○ K
36 * 5 ○ ○ ○ ○ ○ ○ ○ ○ ○ J
41 * 1 x x ○ ○ ○ ○ ○ ○ ○ ○ ○
42 * 2 x ○ ● ● ○ ○ ○ ○ ○ ○ ○
43 * 3 ○ ● ○ ● ○ ○ ○ ○ ○ ○ ○
44 * 4 ○ ● ● ○ ○ ○ ○ ○ ○ ○ x
45 * 5 ○ ○ ○ ○ ○ ○ ○ ○ ○ x x
46 * A B C D E F G H I J K
48 * With connections as:
58 dvonn_board_init (dvonn_board_t *board)
62 for (x = 0; x < DVONN_BOARD_X_SIZE; x++) {
63 for (y = 0; y < DVONN_BOARD_Y_SIZE; y++) {
64 board->cells[x][y].type = DVONN_CELL_EMPTY;
65 board->cells[x][y].height = 0;
66 board->cells[x][y].contains_red = FALSE;
70 /* Cut out the unplayable "corners" of the board. */
71 for (x = 0; x < DVONN_BOARD_Y_SIZE / 2; x++) {
72 for (y = 0; y < (DVONN_BOARD_Y_SIZE / 2) - x; y++) {
73 board->cells[x][y].type = DVONN_CELL_INVALID;
75 [DVONN_BOARD_X_SIZE-1-x]
76 [DVONN_BOARD_Y_SIZE-1-y].type = DVONN_CELL_INVALID;
80 board->phase = DVONN_PHASE_PLACEMENT;
81 board->player = DVONN_PLAYER_WHITE;
86 dvonn_board_cell_occupied (dvonn_board_t *board,
89 if (x < 0 || x >= DVONN_BOARD_X_SIZE ||
90 y < 0 || y >= DVONN_BOARD_Y_SIZE)
95 if (board->cells[x][y].type == DVONN_CELL_INVALID ||
96 board->cells[x][y].type == DVONN_CELL_EMPTY)
105 dvonn_board_cell_surrounded (dvonn_board_t *board,
111 dvonn_board_cell_occupied (board, x - 1, y) +
112 dvonn_board_cell_occupied (board, x + 1, y) +
113 dvonn_board_cell_occupied (board, x, y - 1) +
114 dvonn_board_cell_occupied (board, x, y + 1) +
115 dvonn_board_cell_occupied (board, x + 1, y - 1) +
116 dvonn_board_cell_occupied (board, x - 1, y + 1);
118 return (surround_count == 6);
122 dvonn_board_move_legal (dvonn_board_t *board,
129 if (x1 < 0 || x1 >= DVONN_BOARD_X_SIZE ||
130 y1 < 0 || y1 >= DVONN_BOARD_Y_SIZE ||
131 x2 < 0 || x2 >= DVONN_BOARD_X_SIZE ||
132 y2 < 0 || y2 >= DVONN_BOARD_Y_SIZE)
134 *error = "Invalid coordinates (not on board)";
138 if (board->cells[x1][y1].type == DVONN_CELL_INVALID ||
139 board->cells[x2][y2].type == DVONN_CELL_INVALID)
141 *error = "Not a valid board space";
145 if (board->cells[x1][y1].type == DVONN_CELL_EMPTY) {
146 *error = "There are no pieces there to move";
150 if (dvonn_board_cell_surrounded (board, x1, y1)) {
151 *error = "You cannot move a piece that is surrounded";
155 if (board->cells[x1][y1].type != board->player) {
156 *error = "You cannot move your opponent's stack";
160 if (board->cells[x2][y2].type == DVONN_CELL_EMPTY) {
161 *error = "You cannot move to an empty space";
165 /* Move must be in a straight line, which in our array is
166 * horizontal, vertical, or one of the diagonals. */
168 distance = abs (y2 - y1);
170 distance = abs (x2 - x1);
171 else if ((x2 - x1) == - (y2 - y1))
172 distance = abs (x2 - x1);
174 *error = "Move is not in a straight line";
178 if (distance != board->cells[x1][y1].height) {
179 *error = "Move distance is not the same as stack height";
187 dvonn_board_next_player (dvonn_board_t *board)
189 if (board->player == DVONN_PLAYER_BLACK)
190 board->player = DVONN_PLAYER_WHITE;
192 board->player = DVONN_PLAYER_BLACK;
196 dvonn_board_pass (dvonn_board_t *board)
198 /* XXX: Should check here and only allow a pass if there are
201 dvonn_board_next_player (board);
207 dvonn_board_place (dvonn_board_t *board,
211 if (board->phase != DVONN_PHASE_PLACEMENT) {
212 *error = "Cannot place outside of placement phase";
216 if (board->cells[x][y].type != DVONN_CELL_EMPTY) {
217 *error = "Cannot place on an occupied space";
221 if (board->moves < 3) {
222 board->cells[x][y].type = DVONN_CELL_RED;
223 board->cells[x][y].contains_red = TRUE;
224 } else if (board->moves % 2) {
225 board->cells[x][y].type = DVONN_CELL_BLACK;
227 board->cells[x][y].type = DVONN_CELL_WHITE;
230 board->cells[x][y].height = 1;
234 dvonn_board_next_player (board);
236 if (board->moves == 49) {
237 board->phase = DVONN_PHASE_MOVEMENT;
239 board->player = DVONN_PLAYER_WHITE;
246 fill_living(dvonn_board_t *board,
247 int living[DVONN_BOARD_X_SIZE][DVONN_BOARD_Y_SIZE],
250 if (dvonn_board_cell_occupied (board, x, y) && ! living[x][y])
254 fill_living (board, living, x - 1, y);
255 fill_living (board, living, x + 1, y);
256 fill_living (board, living, x, y - 1);
257 fill_living (board, living, x, y + 1);
258 fill_living (board, living, x + 1, y - 1);
259 fill_living (board, living, x - 1, y + 1);
264 eliminate_disconnected_stacks (dvonn_board_t *board)
267 int living[DVONN_BOARD_X_SIZE][DVONN_BOARD_Y_SIZE];
269 for (x = 0; x < DVONN_BOARD_X_SIZE; x++)
270 for (y = 0; y < DVONN_BOARD_Y_SIZE; y++)
273 for (x = 0; x < DVONN_BOARD_X_SIZE; x++)
274 for (y = 0; y < DVONN_BOARD_Y_SIZE; y++)
275 if (board->cells[x][y].contains_red)
276 fill_living (board, living, x, y);
278 for (x = 0; x < DVONN_BOARD_X_SIZE; x++)
279 for (y = 0; y < DVONN_BOARD_Y_SIZE; y++)
280 if (dvonn_board_cell_occupied (board, x, y) &&
283 board->cells[x][y].type = DVONN_CELL_EMPTY;
284 board->cells[x][y].height = 0;
289 dvonn_board_move (dvonn_board_t *board,
294 if (board->phase != DVONN_PHASE_MOVEMENT) {
295 *error = "Cannot move outside of placement phase";
299 if (! dvonn_board_move_legal (board, x1, y1, x2, y2, error))
302 board->cells[x2][y2].height += board->cells[x1][y1].height;
303 board->cells[x2][y2].type = board->cells[x1][y1].type;
304 board->cells[x2][y2].contains_red |= board->cells[x1][y1].contains_red;
306 board->cells[x1][y1].type = DVONN_CELL_EMPTY;
307 board->cells[x1][y1].height = 0;
308 board->cells[x1][y1].contains_red = FALSE;
310 eliminate_disconnected_stacks (board);
312 dvonn_board_next_player (board);