~ubuntu-branches/ubuntu/natty/sgt-puzzles/natty

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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
/*
 * 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
int verbose = 0;
#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) */
    int 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 int 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(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(game_params *params, int 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(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].sval = dupstr(buf);
    ret[0].ival = 0;

    ret[1].name = "Height";
    ret[1].type = C_STRING;
    sprintf(buf, "%d", params->h);
    ret[1].sval = dupstr(buf);
    ret[1].ival = 0;

    ret[2].name = "Difficulty";
    ret[2].type = C_CHOICES;
    ret[2].sval = DIFFCONFIG;
    ret[2].ival = params->diff;

    ret[3].name = NULL;
    ret[3].type = C_END;
    ret[3].sval = NULL;
    ret[3].ival = 0;

    return ret;
}

static game_params *custom_params(config_item *cfg)
{
    game_params *ret = snew(game_params);

    ret->w = atoi(cfg[0].sval);
    ret->h = atoi(cfg[1].sval);
    ret->diff = cfg[2].ival;

    return ret;
}

static char *validate_params(game_params *params, int 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 = state->used_solve = state->impossible = 0;

    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(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(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(game_params *params, char *desc,
                        game_state **sout, char **mout)
{
    game_state *state = blank_game(params->w, params->h);
    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, int 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 int game_can_format_as_text_now(game_params *params)
{
    return TRUE;
}

static char *game_text_format(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 int 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);

    /* mark (all) white regions as an error if there is more than one.
     * may want to make this less in-your-face (by only marking
     * the smallest region as an error, for example -- but what if we
     * have two regions of identical size?) */
    for (i = 0; i < state->n; i++) {
        if (!(state->flags[i] & F_BLACK) &&
            dsf_size(dsf, i) < nwhite) {
            error += 1;
            if (flags & CC_MARK_ERRORS)
                state->flags[i] |= F_ERROR;
        }
    }

    sfree(dsf);
    return (error > 0) ? 0 : 1;
}

static char *game_state_diff(game_state *src, game_state *dst, int 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)",
           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)", x, y));
        state->impossible = 1;
        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)", x, y));
        state->impossible = 1;
        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)!", op.x, op.y));
                state->impossible = 1;
                return n_ops;
            }
            if (!(state->flags[i] & F_BLACK)) {
                debug(("... solver adding black at (%d,%d): %s", 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)!", op.x, op.y));
                state->impossible = 1;
                return n_ops;
            }
            if (!(state->flags[i] & F_CIRCLE)) {
                debug(("... solver adding circle at (%d,%d): %s", 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)", x, y));
                state->impossible = 1;
                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)",
                       state->nums[y1*w + x1], x1, y1, x2, y2));
                debug(("              and: %d at (%d,%d) and (%d,%d)",
                       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 int solve_hassinglewhiteregion(game_state *state, struct solver_state *ss)
{
    int i, j, nwhite = 0, lwhite = -1, szwhite, start, end, next, a, d, x, y;

    for (i = 0; i < state->n; i++) {
        if (!(state->flags[i] & F_BLACK)) {
            nwhite++;
            lwhite = i;
        }
        state->flags[i] &= ~F_SCRATCH;
    }
    if (lwhite == -1) {
        debug(("solve_hassinglewhite: no white squares found!"));
        state->impossible = 1;
        return 0;
    }
    /* We don't use connect_dsf here; it's too slow, and there's a quicker
     * algorithm if all we want is the size of one region. */
    /* Having written this, this algorithm is only about 5% faster than
     * using a dsf. */
    memset(ss->scratch, -1, state->n * sizeof(int));
    ss->scratch[0] = lwhite;
    state->flags[lwhite] |= F_SCRATCH;
    start = 0; end = next = 1;
    while (start < end) {
        for (a = start; a < end; a++) {
            i = ss->scratch[a]; assert(i != -1);
            for (d = 0; d < 4; d++) {
                x = (i % state->w) + dxs[d];
                y = (i / state->w) + dys[d];
                j = y*state->w + x;
                if (!INGRID(state, x, y)) continue;
                if (state->flags[j] & (F_BLACK | F_SCRATCH)) continue;
                ss->scratch[next++] = j;
                state->flags[j] |= F_SCRATCH;
            }
        }
        start = end; end = next;
    }
    szwhite = next;
    return (szwhite == nwhite) ? 1 : 0;
}

static void solve_removesplits_check(game_state *state, struct solver_state *ss,
                                     int x, int y)
{
    int i = y*state->w + x, issingle;

    if (!INGRID(state, x, y)) return;
    if ((state->flags[i] & F_CIRCLE) || (state->flags[i] & F_BLACK))
        return;

    /* If putting a black square at (x,y) would make the white region
     * non-contiguous, it must be circled. */
    state->flags[i] |= F_BLACK;
    issingle = solve_hassinglewhiteregion(state, ss);
    state->flags[i] &= ~F_BLACK;

    if (!issingle)
        solver_op_add(ss, x, y, CIRCLE, "MC: black square here would split white region");
}

/* For all black squares, search in squares diagonally adjacent to see if
 * we can rule out putting a black square there (because it would make the
 * white region non-contiguous). */
/* This function is likely to be somewhat slow. */
static int solve_removesplits(game_state *state, struct solver_state *ss)
{
    int i, x, y, n_ops = ss->n_ops;

    if (!solve_hassinglewhiteregion(state, ss)) {
        debug(("solve_removesplits: white region is not contiguous at start!"));
        state->impossible = 1;
        return 0;
    }

    for (i = 0; i < state->n; i++) {
        if (!(state->flags[i] & F_BLACK)) continue;

        x = i%state->w; y = i/state->w;
        solve_removesplits_check(state, ss, x-1, y-1);
        solve_removesplits_check(state, ss, x+1, y-1);
        solve_removesplits_check(state, ss, x+1, y+1);
        solve_removesplits_check(state, ss, x-1, y+1);
    }
    return ss->n_ops - n_ops;
}

/*
 * This function performs a solver step that isn't implicit in the rules
 * of the game and is thus treated somewhat differently.
 *
 * It marks cells whose number does not exist elsewhere in its row/column
 * with circles. As it happens the game generator here does mean that this
 * is always correct, but it's a solving method that people should not have
 * to rely upon (except in the hidden 'sneaky' difficulty setting) and so
 * all grids at 'tricky' and above are checked to make sure that the grid
 * is no easier if this solving step is performed beforehand.
 *
 * Calling with ss=NULL just returns the number of sneaky deductions that
 * would have been made.
 */
static int solve_sneaky(game_state *state, struct solver_state *ss)
{
    int i, ii, x, xx, y, yy, nunique = 0;

    /* Clear SCRATCH flags. */
    for (i = 0; i < state->n; i++) state->flags[i] &= ~F_SCRATCH;

    for (x = 0; x < state->w; x++) {
        for (y = 0; y < state->h; y++) {
            i = y*state->w + x;

            /* Check for duplicate numbers on our row, mark (both) if so */
            for (xx = x; xx < state->w; xx++) {
                ii = y*state->w + xx;
                if (i == ii) continue;

                if (state->nums[i] == state->nums[ii]) {
                    state->flags[i] |= F_SCRATCH;
                    state->flags[ii] |= F_SCRATCH;
                }
            }

            /* Check for duplicate numbers on our col, mark (both) if so */
            for (yy = y; yy < state->h; yy++) {
                ii = yy*state->w + x;
                if (i == ii) continue;

                if (state->nums[i] == state->nums[ii]) {
                    state->flags[i] |= F_SCRATCH;
                    state->flags[ii] |= F_SCRATCH;
                }
            }
        }
    }

    /* Any cell with no marking has no duplicates on its row or column:
     * set its CIRCLE. */
    for (i = 0; i < state->n; i++) {
        if (!(state->flags[i] & F_SCRATCH)) {
            if (ss) solver_op_add(ss, i%state->w, i/state->w, CIRCLE,
                                  "SNEAKY: only one of its number in row and col");
            nunique += 1;
        } else
            state->flags[i] &= ~F_SCRATCH;
    }
    return nunique;
}

static int solve_specific(game_state *state, int diff, int sneaky)
{
    struct solver_state *ss = solver_state_new(state);

    if (sneaky) solve_sneaky(state, ss);

    /* Some solver operations we only have to perform once --
     * they're only based on the numbers available, and not black
     * squares or circles which may be added later. */

    solve_singlesep(state, ss);        /* never sets impossible */
    solve_doubles(state, ss);          /* ditto */
    solve_corners(state, ss);          /* ditto */

    if (diff >= DIFF_TRICKY)
        solve_offsetpair(state, ss);       /* ditto */

    while (1) {
        if (ss->n_ops > 0) solver_ops_do(state, ss);
        if (state->impossible) break;

        if (solve_allblackbutone(state, ss) > 0) continue;
        if (state->impossible) break;

        if (diff >= DIFF_TRICKY) {
            if (solve_removesplits(state, ss) > 0) continue;
            if (state->impossible) break;
        }

        break;
    }

    solver_state_free(ss);
    return state->impossible ? -1 : check_complete(state, CC_MUST_FILL);
}

static char *solve_game(game_state *state, game_state *currstate,
			char *aux, char **error)
{
    game_state *solved = dup_game(currstate);
    char *move = NULL;

    if (solve_specific(solved, DIFF_ANY, 0) > 0) goto solved;
    free_game(solved);

    solved = dup_game(state);
    if (solve_specific(solved, DIFF_ANY, 0) > 0) goto solved;
    free_game(solved);

    *error = "Unable to solve puzzle.";
    return NULL;

solved:
    move = game_state_diff(currstate, solved, 1);
    free_game(solved);
    return move;
}

/* --- Game generation --- */

/* A correctly completed Hitori board is essentially a latin square
 * (no duplicated numbers in any row or column) with black squares
 * added such that no black square touches another, and the white
 * squares make a contiguous region.
 *
 * So we can generate it by:
   * constructing a latin square
   * adding black squares at random (minding the constraints)
   * altering the numbers under the new black squares such that
      the solver gets a headstart working out where they are.
 */

static int new_game_is_good(game_params *params,
                            game_state *state, game_state *tosolve)
{
    int sret, sret_easy = 0;

    memcpy(tosolve->nums, state->nums, state->n * sizeof(int));
    memset(tosolve->flags, 0, state->n * sizeof(unsigned int));
    tosolve->completed = tosolve->impossible = 0;

    /*
     * We try and solve it twice, once at our requested difficulty level
     * (ensuring it's soluble at all) and once at the level below (if
     * it exists), which we hope to fail: if you can also solve it at
     * the level below then it's too easy and we have to try again.
     *
     * With this puzzle in particular there's an extra finesse, which is
     * that we check that the generated puzzle isn't too easy _with
     * an extra solver step first_, which is the 'sneaky' mode of deductions
     * (asserting that any number which fulfils the latin-square rules
     * on its row/column must be white). This is an artefact of the
     * generation process and not implicit in the rules, so we don't want
     * people to be able to use it to make the puzzle easier.
     */

    assert(params->diff < DIFF_MAX);
    sret = solve_specific(tosolve, params->diff, 0);
    if (params->diff > DIFF_EASY) {
        memset(tosolve->flags, 0, state->n * sizeof(unsigned int));
        tosolve->completed = tosolve->impossible = 0;

        /* this is the only time the 'sneaky' flag is set to 1. */
        sret_easy = solve_specific(tosolve, params->diff-1, 1);
    }

    if (sret <= 0 || sret_easy > 0) {
        debug(("Generated puzzle %s at chosen difficulty %s",
               sret <= 0 ? "insoluble" : "too easy",
               singles_diffnames[params->diff]));
        return 0;
    }
    return 1;
}

#define MAXTRIES 20

static int best_black_col(game_state *state, random_state *rs, int *scratch,
                          int i, int *rownums, int *colnums)
{
    int w = state->w, x = i%w, y = i/w, j, o = state->o;

    /* Randomise the list of numbers to try. */
    for (i = 0; i < o; i++) scratch[i] = i;
    shuffle(scratch, o, sizeof(int), rs);

    /* Try each number in turn, first giving preference to removing
     * latin-square characteristics (i.e. those numbers which only
     * occur once in a row/column). The '&&' here, although intuitively
     * wrong, results in a smaller number of 'sneaky' deductions on
     * solvable boards. */
    for (i = 0; i < o; i++) {
        j = scratch[i] + 1;
        if (rownums[y*o + j-1] == 1 && colnums[x*o + j-1] == 1)
            goto found;
    }

    /* Then try each number in turn returning the first one that's
     * not actually unique in its row/column (see comment below) */
    for (i = 0; i < o; i++) {
        j = scratch[i] + 1;
        if (rownums[y*o + j-1] != 0 || colnums[x*o + j-1] != 0)
            goto found;
    }
    assert(!"unable to place number under black cell.");
    return 0;

found:
    /* Update column and row counts assuming this number will be placed. */
    rownums[y*o + j-1] += 1;
    colnums[x*o + j-1] += 1;
    return j;
}

static char *new_game_desc(game_params *params, random_state *rs,
			   char **aux, int interactive)
{
    game_state *state = blank_game(params->w, params->h);
    game_state *tosolve = blank_game(params->w, params->h);
    int i, j, *scratch, *rownums, *colnums, x, y, ntries;
    int w = state->w, h = state->h, o = state->o;
    char *ret;
    digit *latin;
    struct solver_state *ss = solver_state_new(state);

    scratch = snewn(state->n, int);
    rownums = snewn(h*o, int);
    colnums = snewn(w*o, int);

generate:
    ss->n_ops = 0;
    debug(("Starting game generation, size %dx%d", w, h));

    memset(state->flags, 0, state->n*sizeof(unsigned int));

    /* First, generate the latin rectangle.
     * The order of this, o, is max(w,h). */
    latin = latin_generate_rect(w, h, rs);
    for (i = 0; i < state->n; i++)
        state->nums[i] = (int)latin[i];
    sfree(latin);
    debug_state("State after latin square", state);

    /* Add black squares at random, using bits of solver as we go (to lay
     * white squares), until we can lay no more blacks. */
    for (i = 0; i < state->n; i++)
        scratch[i] = i;
    shuffle(scratch, state->n, sizeof(int), rs);
    for (j = 0; j < state->n; j++) {
        i = scratch[j];
        if ((state->flags[i] & F_CIRCLE) || (state->flags[i] & F_BLACK)) {
            debug(("generator skipping (%d,%d): %s", i%w, i/w,
                   (state->flags[i] & F_CIRCLE) ? "CIRCLE" : "BLACK"));
            continue; /* solver knows this must be one or the other already. */
        }

        /* Add a random black cell... */
        solver_op_add(ss, i%w, i/w, BLACK, "Generator: adding random black cell");
        solver_ops_do(state, ss);

        /* ... and do as well as we know how to lay down whites that are now forced. */
        solve_allblackbutone(state, ss);
        solver_ops_do(state, ss);

        solve_removesplits(state, ss);
        solver_ops_do(state, ss);

        if (state->impossible) {
            debug(("generator made impossible, restarting..."));
            goto generate;
        }
    }
    debug_state("State after adding blacks", state);

    /* Now we know which squares are white and which are black, we lay numbers
     * under black squares at random, except that the number must appear in
     * white cells at least once more in the same column or row as that [black]
     * square. That's necessary to avoid multiple solutions, where blackening
     * squares in the finished puzzle becomes optional. We use two arrays:
     *
     * rownums[ROW * o + NUM-1] is the no. of white cells containing NUM in y=ROW
     * colnums[COL * o + NUM-1] is the no. of white cells containing NUM in x=COL
     */

    memset(rownums, 0, h*o * sizeof(int));
    memset(colnums, 0, w*o * sizeof(int));
    for (i = 0; i < state->n; i++) {
        if (state->flags[i] & F_BLACK) continue;
        j = state->nums[i];
        x = i%w; y = i/w;
        rownums[y * o + j-1] += 1;
        colnums[x * o + j-1] += 1;
    }

    ntries = 0;
randomise:
    for (i = 0; i < state->n; i++) {
        if (!(state->flags[i] & F_BLACK)) continue;
        state->nums[i] = best_black_col(state, rs, scratch, i, rownums, colnums);
    }
    debug_state("State after adding numbers", state);

    /* DIFF_ANY just returns whatever we first generated, for testing purposes. */
    if (params->diff != DIFF_ANY &&
        !new_game_is_good(params, state, tosolve)) {
        ntries++;
        if (ntries > MAXTRIES) {
            debug(("Ran out of randomisation attempts, re-generating."));
            goto generate;
        }
        debug(("Re-randomising numbers under black squares."));
        goto randomise;
    }

    ret = generate_desc(state, 0);

    free_game(tosolve);
    free_game(state);
    solver_state_free(ss);
    sfree(scratch);
    sfree(rownums);
    sfree(colnums);

    return ret;
}

static char *validate_desc(game_params *params, char *desc)
{
    char *ret = NULL;

    unpick_desc(params, desc, NULL, &ret);
    return ret;
}

static game_state *new_game(midend *me, game_params *params, char *desc)
{
    game_state *state = NULL;

    unpick_desc(params, desc, &state, NULL);
    if (!state) assert(!"new_game failed to unpick");
    return state;
}

/* --- Game UI and move routines --- */

struct game_ui {
    int cx, cy, cshow;
    int show_black_nums;
};

static game_ui *new_ui(game_state *state)
{
    game_ui *ui = snew(game_ui);

    ui->cx = ui->cy = ui->cshow = 0;
    ui->show_black_nums = 0;

    return ui;
}

static void free_ui(game_ui *ui)
{
    sfree(ui);
}

static char *encode_ui(game_ui *ui)
{
    return NULL;
}

static void decode_ui(game_ui *ui, char *encoding)
{
}

static void game_changed_state(game_ui *ui, game_state *oldstate,
                               game_state *newstate)
{
    if (!oldstate->completed && newstate->completed)
        ui->cshow = 0;
}

#define DS_BLACK        0x1
#define DS_CIRCLE       0x2
#define DS_CURSOR       0x4
#define DS_BLACK_NUM    0x8
#define DS_ERROR        0x10
#define DS_FLASH        0x20
#define DS_IMPOSSIBLE   0x40

struct game_drawstate {
    int tilesize, started, solved;
    int w, h, n;

    unsigned int *flags;
};

static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
			    int mx, int my, int button)
{
    char buf[80], c;
    int i, x = FROMCOORD(mx), y = FROMCOORD(my);
    enum { NONE, TOGGLE_BLACK, TOGGLE_CIRCLE, UI } action = NONE;

    if (IS_CURSOR_MOVE(button)) {
        move_cursor(button, &ui->cx, &ui->cy, state->w, state->h, 1);
        ui->cshow = 1;
        action = UI;
    } else if (IS_CURSOR_SELECT(button)) {
        x = ui->cx; y = ui->cy;
        if (!ui->cshow) {
            action = UI;
            ui->cshow = 1;
        }
        if (button == CURSOR_SELECT) {
            action = TOGGLE_BLACK;
        } else if (button == CURSOR_SELECT2) {
            action = TOGGLE_CIRCLE;
        }
    } else if (IS_MOUSE_DOWN(button)) {
        if (ui->cshow) {
            ui->cshow = 0;
            action = UI;
        }
        if (!INGRID(state, x, y)) {
            ui->show_black_nums = 1 - ui->show_black_nums;
            action = UI; /* this wants to be a per-game option. */
        } else if (button == LEFT_BUTTON) {
            action = TOGGLE_BLACK;
        } else if (button == RIGHT_BUTTON) {
            action = TOGGLE_CIRCLE;
        }
    }
    if (action == UI) return "";

    if (action == TOGGLE_BLACK || action == TOGGLE_CIRCLE) {
        i = y * state->w + x;
        if (state->flags[i] & (F_BLACK | F_CIRCLE))
            c = 'E';
        else
            c = (action == TOGGLE_BLACK) ? 'B' : 'C';
        sprintf(buf, "%c%d,%d", (int)c, x, y);
        return dupstr(buf);
    }

    return NULL;
}

static game_state *execute_move(game_state *state, char *move)
{
    game_state *ret = dup_game(state);
    int x, y, i, n;

    debug(("move: %s", move));

    while (*move) {
        char c = *move;
        if (c == 'B' || c == 'C' || c == 'E') {
            move++;
            if (sscanf(move, "%d,%d%n", &x, &y, &n) != 2 ||
                !INGRID(state, x, y))
                goto badmove;

            i = y*ret->w + x;
            ret->flags[i] &= ~(F_CIRCLE | F_BLACK); /* empty first, always. */
            if (c == 'B')
                ret->flags[i] |= F_BLACK;
            else if (c == 'C')
                ret->flags[i] |= F_CIRCLE;
            move += n;
        } else if (c == 'S') {
            move++;
            ret->used_solve = 1;
        } else
            goto badmove;

        if (*move == ';')
            move++;
        else if (*move)
            goto badmove;
    }
    if (check_complete(ret, CC_MARK_ERRORS)) ret->completed = 1;
    return ret;

badmove:
    free_game(ret);
    return NULL;
}

/* ----------------------------------------------------------------------
 * Drawing routines.
 */

static void game_compute_size(game_params *params, int tilesize,
			      int *x, int *y)
{
    /* Ick: fake up `ds->tilesize' for macro expansion purposes */
    struct { int tilesize; } ads, *ds = &ads;
    ads.tilesize = tilesize;

    *x = TILE_SIZE * params->w + 2 * BORDER;
    *y = TILE_SIZE * params->h + 2 * BORDER;
}

static void game_set_size(drawing *dr, game_drawstate *ds,
			  game_params *params, int tilesize)
{
    ds->tilesize = tilesize;
}

static float *game_colours(frontend *fe, int *ncolours)
{
    float *ret = snewn(3 * NCOLOURS, float);
    int i;

    game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
    for (i = 0; i < 3; i++) {
        ret[COL_BLACK * 3 + i] = 0.0F;
        ret[COL_BLACKNUM * 3 + i] = 0.4F;
        ret[COL_WHITE * 3 + i] = 1.0F;
        ret[COL_GRID * 3 + i] = ret[COL_LOWLIGHT * 3 + i];
    }
    ret[COL_CURSOR * 3 + 0] = 0.2F;
    ret[COL_CURSOR * 3 + 1] = 0.8F;
    ret[COL_CURSOR * 3 + 2] = 0.0F;

    ret[COL_ERROR * 3 + 0] = 1.0F;
    ret[COL_ERROR * 3 + 1] = 0.0F;
    ret[COL_ERROR * 3 + 2] = 0.0F;

    *ncolours = NCOLOURS;
    return ret;
}

static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
{
    struct game_drawstate *ds = snew(struct game_drawstate);

    ds->tilesize = ds->started = ds->solved = 0;
    ds->w = state->w;
    ds->h = state->h;
    ds->n = state->n;

    ds->flags = snewn(state->n, unsigned int);

    memset(ds->flags, 0, state->n*sizeof(unsigned int));

    return ds;
}

static void game_free_drawstate(drawing *dr, game_drawstate *ds)
{
    sfree(ds->flags);
    sfree(ds);
}

static void tile_redraw(drawing *dr, game_drawstate *ds, int x, int y,
                        int num, unsigned int f)
{
    int tcol, bg, dnum, cx, cy, tsz;
    char buf[32];

    if (f & DS_BLACK) {
        bg = (f & DS_ERROR) ? COL_ERROR : COL_BLACK;
        tcol = COL_BLACKNUM;
        dnum = (f & DS_BLACK_NUM) ? 1 : 0;
    } else {
        bg = (f & DS_FLASH) ? COL_LOWLIGHT : COL_BACKGROUND;
        tcol = (f & DS_ERROR) ? COL_ERROR : COL_BLACK;
        dnum = 1;
    }

    cx = x + TILE_SIZE/2; cy = y + TILE_SIZE/2;

    draw_rect(dr, x,    y, TILE_SIZE, TILE_SIZE, bg);
    draw_rect_outline(dr, x, y, TILE_SIZE, TILE_SIZE,
                      (f & DS_IMPOSSIBLE) ? COL_ERROR : COL_GRID);

    if (f & DS_CIRCLE) {
        draw_circle(dr, cx, cy, CRAD, tcol, tcol);
        draw_circle(dr, cx, cy, CRAD-1, bg, tcol);
    }

    if (dnum) {
        sprintf(buf, "%d", num);
        if (strlen(buf) == 1)
            tsz = TEXTSZ;
        else
            tsz = (CRAD*2 - 1) / strlen(buf);
        draw_text(dr, cx, cy, FONT_VARIABLE, tsz,
                  ALIGN_VCENTRE | ALIGN_HCENTRE, tcol, buf);
    }

    if (f & DS_CURSOR)
        draw_rect_corners(dr, cx, cy, TEXTSZ/2, COL_CURSOR);

    draw_update(dr, x, y, TILE_SIZE, TILE_SIZE);
}

static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
			game_state *state, int dir, game_ui *ui,
			float animtime, float flashtime)
{
    int x, y, i, flash;
    unsigned int f;

    flash = (int)(flashtime * 5 / FLASH_TIME) % 2;

    if (!ds->started) {
        int wsz = TILE_SIZE * state->w + 2 * BORDER;
        int hsz = TILE_SIZE * state->h + 2 * BORDER;
        draw_rect(dr, 0, 0, wsz, hsz, COL_BACKGROUND);
        draw_rect_outline(dr, COORD(0)-1, COORD(0)-1,
			  TILE_SIZE * state->w + 2, TILE_SIZE * state->h + 2,
                          COL_GRID);
        draw_update(dr, 0, 0, wsz, hsz);
    }
    for (x = 0; x < state->w; x++) {
        for (y = 0; y < state->h; y++) {
            i = y*state->w + x;
            f = 0;

            if (flash) f |= DS_FLASH;
            if (state->impossible) f |= DS_IMPOSSIBLE;

            if (ui->cshow && x == ui->cx && y == ui->cy)
                f |= DS_CURSOR;
            if (state->flags[i] & F_BLACK) {
                f |= DS_BLACK;
                if (ui->show_black_nums) f |= DS_BLACK_NUM;
            }
            if (state->flags[i] & F_CIRCLE)
                f |= DS_CIRCLE;
            if (state->flags[i] & F_ERROR)
                f |= DS_ERROR;

            if (!ds->started || ds->flags[i] != f) {
                tile_redraw(dr, ds, COORD(x), COORD(y),
                            state->nums[i], f);
                ds->flags[i] = f;
            }
        }
    }
    ds->started = 1;
}

