-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.c
426 lines (398 loc) · 10.7 KB
/
generator.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#include "generator.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stddef.h>
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
// ----------------------
// --- FLOODING RULES ---
// ----------------------
bool isPoolSafe(board *pboard, int row, int col)
// Checks if flooding [row][col] creates a 2x2
// pool or not. There is 4 possible pools per cell.
{
int n_rows = pboard -> rows;
int n_cols = pboard -> cols;
// Upper left pool
if (row != 0 && col != 0)
{
if (getStatus(pboard, row - 1, col - 1) == WATER && getStatus(pboard, row, col - 1) == WATER && getStatus(pboard, row - 1, col) == WATER)
{
return false;
}
}
// Lower left pool
if (row != (n_rows - 1) && col != 0)
{
if (getStatus(pboard, row + 1, col - 1) == WATER && getStatus(pboard, row, col - 1) == WATER && getStatus(pboard, row + 1, col) == WATER)
{
return false;
}
}
// Upper right pool
if (row != 0 && col != (n_cols - 1))
{
if (getStatus(pboard, row - 1, col) == WATER && getStatus(pboard, row - 1, col + 1) == WATER && getStatus(pboard, row, col + 1) == WATER)
{
return false;
}
}
// Lower right pool
if (row != (n_rows - 1) && col != (n_cols - 1))
{
if (getStatus(pboard, row + 1, col) == WATER && getStatus(pboard, row, col + 1) == WATER && getStatus(pboard, row + 1, col + 1) == WATER)
{
return false;
}
}
return true;
}
// --------------------------
// --- FLOODING ALGORITHM ---
// --------------------------
cellType landOrWater()
// Randomly pick between land and water
{
int landLikelyhood = 33;
int cursor = rand() % 100;
if (cursor <= landLikelyhood)
{
return LAND;
}
return WATER;
}
void asignCell(board *pboard, int row, int col, cellType status)
{
int new_row, new_col;
cell *pc;
cellList *list = initCellList();
setCellStatus(&(pboard -> grid[row][col]), status);
list = getOrthogonalCells(pboard, row, col);
if (status == WATER) {
pc = popCellList(list);
while (pc != NULL)
{
new_row = pc -> row;
new_col = pc -> col;
if (isInBoard(pboard, new_row, new_col) && (getStatus(pboard, new_row, new_col) == EMPTY))
{
if (isPoolSafe(pboard, new_row, new_col) == false)
{
setCellStatus(&(pboard -> grid[new_row][new_col]), LAND);
} else {
asignCell(pboard, new_row, new_col, landOrWater());
}
}
pc = popCellList(list);
}
}
}
void fillEmptyCells(board *pboard)
// Turns empty cells into land
{
cell *pcell;
int n_rows = pboard -> rows;
int n_cols = pboard -> cols;
for (size_t i = 0; i < n_rows; i++) {
for (size_t j = 0; j < n_cols; j++) {
pcell = &(pboard -> grid[i][j]);
if (pcell -> type == EMPTY)
{
setCellStatus(pcell, LAND);
}
}
}
}
// --------------- //
// --- ISLANDS --- //
// --------------- //
void markIsland(board *pboard, int row, int col, int id)
// Marks cells as island with id then recurse orthogonaly.
{
cell *pc;
cellList *list = getOrthogonalCells(pboard, row, col);
pc = &(pboard -> grid[row][col]);
pc -> island = id;
pc = popCellList(list);
while (pc != NULL)
{
if ((pc -> type) == LAND && (pc -> island) == -1)
{
markIsland(pboard, pc -> row, pc -> col, id);
}
pc = popCellList(list);
}
}
void markIslands(board *pboard)
// Mark islands of the board.
{
int nrow, ncol, id;
cell *pc;
nrow = pboard -> rows;
ncol = pboard -> cols;
id = 0;
for (size_t i = 0; i < nrow; i++)
{
for (size_t j = 0; j < ncol; j++)
{
pc = &(pboard -> grid[i][j]);
if ((pc -> island) == -1 && (pc -> type) == LAND)
{
markIsland(pboard, i, j, id);
id++;
}
}
}
}
void unmarkIslands(board *pboard)
// Resets island markings
{
int nrow, ncol;
cell *pc;
nrow = pboard -> rows;
ncol = pboard -> cols;
for (size_t i = 0; i < nrow; i++)
{
for (size_t j = 0; j < ncol; j++)
{
pc = &(pboard -> grid[i][j]);
pc -> island = -1;
}
}
}
int countIslands(board *pboard)
// Counts the number of islands on the board
{
cell *pc;
int nrow, ncol, maxIslandId;
nrow = pboard -> rows;
ncol = pboard -> cols;
maxIslandId = 0;
for (size_t i = 0; i < nrow; i++)
{
for (size_t j = 0; j < ncol; j++)
{
pc = &(pboard -> grid[i][j]);
if ((pc -> island) > maxIslandId)
{
maxIslandId = (pc -> island);
}
}
}
return maxIslandId + 1;
}
int *getIslandSizes(board *pboard, int nIsland)
// Return an array detailing island sizes:
// ["island Id"] = "island size"
{
cell *pc;
int nrow, ncol, *sizes;
nrow = pboard -> rows;
ncol = pboard -> cols;
sizes = malloc(nIsland * sizeof(int));
for (size_t i = 0; i < nrow; i++)
{
for (size_t j = 0; j < ncol; j++)
{
pc = &(pboard -> grid[i][j]);
if ((pc -> type) == LAND, (pc -> island) != -1)
{
sizes[pc -> island]++;
}
}
}
return sizes;
}
void clearIsland(board *pboard, int islandId)
// Resets type and island attributes of the
// given island.
{
cell *pc;
int nrow, ncol;
nrow = pboard -> rows;
ncol = pboard -> cols;
for (int i = 0; i < nrow; i++)
{
for (int j = 0; j < ncol; j++)
{
pc = &(pboard -> grid[i][j]);
if ((pc -> island) == islandId)
{
pc -> island = -1;
pc -> type = EMPTY;
}
}
}
}
// ----------------------- //
// --- HINTS PLACEMENT --- //
// ----------------------- //
void placeHints(board *pboard, int *sizes, int nIslands)
// Places hints randomly on islands
{
int nrows, ncols, nCell, counter;
cell *pc;
bool placedHint;
nrows = pboard -> rows;
ncols = pboard -> cols;
for (int k = 0; k < nIslands; k++)
{
placedHint = false;
counter = 0;
nCell = rand() % sizes[k];
for (int i = 0; i < nrows; i++)
{
for (int j = 0; j < ncols; j++)
{
pc = &(pboard -> grid[i][j]);
if ((pc -> island) == k)
{
if(counter == nCell){
pc -> hint = sizes[k];
placedHint = true;
}
counter++;
}
if(placedHint){break;}
}
if(placedHint){break;}
}
}
}
// -----------------
// --- GENERATOR ---
// -----------------
void floodBoard(board *pboard)
// Board flooding algorithm
{
int maxItter = 10;
int seed_row, seed_col, *sizes, \
nIslands, idLargestIsland, maxIslandSize, \
nrows, ncols;
bool foundWaterAdjacent;
cell *pc, *poc;
cellList *cList;
// Maximum island size is set to the size
// of the longest board edge - 1
nrows = (pboard -> rows);
ncols = (pboard -> cols);
maxIslandSize = MAX(nrows, ncols) - 2;
// Select a random seed cell
seed_row = rand() % nrows;
seed_col = rand() % ncols;
idLargestIsland = 0;
// Flood the board
asignCell(pboard, seed_row, seed_col, WATER);
// Asign left over cells to land
fillEmptyCells(pboard);
markIslands(pboard);
// Check for island sizes
nIslands = countIslands(pboard);
sizes = getIslandSizes(pboard, nIslands);
for (int i = 0; i < nIslands; i++)
{
if (sizes[i] > sizes[idLargestIsland])
{
idLargestIsland = i;
}
}
// Clear the largest island and re-flood
// if the largest island is too big
while (sizes[idLargestIsland] > maxIslandSize)
{
maxItter--;
foundWaterAdjacent = false;
clearIsland(pboard, idLargestIsland);
unmarkIslands(pboard);
for (int i = 0; i < nrows; i++)
{
for (int j = 0; j < ncols; j++)
{
pc = &(pboard -> grid[i][j]);
if (((pc -> type) == EMPTY))
{
cList = getOrthogonalCells(
pboard, pc -> row, pc -> col);
poc = popCellList(cList);
while(poc != NULL)
{
if((poc -> type) == WATER && \
isPoolSafe(pboard, pc -> row, pc -> col))
{
foundWaterAdjacent = true;
}
poc = popCellList(cList);
}
}
if(foundWaterAdjacent){break;}
}
if(foundWaterAdjacent){break;}
}
asignCell(pboard, pc -> row, pc -> col, WATER);
fillEmptyCells(pboard);
markIslands(pboard);
nIslands = countIslands(pboard);
sizes = getIslandSizes(pboard, nIslands);
for (int i = 0; i < nIslands; i++)
{
if (sizes[i] > sizes[idLargestIsland])
{
idLargestIsland = i;
}
}
}
placeHints(pboard, sizes, nIslands);
if (maxItter == 0)
{
// The algorithm should coverge in less that 10 itterations
maxItter = 10;
pboard = initBoard(nrows, ncols);
asignCell(pboard, seed_row, seed_col, WATER);
}
}
board *generator(int row, int cols)
// Board genertor
{
board *pboard = initBoard(row, cols);
floodBoard(pboard);
return pboard;
}
// ----------
// --- IO ---
// ----------
void writePuzzleToFile(char *filename, board *pboard, bool solved)
// Writes puzzle to file.
{
cell *pc;
int nrow = pboard -> rows;
int ncol = pboard -> cols;
FILE *fp = fopen(filename, "w");
if (fp == NULL)
{
printf("Error opening the file %s", filename);
exit (0);
}
fprintf(fp, "-");
for (size_t k = 0; k < nrow; k++)
{
fprintf(fp, "----");
}
fprintf(fp, "\n");
for (size_t i = 0; i < nrow; i++)
{
fprintf(fp, "|");
for (size_t j = 0; j < ncol; j++)
{
pc = &(pboard -> grid[i][j]);
cellToFile(pc, fp, solved);
fprintf(fp, "|");
}
fprintf(fp, "\n");
fprintf(fp, "-");
for (size_t k = 0; k < nrow; k++)
{
fprintf(fp, "----");
}
fprintf(fp, "\n");
}
}