-
Notifications
You must be signed in to change notification settings - Fork 24
/
dominosa.c
1642 lines (1417 loc) · 47.6 KB
/
dominosa.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
/*
* dominosa.c: Domino jigsaw puzzle. Aim to place one of every
* possible domino within a rectangle in such a way that the number
* on each square matches the provided clue.
*/
/*
* TODO:
*
* - improve solver so as to use more interesting forms of
* deduction
*
* * rule out a domino placement if it would divide an unfilled
* region such that at least one resulting region had an odd
* area
* + use b.f.s. to determine the area of an unfilled region
* + a square is unfilled iff it has at least two possible
* placements, and two adjacent unfilled squares are part
* of the same region iff the domino placement joining
* them is possible
*
* * perhaps set analysis
* + look at all unclaimed squares containing a given number
* + for each one, find the set of possible numbers that it
* can connect to (i.e. each neighbouring tile such that
* the placement between it and that neighbour has not yet
* been ruled out)
* + now proceed similarly to Solo set analysis: try to find
* a subset of the squares such that the union of their
* possible numbers is the same size as the subset. If so,
* rule out those possible numbers for all other squares.
* * important wrinkle: the double dominoes complicate
* matters. Connecting a number to itself uses up _two_
* of the unclaimed squares containing a number. Thus,
* when finding the initial subset we must never
* include two adjacent squares; and also, when ruling
* things out after finding the subset, we must be
* careful that we don't rule out precisely the domino
* placement that was _included_ in our set!
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include "puzzles.h"
/* nth triangular number */
#define TRI(n) ( (n) * ((n) + 1) / 2 )
/* number of dominoes for value n */
#define DCOUNT(n) TRI((n)+1)
/* map a pair of numbers to a unique domino index from 0 upwards. */
#define DINDEX(n1,n2) ( TRI(max(n1,n2)) + min(n1,n2) )
#define FLASH_TIME 0.13F
enum {
COL_BACKGROUND,
COL_TEXT,
COL_DOMINO,
COL_DOMINOCLASH,
COL_DOMINOTEXT,
COL_EDGE,
NCOLOURS
};
struct game_params {
int n;
int unique;
};
struct game_numbers {
int refcount;
int *numbers; /* h x w */
};
#define EDGE_L 0x100
#define EDGE_R 0x200
#define EDGE_T 0x400
#define EDGE_B 0x800
struct game_state {
game_params params;
int w, h;
struct game_numbers *numbers;
int *grid;
unsigned short *edges; /* h x w */
int completed, cheated;
};
static game_params *default_params(void)
{
game_params *ret = snew(game_params);
ret->n = 6;
ret->unique = TRUE;
return ret;
}
static int game_fetch_preset(int i, char **name, game_params **params)
{
game_params *ret;
int n;
char buf[80];
switch (i) {
case 0: n = 3; break;
case 1: n = 4; break;
case 2: n = 5; break;
case 3: n = 6; break;
case 4: n = 7; break;
case 5: n = 8; break;
case 6: n = 9; break;
default: return FALSE;
}
sprintf(buf, "Up to double-%d", n);
*name = dupstr(buf);
*params = ret = snew(game_params);
ret->n = n;
ret->unique = TRUE;
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 *params, char const *string)
{
params->n = atoi(string);
while (*string && isdigit((unsigned char)*string)) string++;
if (*string == 'a')
params->unique = FALSE;
}
static char *encode_params(const game_params *params, int full)
{
char buf[80];
sprintf(buf, "%d", params->n);
if (full && !params->unique)
strcat(buf, "a");
return dupstr(buf);
}
static config_item *game_configure(const game_params *params)
{
config_item *ret;
char buf[80];
ret = snewn(3, config_item);
ret[0].name = "Maximum number on dominoes";
ret[0].type = C_STRING;
sprintf(buf, "%d", params->n);
ret[0].sval = dupstr(buf);
ret[0].ival = 0;
ret[1].name = "Ensure unique solution";
ret[1].type = C_BOOLEAN;
ret[1].sval = NULL;
ret[1].ival = params->unique;
ret[2].name = NULL;
ret[2].type = C_END;
ret[2].sval = NULL;
ret[2].ival = 0;
return ret;
}
static game_params *custom_params(const config_item *cfg)
{
game_params *ret = snew(game_params);
ret->n = atoi(cfg[0].sval);
ret->unique = cfg[1].ival;
return ret;
}
static char *validate_params(const game_params *params, int full)
{
if (params->n < 1)
return "Maximum face number must be at least one";
return NULL;
}
/* ----------------------------------------------------------------------
* Solver.
*/
static int find_overlaps(int w, int h, int placement, int *set)
{
int x, y, n;
n = 0; /* number of returned placements */
x = placement / 2;
y = x / w;
x %= w;
if (placement & 1) {
/*
* Horizontal domino, indexed by its left end.
*/
if (x > 0)
set[n++] = placement-2; /* horizontal domino to the left */
if (y > 0)
set[n++] = placement-2*w-1;/* vertical domino above left side */
if (y+1 < h)
set[n++] = placement-1; /* vertical domino below left side */
if (x+2 < w)
set[n++] = placement+2; /* horizontal domino to the right */
if (y > 0)
set[n++] = placement-2*w+2-1;/* vertical domino above right side */
if (y+1 < h)
set[n++] = placement+2-1; /* vertical domino below right side */
} else {
/*
* Vertical domino, indexed by its top end.
*/
if (y > 0)
set[n++] = placement-2*w; /* vertical domino above */
if (x > 0)
set[n++] = placement-2+1; /* horizontal domino left of top */
if (x+1 < w)
set[n++] = placement+1; /* horizontal domino right of top */
if (y+2 < h)
set[n++] = placement+2*w; /* vertical domino below */
if (x > 0)
set[n++] = placement-2+2*w+1;/* horizontal domino left of bottom */
if (x+1 < w)
set[n++] = placement+2*w+1;/* horizontal domino right of bottom */
}
return n;
}
/*
* Returns 0, 1 or 2 for number of solutions. 2 means `any number
* more than one', or more accurately `we were unable to prove
* there was only one'.
*
* Outputs in a `placements' array, indexed the same way as the one
* within this function (see below); entries in there are <0 for a
* placement ruled out, 0 for an uncertain placement, and 1 for a
* definite one.
*/
static int solver(int w, int h, int n, int *grid, int *output)
{
int wh = w*h, dc = DCOUNT(n);
int *placements, *heads;
int i, j, x, y, ret;
/*
* This array has one entry for every possible domino
* placement. Vertical placements are indexed by their top
* half, at (y*w+x)*2; horizontal placements are indexed by
* their left half at (y*w+x)*2+1.
*
* This array is used to link domino placements together into
* linked lists, so that we can track all the possible
* placements of each different domino. It's also used as a
* quick means of looking up an individual placement to see
* whether we still think it's possible. Actual values stored
* in this array are -2 (placement not possible at all), -1
* (end of list), or the array index of the next item.
*
* Oh, and -3 for `not even valid', used for array indices
* which don't even represent a plausible placement.
*/
placements = snewn(2*wh, int);
for (i = 0; i < 2*wh; i++)
placements[i] = -3; /* not even valid */
/*
* This array has one entry for every domino, and it is an
* index into `placements' denoting the head of the placement
* list for that domino.
*/
heads = snewn(dc, int);
for (i = 0; i < dc; i++)
heads[i] = -1;
/*
* Set up the initial possibility lists by scanning the grid.
*/
for (y = 0; y < h-1; y++)
for (x = 0; x < w; x++) {
int di = DINDEX(grid[y*w+x], grid[(y+1)*w+x]);
placements[(y*w+x)*2] = heads[di];
heads[di] = (y*w+x)*2;
}
for (y = 0; y < h; y++)
for (x = 0; x < w-1; x++) {
int di = DINDEX(grid[y*w+x], grid[y*w+(x+1)]);
placements[(y*w+x)*2+1] = heads[di];
heads[di] = (y*w+x)*2+1;
}
#ifdef SOLVER_DIAGNOSTICS
printf("before solver:\n");
for (i = 0; i <= n; i++)
for (j = 0; j <= i; j++) {
int k, m;
m = 0;
printf("%2d [%d %d]:", DINDEX(i, j), i, j);
for (k = heads[DINDEX(i,j)]; k >= 0; k = placements[k])
printf(" %3d [%d,%d,%c]", k, k/2%w, k/2/w, k%2?'h':'v');
printf("\n");
}
#endif
while (1) {
int done_something = FALSE;
/*
* For each domino, look at its possible placements, and
* for each placement consider the placements (of any
* domino) it overlaps. Any placement overlapped by all
* placements of this domino can be ruled out.
*
* Each domino placement overlaps only six others, so we
* need not do serious set theory to work this out.
*/
for (i = 0; i < dc; i++) {
int permset[6], permlen = 0, p;
if (heads[i] == -1) { /* no placement for this domino */
ret = 0; /* therefore puzzle is impossible */
goto done;
}
for (j = heads[i]; j >= 0; j = placements[j]) {
assert(placements[j] != -2);
if (j == heads[i]) {
permlen = find_overlaps(w, h, j, permset);
} else {
int tempset[6], templen, m, n, k;
templen = find_overlaps(w, h, j, tempset);
/*
* Pathetically primitive set intersection
* algorithm, which I'm only getting away with
* because I know my sets are bounded by a very
* small size.
*/
for (m = n = 0; m < permlen; m++) {
for (k = 0; k < templen; k++)
if (tempset[k] == permset[m])
break;
if (k < templen)
permset[n++] = permset[m];
}
permlen = n;
}
}
for (p = 0; p < permlen; p++) {
j = permset[p];
if (placements[j] != -2) {
int p1, p2, di;
done_something = TRUE;
/*
* Rule out this placement. First find what
* domino it is...
*/
p1 = j / 2;
p2 = (j & 1) ? p1 + 1 : p1 + w;
di = DINDEX(grid[p1], grid[p2]);
#ifdef SOLVER_DIAGNOSTICS
printf("considering domino %d: ruling out placement %d"
" for %d\n", i, j, di);
#endif
/*
* ... then walk that domino's placement list,
* removing this placement when we find it.
*/
if (heads[di] == j)
heads[di] = placements[j];
else {
int k = heads[di];
while (placements[k] != -1 && placements[k] != j)
k = placements[k];
assert(placements[k] == j);
placements[k] = placements[j];
}
placements[j] = -2;
}
}
}
/*
* For each square, look at the available placements
* involving that square. If all of them are for the same
* domino, then rule out any placements for that domino
* _not_ involving this square.
*/
for (i = 0; i < wh; i++) {
int list[4], k, n, adi;
x = i % w;
y = i / w;
j = 0;
if (x > 0)
list[j++] = 2*(i-1)+1;
if (x+1 < w)
list[j++] = 2*i+1;
if (y > 0)
list[j++] = 2*(i-w);
if (y+1 < h)
list[j++] = 2*i;
for (n = k = 0; k < j; k++)
if (placements[list[k]] >= -1)
list[n++] = list[k];
adi = -1;
for (j = 0; j < n; j++) {
int p1, p2, di;
k = list[j];
p1 = k / 2;
p2 = (k & 1) ? p1 + 1 : p1 + w;
di = DINDEX(grid[p1], grid[p2]);
if (adi == -1)
adi = di;
if (adi != di)
break;
}
if (j == n) {
int nn;
assert(adi >= 0);
/*
* We've found something. All viable placements
* involving this square are for domino `adi'. If
* the current placement list for that domino is
* longer than n, reduce it to precisely this
* placement list and we've done something.
*/
nn = 0;
for (k = heads[adi]; k >= 0; k = placements[k])
nn++;
if (nn > n) {
done_something = TRUE;
#ifdef SOLVER_DIAGNOSTICS
printf("considering square %d,%d: reducing placements "
"of domino %d\n", x, y, adi);
#endif
/*
* Set all other placements on the list to
* impossible.
*/
k = heads[adi];
while (k >= 0) {
int tmp = placements[k];
placements[k] = -2;
k = tmp;
}
/*
* Set up the new list.
*/
heads[adi] = list[0];
for (k = 0; k < n; k++)
placements[list[k]] = (k+1 == n ? -1 : list[k+1]);
}
}
}
if (!done_something)
break;
}
#ifdef SOLVER_DIAGNOSTICS
printf("after solver:\n");
for (i = 0; i <= n; i++)
for (j = 0; j <= i; j++) {
int k, m;
m = 0;
printf("%2d [%d %d]:", DINDEX(i, j), i, j);
for (k = heads[DINDEX(i,j)]; k >= 0; k = placements[k])
printf(" %3d [%d,%d,%c]", k, k/2%w, k/2/w, k%2?'h':'v');
printf("\n");
}
#endif
ret = 1;
for (i = 0; i < wh*2; i++) {
if (placements[i] == -2) {
if (output)
output[i] = -1; /* ruled out */
} else if (placements[i] != -3) {
int p1, p2, di;
p1 = i / 2;
p2 = (i & 1) ? p1 + 1 : p1 + w;
di = DINDEX(grid[p1], grid[p2]);
if (i == heads[di] && placements[i] == -1) {
if (output)
output[i] = 1; /* certain */
} else {
if (output)
output[i] = 0; /* uncertain */
ret = 2;
}
}
}
done:
/*
* Free working data.
*/
sfree(placements);
sfree(heads);
return ret;
}
/* ----------------------------------------------------------------------
* End of solver code.
*/
static char *new_game_desc(const game_params *params, random_state *rs,
char **aux, int interactive)
{
int n = params->n, w = n+2, h = n+1, wh = w*h;
int *grid, *grid2, *list;
int i, j, k, len;
char *ret;
/*
* Allocate space in which to lay the grid out.
*/
grid = snewn(wh, int);
grid2 = snewn(wh, int);
list = snewn(2*wh, int);
/*
* I haven't been able to think of any particularly clever
* techniques for generating instances of Dominosa with a
* unique solution. Many of the deductions used in this puzzle
* are based on information involving half the grid at a time
* (`of all the 6s, exactly one is next to a 3'), so a strategy
* of partially solving the grid and then perturbing the place
* where the solver got stuck seems particularly likely to
* accidentally destroy the information which the solver had
* used in getting that far. (Contrast with, say, Mines, in
* which most deductions are local so this is an excellent
* strategy.)
*
* Therefore I resort to the basest of brute force methods:
* generate a random grid, see if it's solvable, throw it away
* and try again if not. My only concession to sophistication
* and cleverness is to at least _try_ not to generate obvious
* 2x2 ambiguous sections (see comment below in the domino-
* flipping section).
*
* During tests performed on 2005-07-15, I found that the brute
* force approach without that tweak had to throw away about 87
* grids on average (at the default n=6) before finding a
* unique one, or a staggering 379 at n=9; good job the
* generator and solver are fast! When I added the
* ambiguous-section avoidance, those numbers came down to 19
* and 26 respectively, which is a lot more sensible.
*/
do {
domino_layout_prealloc(w, h, rs, grid, grid2, list);
/*
* Now we have a complete layout covering the whole
* rectangle with dominoes. So shuffle the actual domino
* values and fill the rectangle with numbers.
*/
k = 0;
for (i = 0; i <= params->n; i++)
for (j = 0; j <= i; j++) {
list[k++] = i;
list[k++] = j;
}
shuffle(list, k/2, 2*sizeof(*list), rs);
j = 0;
for (i = 0; i < wh; i++)
if (grid[i] > i) {
/* Optionally flip the domino round. */
int flip = -1;
if (params->unique) {
int t1, t2;
/*
* If we're after a unique solution, we can do
* something here to improve the chances. If
* we're placing a domino so that it forms a
* 2x2 rectangle with one we've already placed,
* and if that domino and this one share a
* number, we can try not to put them so that
* the identical numbers are diagonally
* separated, because that automatically causes
* non-uniqueness:
*
* +---+ +-+-+
* |2 3| |2|3|
* +---+ -> | | |
* |4 2| |4|2|
* +---+ +-+-+
*/
t1 = i;
t2 = grid[i];
if (t2 == t1 + w) { /* this domino is vertical */
if (t1 % w > 0 &&/* and not on the left hand edge */
grid[t1-1] == t2-1 &&/* alongside one to left */
(grid2[t1-1] == list[j] || /* and has a number */
grid2[t1-1] == list[j+1] || /* in common */
grid2[t2-1] == list[j] ||
grid2[t2-1] == list[j+1])) {
if (grid2[t1-1] == list[j] ||
grid2[t2-1] == list[j+1])
flip = 0;
else
flip = 1;
}
} else { /* this domino is horizontal */
if (t1 / w > 0 &&/* and not on the top edge */
grid[t1-w] == t2-w &&/* alongside one above */
(grid2[t1-w] == list[j] || /* and has a number */
grid2[t1-w] == list[j+1] || /* in common */
grid2[t2-w] == list[j] ||
grid2[t2-w] == list[j+1])) {
if (grid2[t1-w] == list[j] ||
grid2[t2-w] == list[j+1])
flip = 0;
else
flip = 1;
}
}
}
if (flip < 0)
flip = random_upto(rs, 2);
grid2[i] = list[j + flip];
grid2[grid[i]] = list[j + 1 - flip];
j += 2;
}
assert(j == k);
} while (params->unique && solver(w, h, n, grid2, NULL) > 1);
#ifdef GENERATION_DIAGNOSTICS
for (j = 0; j < h; j++) {
for (i = 0; i < w; i++) {
putchar('0' + grid2[j*w+i]);
}
putchar('\n');
}
putchar('\n');
#endif
/*
* Encode the resulting game state.
*
* Our encoding is a string of digits. Any number greater than
* 9 is represented by a decimal integer within square
* brackets. We know there are n+2 of every number (it's paired
* with each number from 0 to n inclusive, and one of those is
* itself so that adds another occurrence), so we can work out
* the string length in advance.
*/
/*
* To work out the total length of the decimal encodings of all
* the numbers from 0 to n inclusive:
* - every number has a units digit; total is n+1.
* - all numbers above 9 have a tens digit; total is max(n+1-10,0).
* - all numbers above 99 have a hundreds digit; total is max(n+1-100,0).
* - and so on.
*/
len = n+1;
for (i = 10; i <= n; i *= 10)
len += max(n + 1 - i, 0);
/* Now add two square brackets for each number above 9. */
len += 2 * max(n + 1 - 10, 0);
/* And multiply by n+2 for the repeated occurrences of each number. */
len *= n+2;
/*
* Now actually encode the string.
*/
ret = snewn(len+1, char);
j = 0;
for (i = 0; i < wh; i++) {
k = grid2[i];
if (k < 10)
ret[j++] = '0' + k;
else
j += sprintf(ret+j, "[%d]", k);
assert(j <= len);
}
assert(j == len);
ret[j] = '\0';
/*
* Encode the solved state as an aux_info.
*/
{
char *auxinfo = snewn(wh+1, char);
for (i = 0; i < wh; i++) {
int v = grid[i];
auxinfo[i] = (v == i+1 ? 'L' : v == i-1 ? 'R' :
v == i+w ? 'T' : v == i-w ? 'B' : '.');
}
auxinfo[wh] = '\0';
*aux = auxinfo;
}
sfree(list);
sfree(grid2);
sfree(grid);
return ret;
}
static char *validate_desc(const game_params *params, const char *desc)
{
int n = params->n, w = n+2, h = n+1, wh = w*h;
int *occurrences;
int i, j;
char *ret;
ret = NULL;
occurrences = snewn(n+1, int);
for (i = 0; i <= n; i++)
occurrences[i] = 0;
for (i = 0; i < wh; i++) {
if (!*desc) {
ret = ret ? ret : "Game description is too short";
} else {
if (*desc >= '0' && *desc <= '9')
j = *desc++ - '0';
else if (*desc == '[') {
desc++;
j = atoi(desc);
while (*desc && isdigit((unsigned char)*desc)) desc++;
if (*desc != ']')
ret = ret ? ret : "Missing ']' in game description";
else
desc++;
} else {
j = -1;
ret = ret ? ret : "Invalid syntax in game description";
}
if (j < 0 || j > n)
ret = ret ? ret : "Number out of range in game description";
else
occurrences[j]++;
}
}
if (*desc)
ret = ret ? ret : "Game description is too long";
if (!ret) {
for (i = 0; i <= n; i++)
if (occurrences[i] != n+2)
ret = "Incorrect number balance in game description";
}
sfree(occurrences);
return ret;
}
static game_state *new_game(midend *me, const game_params *params,
const char *desc)
{
int n = params->n, w = n+2, h = n+1, wh = w*h;
game_state *state = snew(game_state);
int i, j;
state->params = *params;
state->w = w;
state->h = h;
state->grid = snewn(wh, int);
for (i = 0; i < wh; i++)
state->grid[i] = i;
state->edges = snewn(wh, unsigned short);
for (i = 0; i < wh; i++)
state->edges[i] = 0;
state->numbers = snew(struct game_numbers);
state->numbers->refcount = 1;
state->numbers->numbers = snewn(wh, int);
for (i = 0; i < wh; i++) {
assert(*desc);
if (*desc >= '0' && *desc <= '9')
j = *desc++ - '0';
else {
assert(*desc == '[');
desc++;
j = atoi(desc);
while (*desc && isdigit((unsigned char)*desc)) desc++;
assert(*desc == ']');
desc++;
}
assert(j >= 0 && j <= n);
state->numbers->numbers[i] = j;
}
state->completed = state->cheated = FALSE;
return state;
}
static game_state *dup_game(const game_state *state)
{
int n = state->params.n, w = n+2, h = n+1, wh = w*h;
game_state *ret = snew(game_state);
ret->params = state->params;
ret->w = state->w;
ret->h = state->h;
ret->grid = snewn(wh, int);
memcpy(ret->grid, state->grid, wh * sizeof(int));
ret->edges = snewn(wh, unsigned short);
memcpy(ret->edges, state->edges, wh * sizeof(unsigned short));
ret->numbers = state->numbers;
ret->numbers->refcount++;
ret->completed = state->completed;
ret->cheated = state->cheated;
return ret;
}
static void free_game(game_state *state)
{
sfree(state->grid);
sfree(state->edges);
if (--state->numbers->refcount <= 0) {
sfree(state->numbers->numbers);
sfree(state->numbers);
}
sfree(state);
}
static char *solve_game(const game_state *state, const game_state *currstate,
const char *aux, char **error)
{
int n = state->params.n, w = n+2, h = n+1, wh = w*h;
int *placements;
char *ret;
int retlen, retsize;
int i, v;
char buf[80];
int extra;
if (aux) {
retsize = 256;
ret = snewn(retsize, char);
retlen = sprintf(ret, "S");
for (i = 0; i < wh; i++) {
if (aux[i] == 'L')
extra = sprintf(buf, ";D%d,%d", i, i+1);
else if (aux[i] == 'T')
extra = sprintf(buf, ";D%d,%d", i, i+w);
else
continue;
if (retlen + extra + 1 >= retsize) {
retsize = retlen + extra + 256;
ret = sresize(ret, retsize, char);
}
strcpy(ret + retlen, buf);
retlen += extra;
}
} else {
placements = snewn(wh*2, int);
for (i = 0; i < wh*2; i++)
placements[i] = -3;
solver(w, h, n, state->numbers->numbers, placements);
/*
* First make a pass putting in edges for -1, then make a pass
* putting in dominoes for +1.
*/
retsize = 256;
ret = snewn(retsize, char);
retlen = sprintf(ret, "S");
for (v = -1; v <= +1; v += 2)
for (i = 0; i < wh*2; i++)
if (placements[i] == v) {
int p1 = i / 2;
int p2 = (i & 1) ? p1+1 : p1+w;
extra = sprintf(buf, ";%c%d,%d",
(int)(v==-1 ? 'E' : 'D'), p1, p2);
if (retlen + extra + 1 >= retsize) {
retsize = retlen + extra + 256;
ret = sresize(ret, retsize, char);
}
strcpy(ret + retlen, buf);
retlen += extra;
}
sfree(placements);
}
return ret;
}
static int game_can_format_as_text_now(const game_params *params)
{
return TRUE;
}
static char *game_text_format(const game_state *state)
{
return NULL;
}
struct game_ui {
int cur_x, cur_y, cur_visible;
};
static game_ui *new_ui(const game_state *state)
{
game_ui *ui = snew(game_ui);
ui->cur_x = ui->cur_y = 0;
ui->cur_visible = 0;
return ui;
}
static void free_ui(game_ui *ui)
{
sfree(ui);
}
static char *encode_ui(const game_ui *ui)
{
return NULL;
}
static void decode_ui(game_ui *ui, const char *encoding)
{
}
static void game_changed_state(game_ui *ui, const game_state *oldstate,
const game_state *newstate)
{
if (!oldstate->completed && newstate->completed)
ui->cur_visible = 0;
}
#define PREFERRED_TILESIZE 32
#define TILESIZE (ds->tilesize)
#define BORDER (TILESIZE * 3 / 4)
#define DOMINO_GUTTER (TILESIZE / 16)
#define DOMINO_RADIUS (TILESIZE / 8)
#define DOMINO_COFFSET (DOMINO_GUTTER + DOMINO_RADIUS)
#define CURSOR_RADIUS (TILESIZE / 4)
#define COORD(x) ( (x) * TILESIZE + BORDER )
#define FROMCOORD(x) ( ((x) - BORDER + TILESIZE) / TILESIZE - 1 )
struct game_drawstate {
int started;