static float game_anim_length(game_state *oldstate, game_state *newstate,
			      int dir, game_ui *ui)
{
    return 0.0F;
}

static float game_flash_length(game_state *oldstate, game_state *newstate,
			       int dir, game_ui *ui)
{
    if (!oldstate->completed &&
        newstate->completed && !newstate->used_solve)
        return FLASH_TIME;
    return 0.0F;
}

static int game_timing_state(game_state *state, game_ui *ui)
{
    return TRUE;
}

static void game_print_size(game_params *params, float *x, float *y)
{
    int pw, ph;

    /* 8mm squares by default. */
    game_compute_size(params, 800, &pw, &ph);
    *x = pw / 100.0F;
    *y = ph / 100.0F;
}

static void game_print(drawing *dr, game_state *state, int tilesize)
{
    int ink = print_mono_colour(dr, 0);
    int paper = print_mono_colour(dr, 1);
    int x, y, ox, oy, i;
    char buf[32];

    /* Ick: fake up `ds->tilesize' for macro expansion purposes */
    game_drawstate ads, *ds = &ads;
    game_set_size(dr, ds, NULL, tilesize);

    print_line_width(dr, 2 * TILE_SIZE / 40);

    for (x = 0; x < state->w; x++) {
        for (y = 0; y < state->h; y++) {
            ox = COORD(x); oy = COORD(y);
            i = y*state->w+x;

            if (state->flags[i] & F_BLACK) {
                draw_rect(dr, ox, oy, TILE_SIZE, TILE_SIZE, ink);
            } else {
                draw_rect_outline(dr, ox, oy, TILE_SIZE, TILE_SIZE, ink);

                if (state->flags[i] & DS_CIRCLE)
                    draw_circle(dr, ox+TILE_SIZE/2, oy+TILE_SIZE/2, CRAD,
                                paper, ink);

                sprintf(buf, "%d", state->nums[i]);
                draw_text(dr, ox+TILE_SIZE/2, oy+TILE_SIZE/2, FONT_VARIABLE,
                          TEXTSZ/strlen(buf), ALIGN_VCENTRE | ALIGN_HCENTRE,
                          ink, buf);
            }
        }
    }
}

