]> git.cworth.org Git - dvonn/blob - dvonn-board.c
Don't let one player place the opponent's pieces
[dvonn] / dvonn-board.c
1 /*
2  * Copyright (C) 2009 Carl Worth
3  *
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.
8  *
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.
13  *
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/ .
16  *
17  * Author: Carl Worth <cworth@cworth.org>
18  */
19
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <assert.h>
23
24 #include <glib.h>
25
26 #include "dvonn-board.h"
27
28 /* Just a diagram to help start thinking about data structures:
29  *
30  * Played/drawn as:
31  *
32  *   1 ○ ○ ○ ○ ○ ○ ○ ○ ○
33  *  2 ○ ● ● ○ ○ ○ ○ ○ ○ ○
34  * 3 ○ ● ○ ● ○ ○ ○ ○ ○ ○ ○
35  *  4 ○ ● ● ○ ○ ○ ○ ○ ○ ○ K
36  *   5 ○ ○ ○ ○ ○ ○ ○ ○ ○ J
37  *      A B C D E F G H I
38  *
39  * Stored as:
40  *
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
47  *
48  * With connections as:
49  *
50  *         C2  D2
51  *          | /
52  *  B3 <-> C3 <-> D3
53  *        / |
54  *      B2 C4
55  */
56
57 void
58 dvonn_board_init (dvonn_board_t *board)
59 {
60     int x, y;
61
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;
67         }
68     }
69
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;
74             board->cells
75                 [DVONN_BOARD_X_SIZE-1-x]
76                 [DVONN_BOARD_Y_SIZE-1-y].type = DVONN_CELL_INVALID;
77         }
78     }
79
80     board->phase = DVONN_PHASE_PLACEMENT;
81     board->player = DVONN_PLAYER_WHITE;
82     board->moves = 0;
83 }
84
85 dvonn_bool_t
86 dvonn_board_cell_occupied (dvonn_board_t *board,
87                            int x, int y)
88 {
89     if (x < 0 || x >= DVONN_BOARD_X_SIZE ||
90         y < 0 || y >= DVONN_BOARD_Y_SIZE)
91     {
92         return FALSE;
93     }
94
95     if (board->cells[x][y].type == DVONN_CELL_INVALID ||
96         board->cells[x][y].type == DVONN_CELL_EMPTY)
97     {
98         return FALSE;
99     }
100
101     return TRUE;
102 }
103
104 dvonn_bool_t
105 dvonn_board_cell_surrounded (dvonn_board_t *board,
106                              int x, int y)
107 {
108     int surround_count;
109
110     surround_count =
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);
117
118     return (surround_count == 6);
119 }
120
121 static dvonn_bool_t
122 dvonn_board_move_legal (dvonn_board_t *board,
123                         int x1, int y1,
124                         int x2, int y2,
125                         char **error)
126 {
127     int distance;
128
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)
133     {
134         *error = "Invalid coordinates (not on board)";
135         return FALSE;
136     }
137
138     if (board->cells[x1][y1].type == DVONN_CELL_INVALID ||
139         board->cells[x2][y2].type == DVONN_CELL_INVALID)
140     {
141         *error = "Not a valid board space";
142         return FALSE;
143     }
144
145     if (board->cells[x1][y1].type == DVONN_CELL_EMPTY) {
146         *error = "There are no pieces there to move";
147         return FALSE;
148     }
149
150     if (dvonn_board_cell_surrounded (board, x1, y1)) {
151         *error = "You cannot move a piece that is surrounded";
152         return FALSE;
153     }
154
155     if (board->cells[x1][y1].type != board->player) {
156         *error = "You cannot move your opponent's stack";
157         return FALSE;
158     }
159
160     if (board->cells[x2][y2].type == DVONN_CELL_EMPTY) {
161         *error = "You cannot move to an empty space";
162         return FALSE;
163     }
164
165     /* Move must be in a straight line, which in our array is
166      * horizontal, vertical, or one of the diagonals. */
167     if (x2 == x1)
168         distance = abs (y2 - y1);
169     else if (y2 == y1)
170         distance = abs (x2 - x1);
171     else if ((x2 - x1) == - (y2 - y1))
172         distance = abs (x2 - x1);
173     else {
174         *error = "Move is not in a straight line";
175         return FALSE;
176     }
177
178     if (distance != board->cells[x1][y1].height) {
179         *error = "Move distance is not the same as stack height";
180         return FALSE;
181     }
182
183     return TRUE;
184 }
185
186 static void
187 dvonn_board_next_player (dvonn_board_t *board)
188 {
189     if (board->player == DVONN_PLAYER_BLACK)
190         board->player = DVONN_PLAYER_WHITE;
191     else
192         board->player = DVONN_PLAYER_BLACK;
193 }
194
195 int
196 dvonn_board_pass (dvonn_board_t *board)
197 {
198     /* XXX: Should check here and only allow a pass if there are
199      * no legal moves. */
200
201     dvonn_board_next_player (board);
202
203     return TRUE;
204 }
205
206 int
207 dvonn_board_place (dvonn_board_t *board,
208                    int x, int y,
209                    char **error)
210 {
211     if (board->phase != DVONN_PHASE_PLACEMENT) {
212         *error = "Cannot place outside of placement phase";
213         return FALSE;
214     }
215
216     if (board->cells[x][y].type != DVONN_CELL_EMPTY) {
217         *error = "Cannot place on an occupied space";
218         return FALSE;
219     }
220
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;
226     } else {
227         board->cells[x][y].type = DVONN_CELL_WHITE;
228     }
229
230     board->cells[x][y].height = 1;
231
232     board->moves++;
233
234     dvonn_board_next_player (board);
235
236     if (board->moves == 49) {
237         board->phase = DVONN_PHASE_MOVEMENT;
238         board->moves = 0;
239         board->player = DVONN_PLAYER_WHITE;
240     }
241
242     return TRUE;
243 }
244
245 static void
246 fill_living(dvonn_board_t *board,
247             int living[DVONN_BOARD_X_SIZE][DVONN_BOARD_Y_SIZE],
248             int x, int y)
249 {
250     if (dvonn_board_cell_occupied (board, x, y) && ! living[x][y])
251     {
252         living[x][y] = 1;
253
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);
260     }
261 }
262
263 static void
264 eliminate_disconnected_stacks (dvonn_board_t *board)
265 {
266     int x, y;
267     int living[DVONN_BOARD_X_SIZE][DVONN_BOARD_Y_SIZE];
268
269     for (x = 0; x < DVONN_BOARD_X_SIZE; x++)
270         for (y = 0; y < DVONN_BOARD_Y_SIZE; y++)
271             living[x][y] = 0;
272
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);
277
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) &&
281                 ! living[x][y])
282             {
283                 board->cells[x][y].type = DVONN_CELL_EMPTY;
284                 board->cells[x][y].height = 0;
285             }
286 }
287
288 int
289 dvonn_board_move (dvonn_board_t *board,
290                   int x1, int y1,
291                   int x2, int y2,
292                   char **error)
293 {
294     if (board->phase != DVONN_PHASE_MOVEMENT) {
295         *error = "Cannot move outside of placement phase";
296         return FALSE;
297     }
298
299     if (! dvonn_board_move_legal (board, x1, y1, x2, y2, error))
300         return FALSE;
301
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;
305
306     board->cells[x1][y1].type = DVONN_CELL_EMPTY;
307     board->cells[x1][y1].height = 0;
308     board->cells[x1][y1].contains_red = FALSE;
309
310     eliminate_disconnected_stacks (board);
311
312     dvonn_board_next_player (board);
313
314     return TRUE;
315 }