-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridgen.c
371 lines (344 loc) · 7.82 KB
/
bridgen.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
/************************************************************
bridgen.c
UNSW CSE
COMP3411/9814
Generate a rand hashi puzzle of specified size.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#define MAX_ROW 100
#define MAX_COL 100
#define MAX_HUB 500
#define MAX_BRIDGE 1000
#define NONE -1
#define HORIZONTAL 0
#define VERTICAL 1
#define FALSE 0
#define TRUE 1
#define EAST 0
#define NORTH 1
#define WEST 2
#define SOUTH 3
// #define SPROUT
#define SPARSE
// #define TREE
/************************************************************
Print the input map in character format.
*/
void print_map(int nrow, int ncol, int map[MAX_ROW][MAX_COL])
{
int r, c;
for (r = 0; r < nrow; r++)
{
for (c = 0; c < ncol; c++)
{
if (map[r][c] > 0)
{
if (map[r][c] > 9)
{
putchar(map[r][c] + 'a' - 10);
}
else
{
putchar('0' + map[r][c]);
}
}
else
{
putchar('.');
}
}
putchar('\n');
}
}
/************************************************************
Return TRUE if location (r,c) is next to an existing island.
*/
int island_neighbor(
int r,
int c,
int nrow,
int ncol,
int map[MAX_ROW][MAX_COL])
{
if ((c < ncol - 1 && map[r][c + 1] > 0) || (r > 0 && map[r - 1][c] > 0) || (c > 0 && map[r][c - 1] > 0) || (r < nrow - 1 && map[r + 1][c] > 0))
{
return (TRUE);
}
else
{
return (FALSE);
}
}
/************************************************************
Find a rand start and end location and add a new bridge.
*/
int add_bridge(
int map_empty,
int nrow,
int ncol,
int max_plank,
int map[MAX_ROW][MAX_COL],
int dirn[MAX_ROW][MAX_COL],
int nplank[MAX_ROW][MAX_COL])
{
int num_plank;
int r0, c0, r1, c1;
int r, c;
if (map_empty == TRUE)
{ // this will be the first bridge
r = rand() % nrow;
c = rand() % ncol;
}
else
{ // look for an existing island or bridge which can
// be used as the starting point for a new bridge
do
{
r = rand() % nrow;
c = rand() % ncol;
#ifdef SPROUT
} while (map[r][c] == 0);
#else
} while (dirn[r][c] == NONE && map[r][c] == 0);
#endif
if (island_neighbor(r, c, nrow, ncol, map))
{
return (FALSE);
}
}
// choose number of planks
num_plank = 1 + (rand() % max_plank);
// choose direction and end location for new bridge
switch (rand() % 4)
{
case EAST:
c0 = c;
r0 = r1 = r;
c = c0 + 1;
while (c < ncol - 1 && dirn[r][c] == NONE && map[r][c] == 0)
{
c++;
}
#ifdef SPARSE
if (dirn[r][c] != NONE)
{
c--;
}
#else
#ifdef TREE
c--;
#endif
#endif
if (c < c0 + 2)
{ // too short
return (FALSE);
}
c1 = c0 + 2 + (rand() % (c - c0 - 1));
if (island_neighbor(r1, c1, nrow, ncol, map))
{
return (FALSE);
}
for (c = c0 + 1; c < c1; c++)
{
dirn[r][c] = HORIZONTAL;
nplank[r][c] = num_plank;
}
break;
case NORTH:
c0 = c1 = c;
r1 = r;
r = r1 - 1;
while (r > 0 && dirn[r][c] == NONE && map[r][c] == 0)
{
r--;
}
#ifdef SPARSE
if (dirn[r][c] != NONE)
{
r++;
}
#else
#ifdef TREE
r++;
#endif
#endif
if (r > r1 - 2)
{
return (FALSE);
}
r0 = r1 - 2 - (rand() % (r1 - r - 1));
if (island_neighbor(r0, c0, nrow, ncol, map))
{
return (FALSE);
}
for (r = r0 + 1; r < r1; r++)
{
dirn[r][c] = VERTICAL;
nplank[r][c] = num_plank;
}
break;
case WEST:
c1 = c;
r0 = r1 = r;
c = c1 - 1;
while (c > 0 && dirn[r][c] == NONE && map[r][c] == 0)
{
c--;
}
#ifdef SPARSE
if (dirn[r][c] != NONE)
{
c++;
}
#else
#ifdef TREE
c++;
#endif
#endif
if (c > c1 - 2)
{
return (FALSE);
}
c0 = c1 - 2 - (rand() % (c1 - c - 1));
if (island_neighbor(r0, c0, nrow, ncol, map))
{
return (FALSE);
}
for (c = c0 + 1; c < c1; c++)
{
dirn[r][c] = HORIZONTAL;
nplank[r][c] = num_plank;
}
break;
case SOUTH:
c0 = c1 = c;
r0 = r;
r = r0 + 1;
while (r < nrow - 1 && dirn[r][c] == NONE && map[r][c] == 0)
{
r++;
}
#ifdef SPARSE
if (dirn[r][c] != NONE)
{
r--;
}
#else
#ifdef TREE
r--;
#endif
#endif
if (r < r0 + 2)
{
return (FALSE);
}
r1 = r0 + 2 + (rand() % (r - r0 - 1));
if (island_neighbor(r1, c1, nrow, ncol, map))
{
return (FALSE);
}
for (r = r0 + 1; r < r1; r++)
{
dirn[r][c] = VERTICAL;
nplank[r][c] = num_plank;
}
break;
}
// re-compute number of planks at start of new bridge
nplank[r0][c0] = 0;
map[r0][c0] = 0;
if (c0 < ncol - 1 && dirn[r0][c0 + 1] == HORIZONTAL)
map[r0][c0] += nplank[r0][c0 + 1];
if (r0 > 0 && dirn[r0 - 1][c0] == VERTICAL)
map[r0][c0] += nplank[r0 - 1][c0];
if (c0 > 0 && dirn[r0][c0 - 1] == HORIZONTAL)
map[r0][c0] += nplank[r0][c0 - 1];
if (r0 < nrow - 1 && dirn[r0 + 1][c0] == VERTICAL)
map[r0][c0] += nplank[r0 + 1][c0];
// re-compute number of planks at end of new bridge
nplank[r1][c1] = 0;
map[r1][c1] = 0;
if (c1 < ncol - 1 && dirn[r1][c1 + 1] == HORIZONTAL)
map[r1][c1] += nplank[r1][c1 + 1];
if (r1 > 0 && dirn[r1 - 1][c1] == VERTICAL)
map[r1][c1] += nplank[r1 - 1][c1];
if (c1 > 0 && dirn[r1][c1 - 1] == HORIZONTAL)
map[r1][c1] += nplank[r1][c1 - 1];
if (r1 < nrow - 1 && dirn[r1 + 1][c1] == VERTICAL)
map[r1][c1] += nplank[r1 + 1][c1];
return (TRUE);
}
int main(int argc, char *argv[])
{
int map[MAX_ROW][MAX_COL]; // islands
int dirn[MAX_ROW][MAX_COL]; // NONE, HORIZONTAL or VERTICAL
int nplank[MAX_ROW][MAX_COL];
int max_plank = 3;
int nrow, ncol;
int nfail;
int r, c;
if (argc < 2 || !isdigit(argv[1][0]))
{
printf("Usage: %s <nrow> [ncol]\n", argv[0]);
printf("Generate a rand Hashi puzzle of size nrow x ncol;\n");
printf("can be run repeatedly, producing different puzzles each time;\n");
printf("ncol defaults to nrow.\n");
return 0;
}
else
{
nrow = atoi(argv[1]);
}
if (nrow < 3)
{
nrow = 3;
}
if (nrow > MAX_ROW)
{
nrow = MAX_ROW;
}
if (argc < 3)
{
ncol = nrow;
}
else
{
ncol = atoi(argv[2]);
}
if (ncol < 3)
{
ncol = 3;
}
if (ncol > MAX_COL)
{
ncol = MAX_COL;
}
srand(time(NULL));
for (r = 0; r < nrow; r++)
{
for (c = 0; c < ncol; c++)
{
map[r][c] = 0;
nplank[r][c] = 0;
dirn[r][c] = NONE;
}
}
// add the first bridge
while (!add_bridge(TRUE, nrow, ncol, max_plank, map, dirn, nplank))
;
// keep adding bridges until it becomes too difficult
nfail = 0;
while (nfail < 200)
{
nfail = 0;
while (nfail < 200 && !add_bridge(FALSE, nrow, ncol, max_plank, map, dirn, nplank))
{
nfail++;
}
}
print_map(nrow, ncol, map);
return 0;
}