-
Notifications
You must be signed in to change notification settings - Fork 6
/
singles.c
2028 lines (1733 loc) · 61.2 KB
/
singles.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* singles.c: implementation of Hitori ('let me alone') from Nikoli.
*
* Make single-get able to fetch a specific puzzle ID from menneske.no?
*
* www.menneske.no solving methods:
*
* Done:
* SC: if you circle a cell, any cells in same row/col with same no --> black
* -- solver_op_circle
* SB: if you make a cell black, any cells around it --> white
* -- solver_op_blacken
* ST: 3 identical cells in row, centre is white and outer two black.
* SP: 2 identical cells with single-cell gap, middle cell is white.
* -- solver_singlesep (both ST and SP)
* PI: if you have a pair of same number in row/col, any other
* cells of same number must be black.
* -- solve_doubles
* CC: if you have a black on edge one cell away from corner, cell
* on edge diag. adjacent must be white.
* CE: if you have 2 black cells of triangle on edge, third cell must
* be white.
* QM: if you have 3 black cells of diagonal square in middle, fourth
* cell must be white.
* -- solve_allblackbutone (CC, CE, and QM).
* QC: a corner with 4 identical numbers (or 2 and 2) must have the
* corner cell (and cell diagonal to that) black.
* TC: a corner with 3 identical numbers (with the L either way)
* must have the apex of L black, and other two white.
* DC: a corner with 2 identical numbers in domino can set a white
* cell along wall.
* -- solve_corners (QC, TC, DC)
* IP: pair with one-offset-pair force whites by offset pair
* -- solve_offsetpair
* MC: any cells diag. adjacent to black cells that would split board
* into separate white regions must be white.
* -- solve_removesplits
*
* Still to do:
*
* TEP: 3 pairs of dominos parallel to side, can mark 4 white cells
* alongside.
* DEP: 2 pairs of dominos parallel to side, can mark 2 white cells.
* FI: if you have two sets of double-cells packed together, singles
* in that row/col must be white (qv. PI)
* QuM: four identical cells (or 2 and 2) in middle of grid only have
* two possible solutions each.
* FDE: doubles one row/column away from edge can force a white cell.
* FDM: doubles in centre (next to bits of diag. square) can force a white cell.
* MP: two pairs with same number between force number to black.
* CnC: if circling a cell leads to impossible board, cell is black.
* MC: if we have two possiblilities, can we force a white circle?
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include "puzzles.h"
#include "latin.h"
#ifdef STANDALONE_SOLVER
bool verbose = false;
#endif
#define PREFERRED_TILE_SIZE 32
#define TILE_SIZE (ds->tilesize)
#define BORDER (TILE_SIZE / 2)
#define CRAD ((TILE_SIZE / 2) - 1)
#define TEXTSZ ((14*CRAD/10) - 1) /* 2 * sqrt(2) of CRAD */
#define COORD(x) ( (x) * TILE_SIZE + BORDER )
#define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
#define INGRID(s,x,y) ((x) >= 0 && (x) < (s)->w && (y) >= 0 && (y) < (s)->h)
#define FLASH_TIME 0.7F
enum {
COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT,
COL_BLACK, COL_WHITE, COL_BLACKNUM, COL_GRID,
COL_CURSOR, COL_ERROR,
NCOLOURS
};
struct game_params {
int w, h, diff;
};
#define F_BLACK 0x1
#define F_CIRCLE 0x2
#define F_ERROR 0x4
#define F_SCRATCH 0x8
struct game_state {
int w, h, n, o; /* n = w*h; o = max(w, h) */
bool completed, used_solve, impossible;
int *nums; /* size w*h */
unsigned int *flags; /* size w*h */
};
/* top, right, bottom, left */
static const int dxs[4] = { 0, 1, 0, -1 };
static const int dys[4] = { -1, 0, 1, 0 };
/* --- Game parameters and preset functions --- */
#define DIFFLIST(A) \
A(EASY,Easy,e) \
A(TRICKY,Tricky,k)
#define ENUM(upper,title,lower) DIFF_ ## upper,
#define TITLE(upper,title,lower) #title,
#define ENCODE(upper,title,lower) #lower
#define CONFIG(upper,title,lower) ":" #title
enum { DIFFLIST(ENUM) DIFF_MAX, DIFF_ANY };
static char const *const singles_diffnames[] = { DIFFLIST(TITLE) };
static char const singles_diffchars[] = DIFFLIST(ENCODE);
#define DIFFCOUNT lenof(singles_diffchars)
#define DIFFCONFIG DIFFLIST(CONFIG)
static game_params *default_params(void)
{
game_params *ret = snew(game_params);
ret->w = ret->h = 5;
ret->diff = DIFF_EASY;
return ret;
}
static const struct game_params singles_presets[] = {
{ 5, 5, DIFF_EASY },
{ 5, 5, DIFF_TRICKY },
{ 6, 6, DIFF_EASY },
{ 6, 6, DIFF_TRICKY },
{ 8, 8, DIFF_EASY },
{ 8, 8, DIFF_TRICKY },
{ 10, 10, DIFF_EASY },
{ 10, 10, DIFF_TRICKY },
{ 12, 12, DIFF_EASY },
{ 12, 12, DIFF_TRICKY }
};
static bool game_fetch_preset(int i, char **name, game_params **params)
{
game_params *ret;
char buf[80];
if (i < 0 || i >= lenof(singles_presets))
return false;
ret = default_params();
*ret = singles_presets[i];
*params = ret;
sprintf(buf, "%dx%d %s", ret->w, ret->h, singles_diffnames[ret->diff]);
*name = dupstr(buf);
return true;
}
static void free_params(game_params *params)
{
sfree(params);
}
static game_params *dup_params(const game_params *params)
{
game_params *ret = snew(game_params);
*ret = *params; /* structure copy */
return ret;
}
static void decode_params(game_params *ret, char const *string)
{
char const *p = string;
int i;
ret->w = ret->h = atoi(p);
while (*p && isdigit((unsigned char)*p)) p++;
if (*p == 'x') {
p++;
ret->h = atoi(p);
while (*p && isdigit((unsigned char)*p)) p++;
}
if (*p == 'd') {
ret->diff = DIFF_MAX; /* which is invalid */
p++;
for (i = 0; i < DIFFCOUNT; i++) {
if (*p == singles_diffchars[i])
ret->diff = i;
}
p++;
}
}
static char *encode_params(const game_params *params, bool full)
{
char data[256];
if (full)
sprintf(data, "%dx%dd%c", params->w, params->h, singles_diffchars[params->diff]);
else
sprintf(data, "%dx%d", params->w, params->h);
return dupstr(data);
}
static config_item *game_configure(const game_params *params)
{
config_item *ret;
char buf[80];
ret = snewn(4, config_item);
ret[0].name = "Width";
ret[0].type = C_STRING;
sprintf(buf, "%d", params->w);
ret[0].u.string.sval = dupstr(buf);
ret[1].name = "Height";
ret[1].type = C_STRING;
sprintf(buf, "%d", params->h);
ret[1].u.string.sval = dupstr(buf);
ret[2].name = "Difficulty";
ret[2].type = C_CHOICES;
ret[2].u.choices.choicenames = DIFFCONFIG;
ret[2].u.choices.selected = params->diff;
ret[3].name = NULL;
ret[3].type = C_END;
return ret;
}
static game_params *custom_params(const config_item *cfg)
{
game_params *ret = snew(game_params);
ret->w = atoi(cfg[0].u.string.sval);
ret->h = atoi(cfg[1].u.string.sval);
ret->diff = cfg[2].u.choices.selected;
return ret;
}
static const char *validate_params(const game_params *params, bool full)
{
if (params->w < 2 || params->h < 2)
return "Width and neight must be at least two";
if (params->w > 10+26+26 || params->h > 10+26+26)
return "Puzzle is too large";
if (full) {
if (params->diff < 0 || params->diff >= DIFF_MAX)
return "Unknown difficulty rating";
}
return NULL;
}
/* --- Game description string generation and unpicking --- */
static game_state *blank_game(int w, int h)
{
game_state *state = snew(game_state);
memset(state, 0, sizeof(game_state));
state->w = w;
state->h = h;
state->n = w*h;
state->o = max(w,h);
state->completed = false;
state->used_solve = false;
state->impossible = false;
state->nums = snewn(state->n, int);
state->flags = snewn(state->n, unsigned int);
memset(state->nums, 0, state->n*sizeof(int));
memset(state->flags, 0, state->n*sizeof(unsigned int));
return state;
}
static game_state *dup_game(const game_state *state)
{
game_state *ret = blank_game(state->w, state->h);
ret->completed = state->completed;
ret->used_solve = state->used_solve;
ret->impossible = state->impossible;
memcpy(ret->nums, state->nums, state->n*sizeof(int));
memcpy(ret->flags, state->flags, state->n*sizeof(unsigned int));
return ret;
}
static void free_game(game_state *state)
{
sfree(state->nums);
sfree(state->flags);
sfree(state);
}
static char n2c(int num) {
if (num < 10)
return '0' + num;
else if (num < 10+26)
return 'a' + num - 10;
else
return 'A' + num - 10 - 26;
return '?';
}
static int c2n(char c) {
if (isdigit((unsigned char)c))
return (int)(c - '0');
else if (c >= 'a' && c <= 'z')
return (int)(c - 'a' + 10);
else if (c >= 'A' && c <= 'Z')
return (int)(c - 'A' + 10 + 26);
return -1;
}
static void unpick_desc(const game_params *params, const char *desc,
game_state **sout, const char **mout)
{
game_state *state = blank_game(params->w, params->h);
const char *msg = NULL;
int num = 0, i = 0;
if (strlen(desc) != state->n) {
msg = "Game description is wrong length";
goto done;
}
for (i = 0; i < state->n; i++) {
num = c2n(desc[i]);
if (num <= 0 || num > state->o) {
msg = "Game description contains unexpected characters";
goto done;
}
state->nums[i] = num;
}
done:
if (msg) { /* sth went wrong. */
if (mout) *mout = msg;
free_game(state);
} else {
if (mout) *mout = NULL;
if (sout) *sout = state;
else free_game(state);
}
}
static char *generate_desc(game_state *state, bool issolve)
{
char *ret = snewn(state->n+1+(issolve?1:0), char);
int i, p=0;
if (issolve)
ret[p++] = 'S';
for (i = 0; i < state->n; i++)
ret[p++] = n2c(state->nums[i]);
ret[p] = '\0';
return ret;
}
/* --- Useful game functions (completion, etc.) --- */
static bool game_can_format_as_text_now(const game_params *params)
{
return true;
}
static char *game_text_format(const game_state *state)
{
int len, x, y, i;
char *ret, *p;
len = (state->w)*2; /* one row ... */
len = len * (state->h*2); /* ... h rows, including gaps ... */
len += 1; /* ... final NL */
p = ret = snewn(len, char);
for (y = 0; y < state->h; y++) {
for (x = 0; x < state->w; x++) {
i = y*state->w + x;
if (x > 0) *p++ = ' ';
*p++ = (state->flags[i] & F_BLACK) ? '*' : n2c(state->nums[i]);
}
*p++ = '\n';
for (x = 0; x < state->w; x++) {
i = y*state->w + x;
if (x > 0) *p++ = ' ';
*p++ = (state->flags[i] & F_CIRCLE) ? '~' : ' ';
}
*p++ = '\n';
}
*p++ = '\0';
assert(p - ret == len);
return ret;
}
static void debug_state(const char *desc, game_state *state) {
char *dbg = game_text_format(state);
debug(("%s:\n%s", desc, dbg));
sfree(dbg);
}
static void connect_if_same(game_state *state, int *dsf, int i1, int i2)
{
int c1, c2;
if ((state->flags[i1] & F_BLACK) != (state->flags[i2] & F_BLACK))
return;
c1 = dsf_canonify(dsf, i1);
c2 = dsf_canonify(dsf, i2);
dsf_merge(dsf, c1, c2);
}
static void connect_dsf(game_state *state, int *dsf)
{
int x, y, i;
/* Construct a dsf array for connected blocks; connections
* tracked to right and down. */
dsf_init(dsf, state->n);
for (x = 0; x < state->w; x++) {
for (y = 0; y < state->h; y++) {
i = y*state->w + x;
if (x < state->w-1)
connect_if_same(state, dsf, i, i+1); /* right */
if (y < state->h-1)
connect_if_same(state, dsf, i, i+state->w); /* down */
}
}
}
#define CC_MARK_ERRORS 1
#define CC_MUST_FILL 2
static int check_rowcol(game_state *state, int starti, int di, int sz, unsigned flags)
{
int nerr = 0, n, m, i, j;
/* if any circled numbers have identical non-circled numbers on
* same row/column, error (non-circled)
* if any circled numbers in same column are same number, highlight them.
* if any rows/columns have >1 of same number, not complete. */
for (n = 0, i = starti; n < sz; n++, i += di) {
if (state->flags[i] & F_BLACK) continue;
for (m = n+1, j = i+di; m < sz; m++, j += di) {
if (state->flags[j] & F_BLACK) continue;
if (state->nums[i] != state->nums[j]) continue;
nerr++; /* ok, we have two numbers the same in a row. */
if (!(flags & CC_MARK_ERRORS)) continue;
/* If we have two circles in the same row around
* two identical numbers, they are _both_ wrong. */
if ((state->flags[i] & F_CIRCLE) &&
(state->flags[j] & F_CIRCLE)) {
state->flags[i] |= F_ERROR;
state->flags[j] |= F_ERROR;
}
/* Otherwise, if we have a circle, any other identical
* numbers in that row are obviously wrong. We don't
* highlight this, however, since it makes the process
* of solving the puzzle too easy (you circle a number
* and it promptly tells you which numbers to blacken! */
#if 0
else if (state->flags[i] & F_CIRCLE)
state->flags[j] |= F_ERROR;
else if (state->flags[j] & F_CIRCLE)
state->flags[i] |= F_ERROR;
#endif
}
}
return nerr;
}
static bool check_complete(game_state *state, unsigned flags)
{
int *dsf = snewn(state->n, int);
int x, y, i, error = 0, nwhite, w = state->w, h = state->h;
if (flags & CC_MARK_ERRORS) {
for (i = 0; i < state->n; i++)
state->flags[i] &= ~F_ERROR;
}
connect_dsf(state, dsf);
/* If we're the solver we need the grid all to be definitively
* black or definitively white (i.e. circled) otherwise the solver
* has found an ambiguous grid. */
if (flags & CC_MUST_FILL) {
for (i = 0; i < state->n; i++) {
if (!(state->flags[i] & F_BLACK) && !(state->flags[i] & F_CIRCLE))
error += 1;
}
}
/* Mark any black squares in groups of >1 as errors.
* Count number of white squares. */
nwhite = 0;
for (i = 0; i < state->n; i++) {
if (state->flags[i] & F_BLACK) {
if (dsf_size(dsf, i) > 1) {
error += 1;
if (flags & CC_MARK_ERRORS)
state->flags[i] |= F_ERROR;
}
} else
nwhite += 1;
}
/* Check attributes of white squares, row- and column-wise. */
for (x = 0; x < w; x++) /* check cols from (x,0) */
error += check_rowcol(state, x, w, h, flags);
for (y = 0; y < h; y++) /* check rows from (0,y) */
error += check_rowcol(state, y*w, 1, w, flags);
/* If there's more than one white region, pick the largest one to
* be the canonical one (arbitrarily tie-breaking towards lower
* array indices), and mark all the others as erroneous. */
{
int largest = 0, canonical = -1;
for (i = 0; i < state->n; i++)
if (!(state->flags[i] & F_BLACK)) {
int size = dsf_size(dsf, i);
if (largest < size) {
largest = size;
canonical = i;
}
}
if (largest < nwhite) {
for (i = 0; i < state->n; i++)
if (!(state->flags[i] & F_BLACK) &&
dsf_canonify(dsf, i) != canonical) {
error += 1;
if (flags & CC_MARK_ERRORS)
state->flags[i] |= F_ERROR;
}
}
}
sfree(dsf);
return !(error > 0);
}
static char *game_state_diff(const game_state *src, const game_state *dst,
bool issolve)
{
char *ret = NULL, buf[80], c;
int retlen = 0, x, y, i, k;
unsigned int fmask = F_BLACK | F_CIRCLE;
assert(src->n == dst->n);
if (issolve) {
ret = sresize(ret, 3, char);
ret[0] = 'S'; ret[1] = ';'; ret[2] = '\0';
retlen += 2;
}
for (x = 0; x < dst->w; x++) {
for (y = 0; y < dst->h; y++) {
i = y*dst->w + x;
if ((src->flags[i] & fmask) != (dst->flags[i] & fmask)) {
assert((dst->flags[i] & fmask) != fmask);
if (dst->flags[i] & F_BLACK)
c = 'B';
else if (dst->flags[i] & F_CIRCLE)
c = 'C';
else
c = 'E';
k = sprintf(buf, "%c%d,%d;", (int)c, x, y);
ret = sresize(ret, retlen + k + 1, char);
strcpy(ret + retlen, buf);
retlen += k;
}
}
}
return ret;
}
/* --- Solver --- */
enum { BLACK, CIRCLE };
struct solver_op {
int x, y, op; /* op one of BLACK or CIRCLE. */
const char *desc; /* must be non-malloced. */
};
struct solver_state {
struct solver_op *ops;
int n_ops, n_alloc;
int *scratch;
};
static struct solver_state *solver_state_new(game_state *state)
{
struct solver_state *ss = snew(struct solver_state);
ss->ops = NULL;
ss->n_ops = ss->n_alloc = 0;
ss->scratch = snewn(state->n, int);
return ss;
}
static void solver_state_free(struct solver_state *ss)
{
sfree(ss->scratch);
if (ss->ops) sfree(ss->ops);
sfree(ss);
}
static void solver_op_add(struct solver_state *ss, int x, int y, int op, const char *desc)
{
struct solver_op *sop;
if (ss->n_alloc < ss->n_ops + 1) {
ss->n_alloc = (ss->n_alloc + 1) * 2;
ss->ops = sresize(ss->ops, ss->n_alloc, struct solver_op);
}
sop = &(ss->ops[ss->n_ops++]);
sop->x = x; sop->y = y; sop->op = op; sop->desc = desc;
debug(("added solver op %s ('%s') at (%d,%d)\n",
op == BLACK ? "BLACK" : "CIRCLE", desc, x, y));
}
static void solver_op_circle(game_state *state, struct solver_state *ss,
int x, int y)
{
int i = y*state->w + x;
if (!INGRID(state, x, y)) return;
if (state->flags[i] & F_BLACK) {
debug(("... solver wants to add auto-circle on black (%d,%d)\n", x, y));
state->impossible = true;
return;
}
/* Only add circle op if it's not already circled. */
if (!(state->flags[i] & F_CIRCLE)) {
solver_op_add(ss, x, y, CIRCLE, "SB - adjacent to black square");
}
}
static void solver_op_blacken(game_state *state, struct solver_state *ss,
int x, int y, int num)
{
int i = y*state->w + x;
if (!INGRID(state, x, y)) return;
if (state->nums[i] != num) return;
if (state->flags[i] & F_CIRCLE) {
debug(("... solver wants to add auto-black on circled(%d,%d)\n", x, y));
state->impossible = true;
return;
}
/* Only add black op if it's not already black. */
if (!(state->flags[i] & F_BLACK)) {
solver_op_add(ss, x, y, BLACK, "SC - number on same row/col as circled");
}
}
static int solver_ops_do(game_state *state, struct solver_state *ss)
{
int next_op = 0, i, x, y, n_ops = 0;
struct solver_op op;
/* Care here: solver_op_* may call solver_op_add which may extend the
* ss->n_ops. */
while (next_op < ss->n_ops) {
op = ss->ops[next_op++]; /* copy this away, it may get reallocated. */
i = op.y*state->w + op.x;
if (op.op == BLACK) {
if (state->flags[i] & F_CIRCLE) {
debug(("Solver wants to blacken circled square (%d,%d)!\n", op.x, op.y));
state->impossible = true;
return n_ops;
}
if (!(state->flags[i] & F_BLACK)) {
debug(("... solver adding black at (%d,%d): %s\n", op.x, op.y, op.desc));
#ifdef STANDALONE_SOLVER
if (verbose)
printf("Adding black at (%d,%d): %s\n", op.x, op.y, op.desc);
#endif
state->flags[i] |= F_BLACK;
/*debug_state("State after adding black", state);*/
n_ops++;
solver_op_circle(state, ss, op.x-1, op.y);
solver_op_circle(state, ss, op.x+1, op.y);
solver_op_circle(state, ss, op.x, op.y-1);
solver_op_circle(state, ss, op.x, op.y+1);
}
} else {
if (state->flags[i] & F_BLACK) {
debug(("Solver wants to circle blackened square (%d,%d)!\n", op.x, op.y));
state->impossible = true;
return n_ops;
}
if (!(state->flags[i] & F_CIRCLE)) {
debug(("... solver adding circle at (%d,%d): %s\n", op.x, op.y, op.desc));
#ifdef STANDALONE_SOLVER
if (verbose)
printf("Adding circle at (%d,%d): %s\n", op.x, op.y, op.desc);
#endif
state->flags[i] |= F_CIRCLE;
/*debug_state("State after adding circle", state);*/
n_ops++;
for (x = 0; x < state->w; x++) {
if (x != op.x)
solver_op_blacken(state, ss, x, op.y, state->nums[i]);
}
for (y = 0; y < state->h; y++) {
if (y != op.y)
solver_op_blacken(state, ss, op.x, y, state->nums[i]);
}
}
}
}
ss->n_ops = 0;
return n_ops;
}
/* If the grid has two identical numbers with one cell between them, the inner
* cell _must_ be white (and thus circled); (at least) one of the two must be
* black (since they're in the same column or row) and thus the middle cell is
* next to a black cell. */
static int solve_singlesep(game_state *state, struct solver_state *ss)
{
int x, y, i, ir, irr, id, idd, n_ops = ss->n_ops;
for (x = 0; x < state->w; x++) {
for (y = 0; y < state->h; y++) {
i = y*state->w + x;
/* Cell two to our right? */
ir = i + 1; irr = ir + 1;
if (x < (state->w-2) &&
state->nums[i] == state->nums[irr] &&
!(state->flags[ir] & F_CIRCLE)) {
solver_op_add(ss, x+1, y, CIRCLE, "SP/ST - between identical nums");
}
/* Cell two below us? */
id = i + state->w; idd = id + state->w;
if (y < (state->h-2) &&
state->nums[i] == state->nums[idd] &&
!(state->flags[id] & F_CIRCLE)) {
solver_op_add(ss, x, y+1, CIRCLE, "SP/ST - between identical nums");
}
}
}
return ss->n_ops - n_ops;
}
/* If we have two identical numbers next to each other (in a row or column),
* any other identical numbers in that column must be black. */
static int solve_doubles(game_state *state, struct solver_state *ss)
{
int x, y, i, ii, n_ops = ss->n_ops, xy;
for (y = 0, i = 0; y < state->h; y++) {
for (x = 0; x < state->w; x++, i++) {
assert(i == y*state->w+x);
if (state->flags[i] & F_BLACK) continue;
ii = i+1; /* check cell to our right. */
if (x < (state->w-1) &&
!(state->flags[ii] & F_BLACK) &&
state->nums[i] == state->nums[ii]) {
for (xy = 0; xy < state->w; xy++) {
if (xy == x || xy == (x+1)) continue;
if (state->nums[y*state->w + xy] == state->nums[i] &&
!(state->flags[y*state->w + xy] & F_BLACK))
solver_op_add(ss, xy, y, BLACK, "PI - same row as pair");
}
}
ii = i+state->w; /* check cell below us */
if (y < (state->h-1) &&
!(state->flags[ii] & F_BLACK) &&
state->nums[i] == state->nums[ii]) {
for (xy = 0; xy < state->h; xy++) {
if (xy == y || xy == (y+1)) continue;
if (state->nums[xy*state->w + x] == state->nums[i] &&
!(state->flags[xy*state->w + x] & F_BLACK))
solver_op_add(ss, x, xy, BLACK, "PI - same col as pair");
}
}
}
}
return ss->n_ops - n_ops;
}
/* If a white square has all-but-one possible adjacent squares black, the
* one square left over must be white. */
static int solve_allblackbutone(game_state *state, struct solver_state *ss)
{
int x, y, i, n_ops = ss->n_ops, xd, yd, id, ifree;
int dis[4], d;
dis[0] = -state->w;
dis[1] = 1;
dis[2] = state->w;
dis[3] = -1;
for (y = 0, i = 0; y < state->h; y++) {
for (x = 0; x < state->w; x++, i++) {
assert(i == y*state->w+x);
if (state->flags[i] & F_BLACK) continue;
ifree = -1;
for (d = 0; d < 4; d++) {
xd = x + dxs[d]; yd = y + dys[d]; id = i + dis[d];
if (!INGRID(state, xd, yd)) continue;
if (state->flags[id] & F_CIRCLE)
goto skip; /* this cell already has a way out */
if (!(state->flags[id] & F_BLACK)) {
if (ifree != -1)
goto skip; /* this cell has >1 white cell around it. */
ifree = id;
}
}
if (ifree != -1)
solver_op_add(ss, ifree%state->w, ifree/state->w, CIRCLE,
"CC/CE/QM: white cell with single non-black around it");
else {
debug(("White cell with no escape at (%d,%d)\n", x, y));
state->impossible = true;
return 0;
}
skip: ;
}
}
return ss->n_ops - n_ops;
}
/* If we have 4 numbers the same in a 2x2 corner, the far corner and the
* diagonally-adjacent square must both be black.
* If we have 3 numbers the same in a 2x2 corner, the apex of the L
* thus formed must be black.
* If we have 2 numbers the same in a 2x2 corner, the non-same cell
* one away from the corner must be white. */
static void solve_corner(game_state *state, struct solver_state *ss,
int x, int y, int dx, int dy)
{
int is[4], ns[4], xx, yy, w = state->w;
for (yy = 0; yy < 2; yy++) {
for (xx = 0; xx < 2; xx++) {
is[yy*2+xx] = (y + dy*yy) * w + (x + dx*xx);
ns[yy*2+xx] = state->nums[is[yy*2+xx]];
}
} /* order is now (corner, side 1, side 2, inner) */
if (ns[0] == ns[1] && ns[0] == ns[2] && ns[0] == ns[3]) {
solver_op_add(ss, is[0]%w, is[0]/w, BLACK, "QC: corner with 4 matching");
solver_op_add(ss, is[3]%w, is[3]/w, BLACK, "QC: corner with 4 matching");
} else if (ns[0] == ns[1] && ns[0] == ns[2]) {
/* corner and 2 sides: apex is corner. */
solver_op_add(ss, is[0]%w, is[0]/w, BLACK, "TC: corner apex from 3 matching");
} else if (ns[1] == ns[2] && ns[1] == ns[3]) {
/* side, side, fourth: apex is fourth. */
solver_op_add(ss, is[3]%w, is[3]/w, BLACK, "TC: inside apex from 3 matching");
} else if (ns[0] == ns[1] || ns[1] == ns[3]) {
/* either way here we match the non-identical side. */
solver_op_add(ss, is[2]%w, is[2]/w, CIRCLE, "DC: corner with 2 matching");
} else if (ns[0] == ns[2] || ns[2] == ns[3]) {
/* ditto */
solver_op_add(ss, is[1]%w, is[1]/w, CIRCLE, "DC: corner with 2 matching");
}
}
static int solve_corners(game_state *state, struct solver_state *ss)
{
int n_ops = ss->n_ops;
solve_corner(state, ss, 0, 0, 1, 1);
solve_corner(state, ss, state->w-1, 0, -1, 1);
solve_corner(state, ss, state->w-1, state->h-1, -1, -1);
solve_corner(state, ss, 0, state->h-1, 1, -1);
return ss->n_ops - n_ops;
}
/* If you have the following situation:
* ...
* ...x A x x y A x...
* ...x B x x B y x...
* ...
* then both squares marked 'y' must be white. One of the left-most A or B must
* be white (since two side-by-side black cells are disallowed), which means
* that the corresponding right-most A or B must be black (since you can't
* have two of the same number on one line); thus, the adjacent squares
* to that right-most A or B must be white, which include the two marked 'y'
* in either case.
* Obviously this works in any row or column. It also works if A == B.
* It doesn't work for the degenerate case:
* ...x A A x x
* ...x B y x x
* where the square marked 'y' isn't necessarily white (consider the left-most A
* is black).
*
* */
static void solve_offsetpair_pair(game_state *state, struct solver_state *ss,
int x1, int y1, int x2, int y2)
{
int ox, oy, w = state->w, ax, ay, an, d, dx[2], dy[2], dn, xd, yd;
if (x1 == x2) { /* same column */
ox = 1; oy = 0;
} else {
assert(y1 == y2);
ox = 0; oy = 1;
}
/* We try adjacent to (x1,y1) and the two diag. adjacent to (x2, y2).
* We expect to be called twice, once each way around. */
ax = x1+ox; ay = y1+oy;
assert(INGRID(state, ax, ay));
an = state->nums[ay*w + ax];
dx[0] = x2 + ox + oy; dx[1] = x2 + ox - oy;
dy[0] = y2 + oy + ox; dy[1] = y2 + oy - ox;
for (d = 0; d < 2; d++) {
if (INGRID(state, dx[d], dy[d]) && (dx[d] != ax || dy[d] != ay)) {
/* The 'dx != ax || dy != ay' removes the degenerate case,
* mentioned above. */
dn = state->nums[dy[d]*w + dx[d]];
if (an == dn) {
/* We have a match; so (WLOG) the 'A' marked above are at
* (x1,y1) and (x2,y2), and the 'B' are at (ax,ay) and (dx,dy). */
debug(("Found offset-pair: %d at (%d,%d) and (%d,%d)\n",
state->nums[y1*w + x1], x1, y1, x2, y2));
debug((" and: %d at (%d,%d) and (%d,%d)\n",
an, ax, ay, dx[d], dy[d]));
xd = dx[d] - x2; yd = dy[d] - y2;
solver_op_add(ss, x2 + xd, y2, CIRCLE, "IP: next to offset-pair");
solver_op_add(ss, x2, y2 + yd, CIRCLE, "IP: next to offset-pair");
}
}
}
}
static int solve_offsetpair(game_state *state, struct solver_state *ss)
{
int n_ops = ss->n_ops, x, xx, y, yy, n1, n2;
for (x = 0; x < state->w-1; x++) {
for (y = 0; y < state->h; y++) {
n1 = state->nums[y*state->w + x];
for (yy = y+1; yy < state->h; yy++) {
n2 = state->nums[yy*state->w + x];
if (n1 == n2) {
solve_offsetpair_pair(state, ss, x, y, x, yy);
solve_offsetpair_pair(state, ss, x, yy, x, y);
}
}
}
}
for (y = 0; y < state->h-1; y++) {
for (x = 0; x < state->w; x++) {
n1 = state->nums[y*state->w + x];
for (xx = x+1; xx < state->w; xx++) {
n2 = state->nums[y*state->w + xx];
if (n1 == n2) {
solve_offsetpair_pair(state, ss, x, y, xx, y);
solve_offsetpair_pair(state, ss, xx, y, x, y);
}
}
}
}
return ss->n_ops - n_ops;
}
static bool solve_hassinglewhiteregion(
game_state *state, struct solver_state *ss)