#ifdef COMBINED
#define thegame singles
#endif

const struct game thegame = {
    "Singles", "games.singles", "singles",
    default_params,
    game_fetch_preset,
    decode_params,
    encode_params,
    free_params,
    dup_params,
    TRUE, game_configure, custom_params,
    validate_params,
    new_game_desc,
    validate_desc,
    new_game,
    dup_game,
    free_game,
    TRUE, solve_game,
    TRUE, game_can_format_as_text_now, game_text_format,
    new_ui,
    free_ui,
    encode_ui,
    decode_ui,
    game_changed_state,
    interpret_move,
    execute_move,
    PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
    game_colours,
    game_new_drawstate,
    game_free_drawstate,
    game_redraw,
    game_anim_length,
    game_flash_length,
    TRUE, FALSE, game_print_size, game_print,
    FALSE,			       /* wants_statusbar */
    FALSE, game_timing_state,
    REQUIRE_RBUTTON,		       /* flags */
};

#ifdef STANDALONE_SOLVER

#include <time.h>
#include <stdarg.h>

static void start_soak(game_params *p, random_state *rs)
{
    time_t tt_start, tt_now, tt_last;
    char *desc, *aux;
    game_state *s;
    int i, n = 0, ndiff[DIFF_MAX], diff, sret, nblack = 0, nsneaky = 0;

    tt_start = tt_now = time(NULL);

    printf("Soak-testing a %dx%d grid.\n", p->w, p->h);
    p->diff = DIFF_ANY;

    memset(ndiff, 0, DIFF_MAX * sizeof(int));

    while (1) {
        n++;
        desc = new_game_desc(p, rs, &aux, 0);
        s = new_game(NULL, p, desc);
        nsneaky += solve_sneaky(s, NULL);

        for (diff = 0; diff < DIFF_MAX; diff++) {
            memset(s->flags, 0, s->n * sizeof(unsigned int));
            s->completed = s->impossible = 0;
            sret = solve_specific(s, diff, 0);
            if (sret > 0) {
                ndiff[diff]++;
                break;
            } else if (sret < 0)
                fprintf(stderr, "Impossible! %s\n", desc);
        }
        for (i = 0; i < s->n; i++) {
            if (s->flags[i] & F_BLACK) nblack++;
        }
        free_game(s);
        sfree(desc);

        tt_last = time(NULL);
        if (tt_last > tt_now) {
            tt_now = tt_last;
            printf("%d total, %3.1f/s, bl/sn %3.1f%%/%3.1f%%: ",
                   n, (double)n / ((double)tt_now - tt_start),
                   ((double)nblack * 100.0) / (double)(n * p->w * p->h),
                   ((double)nsneaky * 100.0) / (double)(n * p->w * p->h));
            for (diff = 0; diff < DIFF_MAX; diff++) {
                if (diff > 0) printf(", ");
                printf("%d (%3.1f%%) %s",
                       ndiff[diff], (double)ndiff[diff] * 100.0 / (double)n,
                       singles_diffnames[diff]);
            }
            printf("\n");
        }
    }
}

