-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.c
491 lines (388 loc) · 13.4 KB
/
setup.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
PENTOMINO
Copyright (C) 1995-2023 Andreas Huggel <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/************************************************/
/* setup.c */
/* All functions to setup the data structures. */
/************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "pentomino.h"
#include "data.h"
#include "play.h"
#ifdef DEBUG
#include "print.h"
#endif
#include "setup.h"
/* local function declarations */
static struct pnode *rotate (struct pnode *);
static struct pnode *form_exists (struct pnode *, struct pnode *);
/********************************************/
/* create the position lists for all pieces */
/********************************************/
void create_pos_lists(void)
{
struct tnode *curr_piece = NULL;
struct pnode *rot_list = NULL;
struct pnode *curr_rot = NULL;
struct pnode *currx_pos = NULL;
struct pnode *curry_pos = NULL;
struct pnode *tmpx_pos = NULL;
struct pnode *tmpy_pos = NULL;
/* Ueber alle Teilchen ... */
for (curr_piece = gme.first_piece;
curr_piece != NULL;
curr_piece = curr_piece->next)
{
/* Schiebe die allererste Position (aus inifile) in die linke obere Ecke */
p_shift_lu(curr_piece->pos_list);
/* Erstell die Liste aller Drehungen und Spiegelungen */
rot_list = rotate(curr_piece->pos_list);
/* Ueber alle Drehungen und Spiegelungen ... */
for (curr_rot = rot_list; curr_rot != NULL; curr_rot = curr_rot->next)
{
/* Schiebe die Position auf dem ganzen Feld herum ...*/
currx_pos = p_identity(curr_rot);
while (currx_pos != NULL)
{
tmpx_pos = currx_pos;
currx_pos = p_right(tmpx_pos);
curry_pos = p_identity(tmpx_pos);
while (curry_pos != NULL)
{
tmpy_pos = curry_pos;
curry_pos = p_down(tmpy_pos);
/* Uebernimm die Position in die Liste falls der Check OK ist */
/* und die Position nicht die allererste ist */
if ( gme.f_plausible_fct(tmpy_pos->field, NULL) &&
!f_cmp(tmpy_pos->field, rot_list->field) )
{
t_add_pos(&curr_piece, tmpy_pos);
}
else
{
p_free(&tmpy_pos);
}
}
p_free(&tmpx_pos);
}
} /* for (curr_rot = ... ) */
p_flush(&rot_list);
} /* for (curr_piece = ... ) */
}
/*************************************************************/
/* rotate and mirror a position, return ptr to complete list */
/*************************************************************/
static struct pnode *rotate(struct pnode *first_pos)
{
struct pnode *first_rot = NULL;
struct pnode *mirrored_pos = NULL;
struct pnode *turned_pos = NULL;
int i,j;
/* Use each rotation once per mirage. All these functions */
/* allocate memory for new positions. */
for (i=0; *p_mirror_fct[i] != NULL; i++)
{
mirrored_pos = (*p_mirror_fct[i])(first_pos);
for (j=0; *p_turn_fct[j] != NULL; j++)
{
turned_pos = (*p_turn_fct[j])(mirrored_pos);
if (turned_pos != NULL)
{
if (form_exists(turned_pos, first_rot))
{
p_free(&turned_pos);
}
else
{
p_add(&first_rot, turned_pos);
}
}
}
p_free(&mirrored_pos);
}
return first_rot;
}
/******************************************************************/
/* check if pos exists in the list plist, return a pointer to pos */
/* in plist if it does or NULL otherwise. */
/******************************************************************/
static struct pnode *form_exists(struct pnode *pos, struct pnode *plist)
{
struct pnode *curr_pos = NULL;
for (curr_pos = plist;
curr_pos != NULL && !f_cmp(curr_pos->field, pos->field);
curr_pos = curr_pos->next);
return curr_pos;
}
/**********************************************************************/
/* sort the pieces list by pieces with pos already set, LINE_ID_1s, */
/* the number of positions and the order of the pieces in the inifile */
/**********************************************************************/
void sort_pieces(struct tnode **first_piece)
{
struct tnode **base; /* pointer to the basetable */
int i;
/* allocate the temporary basetable of pointers */
if ((base = (struct tnode **)malloc((size_t)(gme.piece_count *
sizeof(struct tnode *)))) == NULL)
{
fprintf(stderr, "%s: Can't allocate memory for sort-basetable\n",
prg.progname);
exit(1);
}
/* initialise the table with the pointers to our pieces */
base[0] = *first_piece;
for (i=0; i<gme.piece_count-1; i++)
{
if (base[i])
{
base[i+1] = base[i]->next;
}
else
{
fprintf(stderr, "%s: Piece list is shorter than computed\n",
prg.progname);
exit(1);
}
}
if (base[i]->next != NULL)
{
fprintf(stderr, "%s: Piece list is longer than computed\n",
prg.progname);
exit(1);
}
/* sort the basetable */
qsort((void *)base,
(size_t)gme.piece_count,
sizeof(struct tnode *),
t_cmp);
/* shuffle the piece list according to the sorted basetable */
*first_piece = base[0];
for (i=0; i<gme.piece_count-1; i++)
{
base[i]->next = base[i+1];
}
base[i]->next = NULL;
/* free the temporary sort basetable */
free(base);
}
/*************************************************************/
/* restrict the position list(s) to omit symmetric solutions */
/*************************************************************/
void restrict_pos_lists(void)
{
/* Ist noch ueberhaupt nicht allgemein gehalten */
int i,j;
int free_it;
struct tnode *piece;
struct pnode **pos;
piece = gme.first_piece; /* This must be piece 'Kreuz' */
/* Loesche alle Positionen des Teilchens 'Kreuz' wo der */
/* Schwerpunkt des Teilchens ausserhalb des oberen */
/* linken Viertel des Feldes liegt. */
pos = &piece->pos_list;
while (*pos != NULL)
{
free_it = FALSE;
if (!gme.f_plausible_fct((*pos)->field, *pos)) {
free_it = TRUE;
}
for (i=4; i<YDIM && !free_it; i++)
{
for (j=0; j<XDIM && !free_it; j++)
{
if (f_testxy((*pos)->field, j, i))
{
free_it = TRUE;
}
}
}
for (i=0; i<4 && !free_it; i++)
{
for (j=6; j<XDIM && !free_it; j++)
{
if (f_testxy((*pos)->field, j, i))
{
free_it = TRUE;
}
}
}
if (free_it)
{
p_free(pos);
piece->pos_count -= 1; /* this should be t_rm_pos() or t_free_pos() */
}
else
{
pos = &(*pos)->next;
}
}
}
/*****************************************************************/
/* check piece sizes and field, setup the plausibility check fct */
/*****************************************************************/
void check_pieces(int *piece_sizes, PF2 *f_plausible_fct)
{
struct tnode *curr_piece;
struct tnode *next_piece;
int total_ones = 0;
int equal_sizes = TRUE;
curr_piece = gme.first_piece;
while(curr_piece != NULL)
{
/* sum up all ones to compare it with the field size later */
total_ones += curr_piece->one_count;
/* figure out if all pieces are of the same size */
if ((next_piece = curr_piece->next) != NULL)
{
equal_sizes &= curr_piece->one_count == next_piece->one_count;
}
curr_piece = next_piece;
}
/* check whether all pieces fit exactly into the field */
if (XDIM * YDIM != total_ones)
{
fprintf(stderr,"%s: %s: The field size is %s than all pieces together\n",
prg.progname,
prg.inifile,
XDIM * YDIM > total_ones ? "larger" : "smaller");
exit(1);
}
/* set the piece sizes variable and the plausibility check function */
*piece_sizes = equal_sizes ? gme.first_piece->one_count : 0;
*f_plausible_fct = equal_sizes ? f_plausible1 : f_plausible2;
}
/************************************************************************/
/* parse the brick-field: Try to identify each single-character-pattern */
/* with a position of a piece and set it at the piece */
/************************************************************************/
void parse_try(brickT *bfield)
{
int i,j;
int *multi_use;
int one_cnt;
char a, brick;
struct fieldT tryfield;
struct tnode *trypiece;
struct tnode *curr_piece;
struct pnode *trypos;
struct pnode *temp_pos;
if (prg.verbose)
{
fprintf(stdout, "Pieces used in %s:\n", prg.tryfile);
}
/* allocate the array to check for multiple piece definitions in tryfile */
if (0 == (multi_use = (int *)calloc(gme.piece_count, sizeof(int))))
{
fprintf(stderr, "%s: Can't allocate memory for multi_use array\n",
prg.progname);
exit(1);
}
/* the pseudo position we need for the search in the pos list */
temp_pos = p_alloc();
/* filter all the pieces from the tryfile */
do /* while (one_cnt != 0) */
{
one_cnt = 0;
brick = '\0';
f_clear(&tryfield);
/* the character pattern for each distinct character */
/* defines a piece and its position. First, get a pattern */
/* into the tryfield variable */
for (i=0; i<YDIM; i++)
{
for (j=0; j<XDIM; j++)
{
switch (a = b_getxy(*bfield, j, i)) {
case EMPTY_BRICK:
break;
default:
if (brick == '\0')
{
brick = a;
}
if (brick == a)
{
b_setxy(bfield, j, i, EMPTY_BRICK);
f_setxy(&tryfield, j, i);
one_cnt++;
}
} /* switch... */
} /* for (j=0... */
} /* for (i=0... */
/* try to find the pattern in the piece list: scan all */
/* positions of the pieces with a matching number of ones. */
/* we need a pseudo pos for form_exists(). */
f_copy(&temp_pos->field, tryfield);
trypiece = NULL;
trypos = NULL;
for (curr_piece = gme.first_piece;
trypiece == NULL && curr_piece != NULL;
curr_piece = curr_piece->next )
{
if (curr_piece->one_count == one_cnt)
{
if ((trypos = form_exists(temp_pos, curr_piece->pos_list)))
{
trypiece = curr_piece;
}
}
}
/* if we found a matching position, check whether this */
/* piece has not been identified yet and set the pos */
/* pointer at the respective piece to point there */
if (trypiece)
{
if (multi_use[trypiece->number-1])
{
fprintf(stderr,
"%s: %s: Piece %s identified a second time (%c pattern)\n",
prg.progname, prg.tryfile, trypiece->name, brick);
#ifdef DEBUG
print_field(tryfield);
#endif
exit(1);
}
multi_use[trypiece->number-1]++;
trypiece->pos = trypos;
if (prg.verbose)
{
fprintf(stdout, "- %c pattern identified as piece %s\n",
brick, trypiece->name);
#ifdef DEBUG
print_field(trypiece->pos->field);
#endif
}
}
else if (one_cnt != 0)
{
fprintf(stderr, "%s: %s: No matching piece found for the %c pattern\n",
prg.progname, prg.tryfile, brick);
#ifdef DEBUG
print_field(tryfield);
#endif
exit(1);
}
} while (one_cnt != 0);
p_free(&temp_pos);
free(multi_use);
if (prg.verbose)
{
fprintf(stdout, "\n");
}
}