forked from dlbeer/cdok
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
456 lines (370 loc) · 9.82 KB
/
main.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
/* cdok -- Calcudoku solver/generator
* Copyright (C) 2012 Daniel Beer <dlbeer@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "cdok.h"
#include "parser.h"
#include "printer.h"
#include "solver.h"
#include "generator.h"
#define OPT_FLAG_UNICODE 0x01
#define OPT_FLAG_TWO_CELL 0x02
#define OPT_FLAG_NYLIMB 0x04
struct command;
struct options {
int flags;
int gen_size;
int gen_iterations;
int gen_limit;
int gen_target;
const char *in_file;
const char *out_file;
const struct command *command;
};
static int read_puzzle(const char *fname, struct cdok_puzzle *puz)
{
struct cdok_parser parse;
FILE *in = stdin;
char buf[4096];
int len;
if (fname) {
in = fopen(fname, "r");
if (!in) {
fprintf(stderr, "Can't open %s for reading: %s\n",
fname, strerror(errno));
return -1;
}
}
cdok_parser_init(&parse, puz);
while ((len = fread(buf, 1, sizeof(buf), in)) > 0)
if (cdok_parser_push(&parse, puz, buf, len) < 0) {
if (fname)
fclose(in);
return -1;
}
if (ferror(in)) {
fprintf(stderr, "IO error reading %s: %s\n",
fname, strerror(errno));
if (fname)
fclose(in);
return -1;
}
if (fname)
fclose(in);
return cdok_parser_end(&parse, puz);
}
static FILE *open_output(const char *fname)
{
FILE *out = stdout;
if (fname) {
out = fopen(fname, "w");
if (!out) {
fprintf(stderr, "Can't open %s for writing: %s\n",
fname, strerror(errno));
return NULL;
}
}
return out;
}
static int close_output(const char *fname, FILE *out)
{
if (fname) {
if (fclose(out) < 0) {
fprintf(stderr, "Error on closing %s: %s\n",
fname, strerror(errno));
return -1;
}
}
return 0;
}
static void write_puzzle(FILE *out, int flags,
const struct cdok_puzzle *puz, const uint8_t *values)
{
cdok_print_puzzle(puz, values, out);
fprintf(out, "\n");
cdok_format_puzzle((flags & OPT_FLAG_UNICODE) ?
&cdok_template_unicode : &cdok_template_ascii,
puz, values, out);
}
static void write_summary(FILE *out, int ret, int diff)
{
if (ret)
fprintf(out, "Solution is not unique.\n");
else
fprintf(out, "Solution is unique. Difficulty: %d\n", diff);
}
static int cmd_print(const struct options *opt)
{
struct cdok_puzzle puz;
FILE *out;
if (read_puzzle(opt->in_file, &puz) < 0)
return -1;
out = open_output(opt->out_file);
if (!out)
return -1;
write_puzzle(out, opt->flags, &puz, puz.values);
return close_output(opt->out_file, out);
}
static int do_solve(const struct options *opt, int want_solution)
{
struct cdok_puzzle puz;
uint8_t solution[CDOK_CELLS];
FILE *out;
int diff = 0;
int r;
if (read_puzzle(opt->in_file, &puz) < 0)
return -1;
puz.nylimb = opt->flags & OPT_FLAG_NYLIMB;
r = cdok_solve(&puz, solution, &diff);
if (r < 0) {
fprintf(stderr, "Puzzle is not solvable\n");
return -1;
}
out = open_output(opt->out_file);
if (!out)
return -1;
if (want_solution) {
write_puzzle(out, opt->flags, &puz, solution);
fprintf(out, "\n");
}
write_summary(out, r, diff);
return close_output(opt->out_file, out);
}
static int cmd_solve(const struct options *opt)
{
return do_solve(opt, 1);
}
static int cmd_examine(const struct options *opt)
{
return do_solve(opt, 0);
}
static int cmd_gen_grid(const struct options *opt)
{
struct cdok_puzzle puz;
FILE *out;
cdok_init_puzzle(&puz, opt->gen_size);
cdok_generate_grid(puz.values, opt->gen_size);
out = open_output(opt->out_file);
if (!out)
return -1;
cdok_print_puzzle(&puz, puz.values, out);
return close_output(opt->out_file, out);
}
static int do_harden(const struct options *opt,
const uint8_t *solution, int size)
{
struct cdok_puzzle puz;
FILE *out;
int r;
r = cdok_generate(&puz, solution, size,
(opt->flags & OPT_FLAG_TWO_CELL) ?
CDOK_FLAGS_TWO_CELL : CDOK_FLAGS_NONE,
opt->gen_iterations,
opt->gen_limit,
opt->gen_target);
out = open_output(opt->out_file);
if (!out)
return -1;
write_puzzle(out, opt->flags, &puz, puz.values);
fprintf(out, "\nDifficulty: %d\n", r);
return close_output(opt->out_file, out);
}
static int cmd_harden(const struct options *opt)
{
struct cdok_puzzle puz;
uint8_t solution[CDOK_CELLS];
int r;
if (read_puzzle(opt->in_file, &puz) < 0)
return -1;
puz.nylimb = opt->flags & OPT_FLAG_NYLIMB;
r = cdok_solve(&puz, solution, NULL);
if (r < 0) {
fprintf(stderr, "Puzzle is not solvable\n");
return -1;
}
if (r)
fprintf(stderr, "warning: input grid solution is "
"not unique\n");
return do_harden(opt, solution, puz.size);
}
static int cmd_generate(const struct options *opt)
{
uint8_t solution[CDOK_CELLS];
cdok_generate_grid(solution, opt->gen_size);
return do_harden(opt, solution, opt->gen_size);
}
struct command {
const char *name;
int (*func)(const struct options *opt);
};
static const struct command command_table[] = {
{"print", cmd_print},
{"solve", cmd_solve},
{"examine", cmd_examine},
{"gen-grid", cmd_gen_grid},
{"harden", cmd_harden},
{"generate", cmd_generate},
{NULL, NULL}
};
static const struct command *find_command(const char *name)
{
int i = 0;
for (;;) {
const struct command *c = &command_table[i];
if (!c->name)
break;
if (!strcasecmp(c->name, name))
return c;
i++;
}
return NULL;
}
static void usage(const char *progname)
{
printf("Usage: %s [options] <command>\n"
"\n"
"Options are:\n"
" -u Use Unicode (UTF-8) line-drawing characters.\n"
" -i filename Read from the given file (default stdin).\n"
" -o filename Write to the given file (default stdout).\n"
" -T Restrict difference and ratio groups to two cells.\n"
" -s size Specify generator grid size (default 6).\n"
" -w num Maximum generator iterations (default 20).\n"
" -m diff Maximum puzzle difficulty (default 0, no limit).\n"
" -t diff Threshold difficulty for early stop (default 0, none).\n"
" -N Nylimb mode\n"
" --help Show this text.\n"
" --version Show version information.\n"
"\n"
"Available commands:\n"
" print Parse a grid spec and print it.\n"
" solve Parse a grid spec and solve the puzzle.\n"
" examine Parse a grid spec and estimate difficulty.\n"
" gen-grid Produce a valid solution grid.\n"
" harden Read a solution grid or puzzle and produce a new puzzle.\n"
" generate Produce a puzzle.\n",
progname);
}
static void version(void)
{
printf("cdok -- Calcudoku solver/generator\n"
"Revision " GIT_VERSION ", built " BUILD_DATE "\n"
"Copyright (C) 2012 Daniel Beer <dlbeer@gmail.com>\n"
"\n"
"Permission to use, copy, modify, and/or distribute this software for any\n"
"purpose with or without fee is hereby granted, provided that the above\n"
"copyright notice and this permission notice appear in all copies.\n"
"\n"
"THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES\n"
"WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF\n"
"MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR\n"
"ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\n"
"WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN\n"
"ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF\n"
"OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n");
}
static int parse_options(int argc, char **argv, struct options *opt)
{
static const struct option longopts[] = {
{"help", 0, 0, 'H'},
{"version", 0, 0, 'V'},
{NULL, 0, 0, 0}
};
int o;
memset(opt, 0, sizeof(*opt));
opt->gen_iterations = 20;
opt->gen_size = 6;
while ((o = getopt_long(argc, argv, "i:o:uTs:w:m:t:N",
longopts, NULL)) >= 0)
switch (o) {
case 'T':
opt->flags |= OPT_FLAG_TWO_CELL;
break;
case 's':
opt->gen_size = atoi(optarg);
if (opt->gen_size < 1 || opt->gen_size > CDOK_SIZE) {
fprintf(stderr, "Invalid grid size: %d\n",
opt->gen_size);
return -1;
}
break;
case 'w':
opt->gen_iterations = atoi(optarg);
break;
case 'm':
opt->gen_limit = atoi(optarg);
break;
case 't':
opt->gen_target = atoi(optarg);
break;
case 'u':
opt->flags |= OPT_FLAG_UNICODE;
break;
case 'i':
opt->in_file = optarg;
break;
case 'o':
opt->out_file = optarg;
break;
case 'N':
opt->flags |= OPT_FLAG_NYLIMB;
break;
case 'V':
version();
exit(0);
case 'H':
usage(argv[0]);
exit(0);
}
argc -= optind;
argv += optind;
if (argc < 1) {
fprintf(stderr, "You need to specify a command. Try --help.\n");
return -1;
}
opt->command = find_command(argv[0]);
if (!opt->command) {
fprintf(stderr, "Unknown command: %s. Try --help.\n", argv[0]);
return -1;
}
return 0;
}
static unsigned long get_seed(void)
{
int fd = open("/dev/urandom", O_RDONLY);
unsigned long seed = time(NULL);
if (fd < 0)
return seed;
read(fd, &seed, sizeof(seed));
close(fd);
return seed;
}
int main(int argc, char **argv)
{
struct options opt;
if (parse_options(argc, argv, &opt) < 0)
return -1;
srandom(get_seed());
return opt.command->func(&opt);
}