-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpentomino.c
207 lines (174 loc) · 5.58 KB
/
pentomino.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
/*
PENTOMINO
Copyright (C) 1995-2023 Andreas Huggel <ahuggel@gmx.net>
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.
*/
/************************************************/
/* pentomino.c */
/* The main function parses the command line */
/* and controls the program flow. */
/************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "data.h"
#include "setup.h"
#include "read.h"
#include "print.h"
#include "play.h"
#ifdef SCARCH
#include "scarch.h"
#endif
#include "pentomino.h"
struct pinfo prg; /* Global program information */
struct ginfo gme; /* Global game information */
int main(int argc, char *argv[])
{
int c, errflg = 0;
extern char *optarg; /* for getopt */
extern int optind, opterr, optopt; /* also for getopt */
struct tnode *start_piece;
/* Initialisations */
prg.progname = argv[0];
prg.inifile = NULL;
prg.tryfile = NULL;
prg.color = FALSE;
prg.quiet = FALSE;
prg.verbose = FALSE;
prg.sort = TRUE;
#ifdef SCARCH
prg.scale = 1;
#endif
gme.first_piece = NULL;
gme.before_my_first_pos = NULL;
gme.after_my_last_pos = NULL;
gme.f_plausible_fct = NULL;
gme.piece_count = 0;
gme.piece_sizes = -1;
gme.counter = 0;
/* make sure the data type used for a board is large enough */
if (sizeof(lineT)*8 < XDIM*YDIM)
{
fprintf(stderr, "%s: The data type used for a board is too small (lineT is %ld bytes)\n",
prg.progname, sizeof(lineT));
exit(1);
}
/* handle the command line arguments */
optarg = NULL;
while (!errflg && (c = getopt(argc, argv, ARGS)) != -1)
{
switch (c)
{
case 'c':
prg.color = TRUE;
break;
case 'n':
prg.sort = FALSE;
break;
#ifdef SCARCH
case 'p':
prg.scale = abs (atoi (optarg));
break;
#endif
case 'q':
prg.quiet = TRUE;
break;
case 's':
prg.tryfile = optarg;
break;
case 'v':
prg.verbose = TRUE;
break;
case ':':
fprintf(stderr, "%s: Option -%c requires an argument\n",
prg.progname, optopt);
errflg++;
break;
case '?':
fprintf(stderr, "%s: Unrecognized option: -%c\n",
prg.progname, optopt);
errflg++;
break;
default :
errflg++;
}
}
#ifdef SCARCH
if (prg.scale == 0)
{
fprintf(stderr, "%s: Scale must be greater than zero\n",
prg.progname);
errflg++;
}
#endif
if (prg.sort == FALSE && prg.tryfile != NULL)
{
fprintf(stderr, "%s: Only one of -n and -s is allowed\n",
prg.progname);
errflg++;
}
if (!errflg && optind != argc - 1)
{
fprintf(stderr, "%s: No inifile specified\n", prg.progname);
errflg++;
}
if (errflg)
{
fprintf(stderr, USAGE_STR, prg.progname);
exit(1);
}
prg.inifile = argv[optind];
read_pieces(&gme.first_piece,
&gme.piece_count ); /* read the inifile, count pieces */
check_pieces(&gme.piece_sizes,
&gme.f_plausible_fct); /* check and setup the plausi fct */
create_pos_lists(); /* pos lists for all pieces */
if (prg.tryfile)
{
read_try(); /* read the tryfile */
}
/* if we're trying to find a solution for a tryfile, then we can't restrict */
/* the position lists, as the position of any piece in the file may be one */
/* one that would get removed. */
if (!prg.tryfile && gme.piece_sizes == 5) /* 5 not general enough yet */
{
restrict_pos_lists(); /* due to field symmetries */
}
if (prg.sort)
{
sort_pieces(&gme.first_piece);
}
if (prg.verbose)
{
fprintf(stdout, "%s order:\n", prg.sort ? "Sorted" : "Unsorted");
list_pieces(); /* print the pieces list */
}
if (prg.verbose)
{
fprintf(stdout, "Starting the search...\n");
prt_time(stdout);
}
start_piece = setup_gameboard(); /* set all pieces from tryfile */
#ifdef SCARCH
spawn_processes(start_piece);
#else
search_tree(start_piece); /* trial and error... */
fprintf (stdout, "%d solutions found\n", gme.counter);
#endif
if (prg.verbose)
{
prt_time (stdout);
}
return 0;
}