int main(int argc, char **argv)
{
    char *id = NULL, *desc, *desc_gen = NULL, *tgame, *err, *aux;
    game_state *s = NULL;
    game_params *p = NULL;
    int soln, soak = 0, ret = 1;
    time_t seed = time(NULL);
    random_state *rs = NULL;

    setvbuf(stdout, NULL, _IONBF, 0);

    while (--argc > 0) {
        char *p = *++argv;
        if (!strcmp(p, "-v")) {
            verbose = 1;
        } else if (!strcmp(p, "--soak")) {
            soak = 1;
        } else if (!strcmp(p, "--seed")) {
            if (argc == 0) {
                fprintf(stderr, "%s: --seed needs an argument", argv[0]);
                goto done;
            }
            seed = (time_t)atoi(*++argv);
            argc--;
        } else if (*p == '-') {
            fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p);
            return 1;
        } else {
            id = p;
        }
    }

    rs = random_new((void*)&seed, sizeof(time_t));

    if (!id) {
        fprintf(stderr, "usage: %s [-v] [--soak] <params> | <game_id>\n", argv[0]);
        goto done;
    }
    desc = strchr(id, ':');
    if (desc) *desc++ = '\0';

    p = default_params();
    decode_params(p, id);
    err = validate_params(p, 1);
    if (err) {
        fprintf(stderr, "%s: %s", argv[0], err);
        goto done;
    }

    if (soak) {
        if (desc) {
            fprintf(stderr, "%s: --soak only needs params, not game desc.\n", argv[0]);
            goto done;
        }
        start_soak(p, rs);
    } else {
        if (!desc) desc = desc_gen = new_game_desc(p, rs, &aux, 0);

        err = validate_desc(p, desc);
        if (err) {
            fprintf(stderr, "%s: %s\n", argv[0], err);
            free_params(p);
            goto done;
        }
        s = new_game(NULL, p, desc);

        if (verbose) {
            tgame = game_text_format(s);
            fputs(tgame, stdout);
            sfree(tgame);
        }

        soln = solve_specific(s, DIFF_ANY, 0);
        tgame = game_text_format(s);
        fputs(tgame, stdout);
        sfree(tgame);
        printf("Game was %s.\n\n",
               soln < 0 ? "impossible" : soln > 0 ? "solved" : "not solved");
    }
    ret = 0;

done:
    if (desc_gen) sfree(desc_gen);
    if (p) free_params(p);
    if (s) free_game(s);
    if (rs) random_free(rs);

    return ret;
}

#endif


/* vim: set shiftwidth=4 tabstop=8: */