-
Notifications
You must be signed in to change notification settings - Fork 3
/
abcd.c
2235 lines (1861 loc) · 49 KB
/
abcd.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
/*
* abcd.c: Implementation for ABCD Puzzles.
* (C) 2011 Lennard Sprong
* Created for Simon Tatham's Portable Puzzle Collection
* See LICENCE for licence details
*
* More information about the puzzle type:
* http://wiki.logic-masters.de/index.php?title=ABCD_Puzzle/en
* http://www.janko.at/Raetsel/AbcKombi/index.htm
*
* Objective of the game: Place one letter in each square. The numbers indicate
* the amount of letters in each row and column.
* Identical letters may not touch each other.
*/
/*
* TODO:
*
* - Get large puzzles to have a lower fail ratio.
* I haven't currently been able to produce a valid 10x10n4 puzzle,
* and a 9x9n4 puzzle can take _tens of thousands_ of attempts.
* + Force a 0 on a row/column for a letter, and a high number on
* a column/row?
* + Or maybe introduce immutable letters, after a certain amount
* of attempts...
*
* - Solver techniques for diagonal mode?
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include "puzzles.h"
#ifdef STANDALONE_SOLVER
bool solver_verbose = false;
#endif
enum {
COL_OUTERBG, COL_INNERBG,
COL_GRID,
COL_BORDERLETTER, COL_TEXT,
COL_GUESS, COL_ERROR, COL_PENCIL,
COL_HIGHLIGHT, COL_LOWLIGHT,
NCOLOURS
};
struct game_params {
int w, h, n;
bool diag; /* disallow diagonal adjacent letters */
bool removenums; /* incomplete clue set */
};
struct game_state {
int w, h, n;
bool diag;
char *grid; /* size w*h */
unsigned char *clues; /* remaining possibilities, size w*h*n */
int *numbers; /* size n*(w+h) */
bool completed, cheated;
};
#define PREFERRED_TILE_SIZE 36
#define FLASH_TIME 0.7F
#define FLASH_FRAME 0.1F
#define CUBOID(x,y,i) ( (i) + ((x)*n) + ((y)*n*w) )
#define HOR_CLUE(y,i) ( (i) + ((y)*n) )
#define VER_CLUE(x,i) ( HOR_CLUE( (x) +h, (i) ) )
#define EMPTY 127
#define NO_NUMBER -1
const struct game_params abcd_presets[] = {
{4, 4, 4, false, false},
{4, 4, 4, false, true},
{5, 5, 4, false, false},
{5, 5, 4, false, true},
{6, 6, 4, false, false},
{7, 7, 3, false, false},
{7, 7, 4, false, false},
};
static bool game_fetch_preset(int i, char **name, game_params **params)
{
if (i < 0 || i >= lenof(abcd_presets))
return false;
game_params *ret = snew(game_params);
*ret = abcd_presets[i]; /* struct copy */
*params = ret;
char buf[80];
sprintf(buf, "%dx%d, %d letters %s", ret->w, ret->h, ret->n,
ret->diag ? "No diagonals" : ret->removenums ? "Hard" : "Easy");
*name = dupstr(buf);
return true;
}
static game_params *default_params(void)
{
game_params *ret = snew(game_params);
*ret = abcd_presets[2]; /* struct copy */
return ret;
}
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; /* struct copy */
return ret;
}
static void decode_params(game_params *ret, char const *string)
{
/* Find width */
ret->w = ret->h = atoi(string);
while (*string && isdigit((unsigned char) *string)) ++string;
/* Find height */
if (*string == 'x')
{
++string;
ret->h = atoi(string);
}
while (*string && isdigit((unsigned char) *string)) ++string;
/* Find number of letters */
if (*string == 'n')
{
++string;
ret->n = atoi(string);
}
while (*string && isdigit((unsigned char) *string)) ++string;
/* Find Diagonal flag */
ret->diag = false;
if (*string == 'D')
{
ret->diag = true;
++string;
}
/* Find Remove clues flag */
ret->removenums = false;
if (*string == 'R')
{
ret->removenums = true;
++string;
}
}
static char *encode_params(const game_params *params, bool full)
{
char data[256];
sprintf(data, "%dx%dn%d", params->w, params->h, params->n);
if(params->diag)
sprintf(data + strlen(data), "D");
if(full && params->removenums)
sprintf(data + strlen(data), "R");
return dupstr(data);
}
static config_item *game_configure(const game_params *params)
{
config_item *ret;
char buf[80];
ret = snewn(6, 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 = "Letters";
ret[2].type = C_STRING;
sprintf(buf, "%d", params->n);
ret[2].u.string.sval = dupstr(buf);
ret[3].name = "Remove clues";
ret[3].type = C_BOOLEAN;
ret[3].u.boolean.bval = params->removenums;
ret[4].name = "Allow diagonal touching";
ret[4].type = C_BOOLEAN;
ret[4].u.boolean.bval = !params->diag;
ret[5].name = NULL;
ret[5].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->n = atoi(cfg[2].u.string.sval);
ret->removenums = cfg[3].u.boolean.bval;
ret->diag = !cfg[4].u.boolean.bval;
return ret;
}
static const char *validate_params(const game_params *params, bool full)
{
/* A width or height under 2 could possibly break the solver */
if (params->w < 2) return "Width must be at least 2";
if (params->h < 2) return "Height must be at least 2";
/*
* It is actually possible for puzzles with 2 letters to exist, but they're
* not really interesting. There are also no puzzles with unique solutions
* for an even x even grid with 2 letters. Puzzles with 1 letter and more
* than one cell don't exist.
*/
if (params->n < 3 && !params->diag) return "Letters must be at least 3";
/*
* Diagonal puzzles with 4 letters do exist, however using 4 letters will
* almost certainly break the generator. It doesn't seem worth the effort
* to make a special case for this configuration.
* Anything under 4 letters can't avoid violating the no-neighbor rule.
*/
if (params->n < 5 && params->diag) return "Letters for Diagonal mode must be at least 5";
/*
* This limit is actually fairly arbitrary, but I'd rather avoid clashing
* with hotkeys in the midend. It also fits nicely with the keypad.
*/
if (params->n > 9) return "Letters must be no more than 9";
return NULL;
}
static const char *validate_desc(const game_params *params, const char *desc)
{
int w = params->w;
int h = params->h;
int n = params->n;
int l = w+h;
int num;
int i = 0;
const char *p = desc;
while(*p)
{
if(isdigit((unsigned char) *p))
{
/* Number clue */
num = atoi(p);
while (*p && isdigit((unsigned char) *p)) ++p;
/* A clue which can't possibly fit should be blocked. */
if((i < h*n && num > 1+(w/2)) || (i >= h*n && num > 1+(h/2)))
return "Description contains invalid number clue.";
++i;
}
else if (*p == '-')
{
/* Hidden number clue */
++i;
++p;
}
else if (*p == ',')
{
/* Commas can be skipped over */
++p;
}
else
{
return "Invalid character in description.";
}
}
if(i < l*n)
return "Description contains not enough clues.";
else if (i > l*n)
return "Description contains too many clues.";
return NULL;
}
static game_state *blank_state(int w, int h, int n, bool diag)
{
int l = w + h;
game_state *ret = snew(game_state);
ret->w = w;
ret->h = h;
ret->n = n;
ret->diag = diag;
ret->grid = snewn(w * h, char);
ret->clues = snewn(w * h * n, unsigned char);
ret->numbers = snewn(l * n, int);
memset(ret->grid, EMPTY, w * h);
memset(ret->clues, true, w*h*n);
memset(ret->numbers, 0, l*n * sizeof(int));
ret->completed = ret->cheated = false;
return ret;
}
static key_label *game_request_keys(const game_params *params, int *nkeys)
{
int i;
int n = params->n;
key_label *keys = snewn(n + 1, key_label);
*nkeys = n + 1;
for (i = 0; i < n; i++)
{
keys[i].button = 'A' + i;
keys[i].label = NULL;
}
keys[n].button = '\b';
keys[n].label = NULL;
return keys;
}
static game_state *new_game(midend *me, const game_params *params, const char *desc)
{
int w = params->w;
int h = params->h;
int n = params->n;
bool diag = params->diag;
game_state *state = blank_state(w, h, n, diag);
/* Disable all clues for user interaction */
memset(state->clues, false, w*h*n);
const char *p = desc;
int num;
int i = 0;
while(*p)
{
if(isdigit((unsigned char) *p))
{
num = atoi(p);
state->numbers[i++] = num;
while (*p && isdigit((unsigned char) *p)) ++p;
}
else if (*p == '-')
{
state->numbers[i++] = NO_NUMBER;
++p;
}
else
++p;
}
return state;
}
static game_state *dup_game(const game_state *state)
{
int w = state->w;
int h = state->h;
int n = state->n;
bool diag = state->diag;
int l = w + h;
game_state *ret = blank_state(w, h, n, diag);
memcpy(ret->grid, state->grid, w * h);
memcpy(ret->clues, state->clues, w * h * n);
memcpy(ret->numbers, state->numbers, l * n * sizeof(int));
ret->completed = state->completed;
ret->cheated = state->cheated;
return ret;
}
static void free_game(game_state *state)
{
sfree(state->grid);
sfree(state->clues);
sfree(state->numbers);
sfree(state);
}
static bool game_can_format_as_text_now(const game_params *params)
{
/*
* Puzzles with a width or height of 19 or more could contain a clue number
* of 2 digits, which isn't supported by the format.
*/
return (params->w < 19 && params->h < 19);
}
static char *game_text_format(const game_state *state)
{
char *ret;
int w = state->w;
int h = state->h;
int n = state->n;
int i,j;
char c;
int rw = (w+n)*2 + 1;
int rh = h+n+2;
int len = (rw * rh) + 1;
/* Create return string, and fill it with spaces */
ret = snewn(len, char);
memset(ret, ' ', len);
/* Place newlines */
for(i = 0; i < rh; i++)
ret[rw*(i+1)-1] = '\n';
/* Place NUL */
ret[len-1] = '\0';
/* Place letters in topleft corner */
for(i = 0; i < n; i++)
{
ret[rw*(n-1) + i*2] = 'A' + i; /* horizontal */
ret[rw*i + (n-1)*2] = 'A' + i; /* vertical */
}
/* Place top clues */
for(i = 0; i < w; i++)
for(j = 0; j < n; j++)
{
if (state->numbers[VER_CLUE(i,j)] == NO_NUMBER)
continue;
ret[rw*j + n*2 + i*2] = '0' + state->numbers[VER_CLUE(i,j)];
}
/* Place left clues */
for(i = 0; i < h; i++)
for(j = 0; j < n; j++)
{
if (state->numbers[HOR_CLUE(i,j)] == NO_NUMBER)
continue;
ret[rw*(i+n+1) + j*2] = '0' + state->numbers[HOR_CLUE(i,j)];
}
/*
* Place outline corners. We use a subtle visual cue here to differentiate
* between puzzles where diagonal touching is allowed or disallowed.
*/
c = state->diag ? '*' : '+';
ret[rw*n + n*2 - 1] = c; /* top left */
ret[rw*(n+1) - 2] = c; /* top right */
ret[rw*(n+h+1) + n*2 - 1] = c; /* bottom left */
ret[rw*(n+h+2) - 2] = c; /* bottom right */
/* Place horizontal borders */
for (i = 0; i < (w*2)-1; i++)
{
ret[rw*n + n*2 + i] = '-'; /* top */
ret[rw*(n+h+1) + n*2 + i] = '-'; /* bottom */
}
/* Place vertical borders */
for (i = 0; i < h; i++)
{
ret[rw*(n+i+1) + n*2 - 1] = '|'; /* left */
ret[rw*(n+i+2) - 2] = '|'; /* right */
}
/* Place letters */
for (i = 0; i < h; i++)
for(j = 0; j < w; j++)
{
c = state->grid[i*w+j];
ret[rw*(n+i+1) + (n+j)*2] = (c != EMPTY ? 'A' + c : '.');
}
return ret;
}
static char *abcd_format_letters(const game_state *state, char solve)
{
/*
* Formats all entered letters to a single string.
* Used for making the Solve move, as well as debugging purposes.
*/
char *ret, *p;
int i;
int w = state->w;
int h = state->h;
ret = snewn(w*h * 2,char);
p = ret;
if(solve)
*p++ = 'S';
for(i = 0; i < w*h; i++)
{
*p++ = (state->grid[i] != EMPTY ? 'A' + state->grid[i] : '.');
}
*p++ = '\0';
ret = sresize(ret, p - ret, char);
return ret;
}
static void abcd_place_letter(game_state *state, int x, int y, char l, int *remaining)
{
/*
* Place one letter in the grid, rule the letter out for all adjacent squares,
* and subtract the corresponding remaining values (if available)
*/
int w = state->w;
int h = state->h;
int n = state->n;
bool diag = state->diag;
int pos = y*w+x;
char i;
/* Place letter in grid */
state->grid[pos] = l;
/* Rule out all other letters in square */
for (i = 0; i < n; i++)
{
if (i == l)
continue;
state->clues[CUBOID(x,y,i)] = false;
}
/* Remove possibility for adjacent squares */
if (diag && x > 0 && y > 0)
state->clues[CUBOID(x-1,y-1,l)] = false;
if (diag && x < w-1 && y > 0)
state->clues[CUBOID(x+1,y-1,l)] = false;
if (diag && x > 0 && y < h-1)
state->clues[CUBOID(x-1,y+1,l)] = false;
if (diag && x < w-1 && y < h-1)
state->clues[CUBOID(x+1,y+1,l)] = false;
if (x > 0)
state->clues[CUBOID(x-1,y,l)] = false;
if (x < w-1)
state->clues[CUBOID(x+1,y,l)] = false;
if (y > 0)
state->clues[CUBOID(x,y-1,l)] = false;
if (y < h-1)
state->clues[CUBOID(x,y+1,l)] = false;
/* Update remaining array if entered */
if (remaining)
{
if (remaining[HOR_CLUE(y,l)] != NO_NUMBER)
remaining[HOR_CLUE(y,l)]--;
if (remaining[VER_CLUE(x,l)] != NO_NUMBER)
remaining[VER_CLUE(x,l)]--;
}
}
static int abcd_solver_runs(game_state *state, int *remaining, int horizontal, char c)
{
/*
* For each row and column, get the available runs of spaces where
* a certain letter can be placed. These are used to determine the
* maximum amount of letters which can be placed in this row/column.
* We can use this information to confirm several letters.
*
* Example:
* #.###.##
* (# is a square with letter A as a possibility)
* These are three runs, with size 1, 3 and 2 respectively. The maximum
* amount of A's which can be placed without violating the no-neighbour
* rule is 4. If the amount of A's we needed to place was 4, we could
* confirm these letters:
* A.A.A...
* For the run of size 2, we can't determine which position the A
* should go.
*/
int w = state->w;
int h = state->h;
int n = state->n;
int x = 0, y = 0;
int a,b,amx,bmx,i,point;
int action = false;
int *rslen;
int *rspos;
amx = (horizontal ? h : w);
bmx = (horizontal ? w : h);
rslen = snewn(bmx, int);
rspos = snewn(bmx, int);
for (a = 0; a < amx; a++)
{
if (horizontal)
y = a;
else
x = a;
int req = (horizontal ? remaining[HOR_CLUE(y,c)] : remaining[VER_CLUE(x,c)]);
if(req == NO_NUMBER || req == 0)
continue;
point = 0;
memset(rslen, 0, bmx*sizeof(int));
memset(rspos, 0, bmx*sizeof(int));
/* Collect all runs and their starting positions */
for (b = 0; b < bmx; b++)
{
if (horizontal)
x = b;
else
y = b;
if(state->clues[CUBOID(x,y,c)] && state->grid[y*w+x] == EMPTY)
{
if(rslen[point] == 0)
rspos[point] = b;
rslen[point]++;
}
else
{
if(rslen[point] != 0)
point++;
}
}
/* Make sure the point is on an index with no run */
if(rslen[point] != 0)
point++;
/*
* Get the maximum amount of letters. This is length/2 + 1 for odd lengths,
* and length/2 for even lengths.
*/
int maxletters = 0;
for(i = 0; i < point; i++)
maxletters += (rslen[i] / 2) + (rslen[i] & 1);
/*
* If the maximum amount of letters is also the required amount,
* we place letters on all runs with an odd length
*/
if (maxletters == req)
{
for (i = 0; i < point; i++)
if (rslen[i] & 1)
{
action = true;
for (b = rspos[i]; b <= rspos[i] + rslen[i]; b+=2)
{
x = (horizontal ? b : x);
y = (horizontal ? y : b);
#ifdef STANDALONE_SOLVER
if(solver_verbose)
printf("Solver: Run on %s %i confirms %c at %i,%i\n",
horizontal ? "Row" : "Column",
a, 'A'+c, x+1,y+1);
#endif
abcd_place_letter(state, x, y, c, remaining);
}
}
}
/* TODO techniques involving diagonal adjacency */
}
sfree(rslen);
sfree(rspos);
return action;
}
static int abcd_validate_adjacency(game_state *state, int sx, int sy, int ex, int ey, int dx, int dy)
{
/* Returns true if no adjacency error was found with the directional data. */
int w = state->w;
int x,y,px,py;
for (x = sx; x < ex; x++)
for (y = sy; y < ey; y++)
{
px = x+dx;
py = y+dy;
if (state->grid[y*w+x] != EMPTY && state->grid[y*w+x] == state->grid[py*w+px])
return false;
}
return true;
}
static int abcd_validate_clues(game_state *state, int horizontal)
{
/*
* Returns 1 if a clue is not yet satisfied,
* -1 if a clue is overcrowded, and 0 if all clues are satisfied.
*/
int w = state->w;
int h = state->h;
int n = state->n;
int a,amx,b,bmx,i;
int found, pos, clue, gridpos;
int error = 0;
amx = (horizontal ? h : w);
bmx = (horizontal ? w : h);
for (a = 0; a < amx; a++)
for (i = 0; i < n; i++)
{
found = 0;
pos = (horizontal ? HOR_CLUE(a,i) : VER_CLUE(a,i));
clue = state->numbers[pos];
if (clue == NO_NUMBER)
continue;
for(b = 0; b < bmx; b++)
{
gridpos = (horizontal ? a*w+b : b*w+a);
if(state->grid[gridpos] == i)
found++;
}
if (found < clue)
error = (error == 0 ? 1 : error);
else if (found > clue)
error = -1;
}
return error;
}
static int abcd_validate_puzzle(game_state *state)
{
int w = state->w;
int h = state->h;
bool diag = state->diag;
/* Check for clue violations */
int invalid;
invalid = abcd_validate_clues(state,true);
if(invalid == -1)
return -1;
if(invalid == 1)
{
int invalid2 = abcd_validate_clues(state,false);
if(invalid2 == -1)
return invalid2;
}
else
{
invalid = abcd_validate_clues(state,false);
if(invalid == -1)
return invalid;
}
/* Check for adjacency violations */
if(!abcd_validate_adjacency(state, 0, 0, w-1, h, 1, 0))
return -1;
if(!abcd_validate_adjacency(state, 0, 0, w, h-1, 0, 1))
return -1;
if(diag && !abcd_validate_adjacency(state, 0, 0, w-1, h-1, 1, 1))
return -1;
if(diag && !abcd_validate_adjacency(state, 0, 1, w-1, h, 1, -1))
return -1;
/*
* If no validators found a critical error,
* but not all numbers are satisfied, return now
*/
if (invalid == 1)
return invalid;
/* Finally, make sure all squares are entered. */
int x,y;
for (x = 0; x < w; x++)
for (y = 0; y < h; y++)
{
if(state->grid[y*w+x] == EMPTY)
return 1;
}
/* Puzzle solved */
return 0;
}
#define MULTIPLE 126
static int abcd_solve_game(int *numbers, game_state *state)
{
/*
* Changes the *state to contain the number clues from *numbers and the found
* solution. Returns an integer with an error code.
* 0 is no error
* 1 is multiple solutions
* -1 is no solution
*/
int w = state->w;
int h = state->h;
int n = state->n;
int l = w + h;
int x,y;
char c;
int *remaining;
int busy = true;
int error = 0;
#ifdef STANDALONE_SOLVER
char *debug;
#endif
/* Create a new game. Copy only the number clues and parameters */
memcpy(state->numbers, numbers, l*n * sizeof(int));
/*
* Create an editable copy of the numbers, with the amount of
* remaining letters per row/column.
*/
remaining = snewn(l * n, int);
memcpy(remaining, state->numbers, l*n * sizeof(int));
/* Enter loop */
while(busy && error == 0)
{
busy = false;
/*
* Check for all letters if the number is satisfied in each row/column.
* Rule out this letter in all squares of this row/column,
* then remove the number from the remaining array to speed up the rest
* of the solving process.
*/
for (c = 0; c < n; c++)
{
/* Check rows */
for (y = 0; y < h; y++)
if(remaining[HOR_CLUE(y,c)] == 0)
{
#ifdef STANDALONE_SOLVER
if(solver_verbose)
printf("Solver: %c satisfied for Row %i \n", 'A'+c, y+1);
#endif
busy = true;
remaining[HOR_CLUE(y,c)] = NO_NUMBER;
for (x = 0; x < w; x++)
state->clues[CUBOID(x,y,c)] = false;
}
/* Check cols */
for (x = 0; x < w; x++)
if(remaining[VER_CLUE(x,c)] == 0)
{
#ifdef STANDALONE_SOLVER
if(solver_verbose)
printf("Solver: %c satisfied for Column %i \n", 'A'+c, x+1);
#endif
busy = true;
remaining[VER_CLUE(x,c)] = NO_NUMBER;
for (y = 0; y < h; y++)
state->clues[CUBOID(x,y,c)] = false;
}
}
/* END CHECK */
/*
* Check for single remaining possibility in one square
*/
for (y = 0; y < h; y++)
for (x = 0; x < w; x++)
{
if (state->grid[y*w+x] != EMPTY)
continue;
/* Get the single possibility, or MULTIPLE if there's multiple */
char let = EMPTY;
for (c = 0; c < n; c++)
{
if (state->clues[CUBOID(x,y,c)])
let = (let == EMPTY ? c : MULTIPLE);
}
/* There must be at least one remaining possibility */
if (let == EMPTY)
error = -1;
else if (let != MULTIPLE)
{
/* One possibility found */
#ifdef STANDALONE_SOLVER
if(solver_verbose)
printf("Solver: Single possibility %c on %i,%i\n", 'A'+let, x+1,y+1);
#endif
busy = true;
abcd_place_letter(state, x, y, let, remaining);
}
}
/* END CHECK */
/*
* If something has been done at this point,
* reuse the easier techniques before continuing
*/
if(busy)
continue;
/*
* Try the runs techniques on all rows and columns for all letters
*/
for (c = 0; c < n; c++)
{
int action;
action = abcd_solver_runs(state, remaining, true, c);
busy = (action ? true : busy);
action = abcd_solver_runs(state, remaining, false, c);
busy = (action ? true : busy);
}
/* END CHECK */
} /* while busy */
#ifdef STANDALONE_SOLVER
if(solver_verbose)
{
debug = abcd_format_letters(state, false);
printf("Solver letters: %s \n", abcd_format_letters(state, false));
sfree(debug);
}
#endif
/* Check if the puzzle has been solved. */
if (error == 0)
error = abcd_validate_puzzle(state);
#ifdef STANDALONE_SOLVER
if(solver_verbose)
{
printf("Solver result: %s \n\n",
error == 0 ? "Success" : error == 1 ? "No solution found" : "Error"
);
}
#endif
sfree(remaining);
return error;
}
#undef MULTIPLE
static char *solve_game(const game_state *state, const game_state *currstate,
const char *aux, const char **error)
{
if(aux)