-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathboard.c
350 lines (321 loc) · 9.39 KB
/
board.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "board.h"
/** Gets the array index from the provided slot.
* The slot is recorded with x and y values for graphic representation.
* The array used for storing all slots is an AXIS*AXIS length array.
*/
size_t get_index_from_slot(struct slot s)
{
return AXIS * s.x + s.y;
}
/** Returns whether the given slot's x/y position
* exists somewhere within (0, 0) to (AXIS, AXIS)
*/
static inline bool is_slot_in_boundary(struct slot s)
{
return (s.x < AXIS && s.y < AXIS);
}
void list_adjacent_slots(struct slot s, struct slot **adjs)
{
/* up right bottom left */
int n[4][2] = { {0, 1}, {1, 0}, {0, -1}, {-1, 0} };
/* Check adjacent slots to make sure edges match. */
for (int i = 0; i < 4; ++i) {
struct slot sprime = make_slot(s.x + n[i][0], s.y + n[i][1]);
if (is_slot_in_boundary(s)) {
*(adjs[i]) = sprime;
} else {
adjs[i] = NULL;
}
}
}
void list_adjacent_tiles(struct board *b, struct slot **ss, struct tile **ts)
{
struct tile empty =
make_tile((enum edge[5]){EMPTY,EMPTY,EMPTY,EMPTY,EMPTY}, NONE);
for (int i = 0; i < 4; ++i) {
if (ss[i] == NULL) {
ts[i] = NULL;
continue;
}
struct tile t = b->tiles[get_index_from_slot((*ss[i]))];
if (is_tile_equal(t, empty)) {
ts[i] = NULL;
} else {
*ts[i] = t;
}
}
}
/**
* @remarks Although slots and tiles have the same length arrays,
* we search through the board's slot array due to no sorting guarantees.
*/
static bool is_slot_placeable(struct board b, struct slot s)
{
/* TODO: Switch to linear search? */
/* Linear search open positions for the desired one. */
for (unsigned i = 0; i < b.empty_slot_count; ++i) {
switch(compare_slot_positions(b.slot_spots[i], s)) {
case -1:
continue;
case 0:
return true;
case 1:
return false;
}
}
return false;
}
/** Returns whether the given slot on the given board has no tile on it.
* @remarks todo: Confirm whether "placing" a tile is done by writing the tile's edge[] onto an empty tile's edge[].
*/
static bool is_slot_empty(struct board b, struct slot s)
{
struct tile t = b.tiles[get_index_from_slot(s)];
for (int i = 0; i < 5; ++i) {
if (t.edges[i] != EMPTY) {
return false;
}
}
return true;
}
/** Returns the index of <em>slots</em> at/immediately following the
* given slot <em>s</em>, or <em>count</em> if not found.
*
* @precondition <em>slots</em> is sorted by ascending position.
*/
size_t get_insertion_index(struct slot *slots, size_t count, struct slot s)
{
/* Iterate i until count is reached
* or we pass the given slot's x/y position
*/
size_t i;
for (i = 0; i< count && compare_slot_positions(s, slots[i]) > 0; ++i) {}
return i;
}
/** Inserts the given slot into the given board's slot_spots. */
static struct board add_placeable_slot(struct board b, struct slot s)
{
struct slot *spots = b.slot_spots;
size_t i = get_insertion_index(spots, b.empty_slot_count, s);
if (i < b.empty_slot_count) { /* Sorted insert. */
memmove(&spots[i+1], &spots[i], sizeof(s)*b.empty_slot_count-i);
}
spots[i] = s;
b.empty_slot_count++;
return b;
}
/** Removes the given slot from the given board's slot_spots. */
static struct board remove_placeable_slot(struct board b, struct slot s)
{
struct slot *spots = b.slot_spots;
size_t i = get_insertion_index(spots, b.empty_slot_count, s);
memmove(&spots[i], &spots[i + 1], sizeof(s) * (b.empty_slot_count - i));
b.empty_slot_count--;
return b;
}
/** Removes the given slot from the given board's slot_spots
* and add the newly available slots adjacent to the removed slot.
*
* Intended to be called when a tile has been placed
* in the position of the removed slot.
*/
static struct board update_slot_spots(struct board b, struct slot s)
{
struct slot adj[4];
struct slot *adjs[4];
for (size_t i = 0; i < 4; ++i) {
adjs[i] = &adj[i];
}
list_adjacent_slots(s, adjs);
b = remove_placeable_slot(b, s);
for (int i = 0; i < 4; ++i) {
if (adjs[i] == NULL) {
continue;
}
if (is_slot_empty(b, *adjs[i])) {
b = add_placeable_slot(b, adj[i]);
}
}
return b;
}
/** Returns the validation code of the given move on the given board.
* @returns 0 (OK) if a legal valid move, non-zero otherwise.
* @see move.h:enum game_error_code
*/
static enum game_error_code invalid_move(struct board b, struct move m)
{
if (!is_slot_placeable(b, m.slot)) {
return E_TILE_NOT_PLACEABLE; /* Slot not placeable. */
}
struct slot adj[4];
struct slot *adjs[4] = {&adj[0], &adj[1], &adj[2], &adj[3]};
list_adjacent_slots(m.slot, adjs);
struct tile t = rotate_tile(m.tile, m.rotation);
for (unsigned int i = 0; i < 4; ++i) { /* Need wrapping */
if (adjs[i] == NULL) { /* Ignore if not on board. */
continue;
}
/* The (i + 2) % 4 math here is a bit evil, but it works. */
enum edge pair =
b.tiles[get_index_from_slot(*adjs[i])].edges[(i+2)%4];
if (pair == EMPTY) {
continue; /* Empty tiles match with everything. */
}
if (pair != t.edges[i]) { /* Corresponding don't match. */
return E_TILE_EDGE_CONFLICT;
}
}
return OK;
}
/** Returns an initialised board with an empty slot in the very centre. */
struct board make_board(void)
{
struct board b;
/* Starting centre piece */
enum edge edges[5] = { EMPTY, EMPTY, EMPTY, EMPTY, EMPTY };
const unsigned int mid = (AXIS - 1) / 2; /* Must start in center. */
b.slot_spots[0] = make_slot(mid, mid);
b.empty_slot_count = 1;
for (unsigned int i = 0; i < AXIS*AXIS; ++i) {
b.tiles[i] = make_tile(edges, NONE);
}
/* Tab between columns except for the last one, which newlines. */
memset(b.column_terminators, '\t', AXIS - 1);
b.column_terminators[AXIS - 1] = '\n';
return b;
}
char *print_board(struct board b, char res[BOARD_LEN])
{
const size_t cnt = TILE_LINES;
const size_t len = TILE_LINE_LEN;
char buf[TILE_LEN];
/* Pretty print the board in NxN format. */
for (size_t i = 0; i < AXIS; ++i) {
for (size_t j = 0; j < AXIS; ++j) {
print_tile(b.tiles[get_index_from_slot(make_slot(i, j))], buf);
for (size_t k = 0; k < cnt; ++k) {
const size_t ind = ((i *cnt +k) *AXIS +j) *len;
buf[(k + 1) *len - 1] = b.column_terminators[j];
memcpy(&res[ind], &buf[len * k], len);
}
}
}
res[BOARD_LEN - 1] = '\0';
return res;
}
enum game_error_code test_move_board(struct board *b, struct move m)
{
enum game_error_code rc;
if ((rc = invalid_move(*b, m))) {
return rc;
}
return OK;
}
/** Tries to play the given move on the given board, returning a status code.
*
* @postcondition Board is updated if given move is valid.
* @returns 0 (OK) on success, otherwise a respective <code>game_error_code</code>
*/
enum game_error_code
play_move_board(struct board *b, struct move m)
{
enum game_error_code rc;
if ((rc = test_move_board(b, m)) == OK) {
b->tiles[get_index_from_slot(m.slot)] =
rotate_tile(m.tile, m.rotation);
*b = update_slot_spots(*b, m.slot);
}
return rc;
}
#ifdef TEST
static void print_placeable_slots(struct board b)
{
printf("Slots:\n");
printf("X\tY\n");
for (size_t i = 0; i < b.empty_slot_count; ++i) {
printf("%u\t%u\n", b.slot_spots[i].x, b.slot_spots[i].y);
}
return;
}
static void play_and_check_move(struct board *b,struct move m,struct slot **adj)
{
enum game_error_code rc;
if ((rc = play_move_board(b, m))) {
printf("Invalid move! %d\n", rc);
} else {
printf("Good move!\n");
}
}
int main(void)
{
char buffer[TILE_LEN];
char board_buffer[BOARD_LEN];
enum edge edges[5][5] = {
{ EMPTY, EMPTY, EMPTY, EMPTY, EMPTY },
{ GAMETRAIL, GAMETRAIL, GAMETRAIL, GAMETRAIL, GAMETRAIL },
{ JUNGLE, JUNGLE, JUNGLE, JUNGLE, JUNGLE },
{ LAKE, LAKE, LAKE, LAKE, LAKE },
{ LAKE, JUNGLE, GAMETRAIL, LAKE, GAMETRAIL }
};
struct tile tiles[5] = {
make_tile(edges[0], NONE),
make_tile(edges[1], NONE),
make_tile(edges[2], NONE),
make_tile(edges[3], NONE),
make_tile(edges[4], NONE)
};
const char string[5][30] = {
"\nEmpty tile:",
"\nAll Road tile:",
"\nAll Field tile:",
"\nAll City tile:",
"\nMixed tile:"
};
printf("Testing different tile types.\n");
for (int i = 0; i < 5; ++i) {
printf("%s\n%s\n", string[i], print_tile(tiles[i], buffer));
}
printf("\nTile Rotations: \n");
for (int i = 0; i < 4; ++i) {
printf("%d rotation:\n%s\n", i,
print_tile(rotate_tile(tiles[4], i), buffer));
}
printf("\nTesting board creation. All Null.\n");
struct board b = make_board();
printf("%s\n", print_board(b, board_buffer));
printf("\nLet's see what slots are placeable.\n");
print_placeable_slots(b);
const unsigned int mid = AXIS / 2;
printf("\nPlay the center (%d, %d), the starting move.\n", mid, mid);
struct slot adj[4];
struct slot *adjs[4];
for (size_t i = 0; i < 4; ++i) {
adjs[i] = &adj[i];
}
play_and_check_move(&b,
make_move(tiles[3], make_slot(mid, mid), 0, -1, -1),
adjs);
printf("%s\n", print_board(b, board_buffer));
printf("\nAnd now what slots are placeable?\n");
print_placeable_slots(b);
printf("\nTest tile validator (should fail): (%d, %d)\n", mid, mid + 1);
for (size_t i = 0; i < 4; ++i) {
adjs[i] = &adj[i];
}
play_and_check_move(&b,make_move(tiles[2],
make_slot(mid, mid + 1), 0, -1, -1),
adjs);
printf("%s\n", print_board(b, board_buffer));
print_placeable_slots(b);
printf("\nTest tile validator (should pass): (%d, %d)\n", mid, mid + 1);
for (size_t i = 0; i < 4; ++i) {
adjs[i] = &adj[i];
}
play_and_check_move(&b,
make_move(tiles[3], make_slot(mid, mid + 1), 0, -1, -1),
adjs);
printf("%s\n", print_board(b, board_buffer));
print_placeable_slots(b);
return 0;
}
#endif