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
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
|
/*
* slide.c: Implementation of the block-sliding puzzle `Klotski'.
*/
/*
* TODO:
*
* - Improve the generator.
* * actually, we seem to be mostly sensible already now. I
* want more choice over the type of main block and location
* of the exit/target, and I think I probably ought to give
* up on compactness and just bite the bullet and have the
* target area right outside the main wall, but mostly I
* think it's OK.
* * the move limit tends to make the game _slower_ to
* generate, which is odd. Perhaps investigate why.
*
* - Improve the graphics.
* * All the colours are a bit wishy-washy. _Some_ dark
* colours would surely not be excessive? Probably darken
* the tiles, the walls and the main block, and leave the
* target marker pale.
* * The cattle grid effect is still disgusting. Think of
* something completely different.
* * The highlight for next-piece-to-move in the solver is
* excessive, and the shadow blends in too well with the
* piece lowlights. Adjust both.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include "puzzles.h"
#include "tree234.h"
/*
* The implementation of this game revolves around the insight
* which makes an exhaustive-search solver feasible: although
* there are many blocks which can be rearranged in many ways, any
* two blocks of the same shape are _indistinguishable_ and hence
* the number of _distinct_ board layouts is generally much
* smaller. So we adopt a representation for board layouts which
* is inherently canonical, i.e. there are no two distinct
* representations which encode indistinguishable layouts.
*
* The way we do this is to encode each square of the board, in
* the normal left-to-right top-to-bottom order, as being one of
* the following things:
* - the first square (in the given order) of a block (`anchor')
* - special case of the above: the anchor for the _main_ block
* (i.e. the one which the aim of the game is to get to the
* target position)
* - a subsequent square of a block whose previous square was N
* squares ago
* - an impassable wall
*
* (We also separately store data about which board positions are
* forcefields only passable by the main block. We can't encode
* that in the main board data, because then the main block would
* destroy forcefields as it went over them.)
*
* Hence, for example, a 2x2 square block would be encoded as
* ANCHOR, followed by DIST(1), and w-2 squares later on there
* would be DIST(w-1) followed by DIST(1). So if you start at the
* last of those squares, the DIST numbers give you a linked list
* pointing back through all the other squares in the same block.
*
* So the solver simply does a bfs over all reachable positions,
* encoding them in this format and storing them in a tree234 to
* ensure it doesn't ever revisit an already-analysed position.
*/
enum {
/*
* The colours are arranged here so that every base colour is
* directly followed by its highlight colour and then its
* lowlight colour. Do not break this, or draw_tile() will get
* confused.
*/
COL_BACKGROUND,
COL_HIGHLIGHT,
COL_LOWLIGHT,
COL_DRAGGING,
COL_DRAGGING_HIGHLIGHT,
COL_DRAGGING_LOWLIGHT,
COL_MAIN,
COL_MAIN_HIGHLIGHT,
COL_MAIN_LOWLIGHT,
COL_MAIN_DRAGGING,
COL_MAIN_DRAGGING_HIGHLIGHT,
COL_MAIN_DRAGGING_LOWLIGHT,
COL_TARGET,
COL_TARGET_HIGHLIGHT,
COL_TARGET_LOWLIGHT,
NCOLOURS
};
/*
* Board layout is a simple array of bytes. Each byte holds:
*/
#define ANCHOR 255 /* top-left-most square of some piece */
#define MAINANCHOR 254 /* anchor of _main_ piece */
#define EMPTY 253 /* empty square */
#define WALL 252 /* immovable wall */
#define MAXDIST 251
/* all other values indicate distance back to previous square of same block */
#define ISDIST(x) ( (unsigned char)((x)-1) <= MAXDIST-1 )
#define DIST(x) (x)
#define ISANCHOR(x) ( (x)==ANCHOR || (x)==MAINANCHOR )
#define ISBLOCK(x) ( ISANCHOR(x) || ISDIST(x) )
/*
* MAXDIST is the largest DIST value we can encode. This must
* therefore also be the maximum puzzle width in theory (although
* solver running time will dictate a much smaller limit in
* practice).
*/
#define MAXWID MAXDIST
struct game_params {
int w, h;
int maxmoves;
};
struct game_immutable_state {
int refcount;
unsigned char *forcefield;
};
struct game_solution {
int nmoves;
int *moves; /* just like from solve_board() */
int refcount;
};
struct game_state {
int w, h;
unsigned char *board;
int tx, ty; /* target coords for MAINANCHOR */
int minmoves; /* for display only */
int lastmoved, lastmoved_pos; /* for move counting */
int movecount;
int completed;
int cheated;
struct game_immutable_state *imm;
struct game_solution *soln;
int soln_index;
};
static game_params *default_params(void)
{
game_params *ret = snew(game_params);
ret->w = 7;
ret->h = 6;
ret->maxmoves = 40;
return ret;
}
static const struct game_params slide_presets[] = {
{7, 6, 25},
{7, 6, -1},
{8, 6, -1},
};
static int game_fetch_preset(int i, char **name, game_params **params)
{
game_params *ret;
char str[80];
if (i < 0 || i >= lenof(slide_presets))
return FALSE;
ret = snew(game_params);
*ret = slide_presets[i];
sprintf(str, "%dx%d", ret->w, ret->h);
if (ret->maxmoves >= 0)
sprintf(str + strlen(str), ", max %d moves", ret->maxmoves);
else
sprintf(str + strlen(str), ", no move limit");
*name = dupstr(str);
*params = ret;
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->w = params->h = atoi(string);
while (*string && isdigit((unsigned char)*string)) string++;
if (*string == 'x') {
string++;
params->h = atoi(string);
while (*string && isdigit((unsigned char)*string)) string++;
}
if (*string == 'm') {
string++;
params->maxmoves = atoi(string);
while (*string && isdigit((unsigned char)*string)) string++;
} else if (*string == 'u') {
string++;
params->maxmoves = -1;
}
}
static char *encode_params(const game_params *params, int full)
{
char data[256];
sprintf(data, "%dx%d", params->w, params->h);
if (params->maxmoves >= 0)
sprintf(data + strlen(data), "m%d", params->maxmoves);
else
sprintf(data + strlen(data), "u");
return dupstr(data);
}
static config_item *game_configure(const game_params *params)
{
config_item *ret;
char buf[80];
ret = snewn(4, config_item);
ret[0].name = "Width";
ret[0].type = C_STRING;
sprintf(buf, "%d", params->w);
ret[0].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 = "Solution length limit";
ret[2].type = C_STRING;
sprintf(buf, "%d", params->maxmoves);
ret[2].sval = dupstr(buf);
ret[2].ival = 0;
ret[3].name = NULL;
ret[3].type = C_END;
ret[3].sval = NULL;
ret[3].ival = 0;
return ret;
}
static game_params *custom_params(const config_item *cfg)
{
game_params *ret = snew(game_params);
ret->w = atoi(cfg[0].sval);
ret->h = atoi(cfg[1].sval);
ret->maxmoves = atoi(cfg[2].sval);
return ret;
}
static char *validate_params(const game_params *params, int full)
{
if (params->w > MAXWID)
return "Width must be at most " STR(MAXWID);
if (params->w < 5)
return "Width must be at least 5";
if (params->h < 4)
return "Height must be at least 4";
return NULL;
}
static char *board_text_format(int w, int h, unsigned char *data,
unsigned char *forcefield)
{
int wh = w*h;
int *dsf = snew_dsf(wh);
int i, x, y;
int retpos, retlen = (w*2+2)*(h*2+1)+1;
char *ret = snewn(retlen, char);
for (i = 0; i < wh; i++)
if (ISDIST(data[i]))
dsf_merge(dsf, i - data[i], i);
retpos = 0;
for (y = 0; y < 2*h+1; y++) {
for (x = 0; x < 2*w+1; x++) {
int v;
int i = (y/2)*w+(x/2);
#define dtype(i) (ISBLOCK(data[i]) ? \
dsf_canonify(dsf, i) : data[i])
#define dchar(t) ((t)==EMPTY ? ' ' : (t)==WALL ? '#' : \
data[t] == MAINANCHOR ? '*' : '%')
if (y % 2 && x % 2) {
int j = dtype(i);
v = dchar(j);
} else if (y % 2 && !(x % 2)) {
int j1 = (x > 0 ? dtype(i-1) : -1);
int j2 = (x < 2*w ? dtype(i) : -1);
if (j1 != j2)
v = '|';
else
v = dchar(j1);
} else if (!(y % 2) && (x % 2)) {
int j1 = (y > 0 ? dtype(i-w) : -1);
int j2 = (y < 2*h ? dtype(i) : -1);
if (j1 != j2)
v = '-';
else
v = dchar(j1);
} else {
int j1 = (x > 0 && y > 0 ? dtype(i-w-1) : -1);
int j2 = (x > 0 && y < 2*h ? dtype(i-1) : -1);
int j3 = (x < 2*w && y > 0 ? dtype(i-w) : -1);
int j4 = (x < 2*w && y < 2*h ? dtype(i) : -1);
if (j1 == j2 && j2 == j3 && j3 == j4)
v = dchar(j1);
else if (j1 == j2 && j3 == j4)
v = '|';
else if (j1 == j3 && j2 == j4)
v = '-';
else
v = '+';
}
assert(retpos < retlen);
ret[retpos++] = v;
}
assert(retpos < retlen);
ret[retpos++] = '\n';
}
assert(retpos < retlen);
ret[retpos++] = '\0';
assert(retpos == retlen);
return ret;
}
/* ----------------------------------------------------------------------
* Solver.
*/
/*
* During solver execution, the set of visited board positions is
* stored as a tree234 of the following structures. `w', `h' and
* `data' are obvious in meaning; `dist' represents the minimum
* distance to reach this position from the starting point.
*
* `prev' links each board to the board position from which it was
* most efficiently derived.
*/
struct board {
int w, h;
int dist;
struct board *prev;
unsigned char *data;
};
static int boardcmp(void *av, void *bv)
{
struct board *a = (struct board *)av;
struct board *b = (struct board *)bv;
return memcmp(a->data, b->data, a->w * a->h);
}
static struct board *newboard(int w, int h, unsigned char *data)
{
struct board *b = malloc(sizeof(struct board) + w*h);
b->data = (unsigned char *)b + sizeof(struct board);
memcpy(b->data, data, w*h);
b->w = w;
b->h = h;
b->dist = -1;
b->prev = NULL;
return b;
}
/*
* The actual solver. Given a board, attempt to find the minimum
* length of move sequence which moves MAINANCHOR to (tx,ty), or
* -1 if no solution exists. Returns that minimum length.
*
* Also, if `moveout' is provided, writes out the moves in the
* form of a sequence of pairs of integers indicating the source
* and destination points of the anchor of the moved piece in each
* move. Exactly twice as many integers are written as the number
* returned from solve_board(), and `moveout' receives an int *
* which is a pointer to a dynamically allocated array.
*/
static int solve_board(int w, int h, unsigned char *board,
unsigned char *forcefield, int tx, int ty,
int movelimit, int **moveout)
{
int wh = w*h;
struct board *b, *b2, *b3;
int *next, *anchors, *which;
int *movereached, *movequeue, mqhead, mqtail;
tree234 *sorted, *queue;
int i, j, dir;
int qlen, lastdist;
int ret;
#ifdef SOLVER_DIAGNOSTICS
{
char *t = board_text_format(w, h, board);
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
int c = board[i*w+j];
if (ISDIST(c))
printf("D%-3d", c);
else if (c == MAINANCHOR)
printf("M ");
else if (c == ANCHOR)
printf("A ");
else if (c == WALL)
printf("W ");
else if (c == EMPTY)
printf("E ");
}
printf("\n");
}
printf("Starting solver for:\n%s\n", t);
sfree(t);
}
#endif
sorted = newtree234(boardcmp);
queue = newtree234(NULL);
b = newboard(w, h, board);
b->dist = 0;
add234(sorted, b);
addpos234(queue, b, 0);
qlen = 1;
next = snewn(wh, int);
anchors = snewn(wh, int);
which = snewn(wh, int);
movereached = snewn(wh, int);
movequeue = snewn(wh, int);
lastdist = -1;
while ((b = delpos234(queue, 0)) != NULL) {
qlen--;
if (movelimit >= 0 && b->dist >= movelimit) {
/*
* The problem is not soluble in under `movelimit'
* moves, so we can quit right now.
*/
b2 = NULL;
goto done;
}
if (b->dist != lastdist) {
#ifdef SOLVER_DIAGNOSTICS
printf("dist %d (%d)\n", b->dist, count234(sorted));
#endif
lastdist = b->dist;
}
/*
* Find all the anchors and form a linked list of the
* squares within each block.
*/
for (i = 0; i < wh; i++) {
next[i] = -1;
anchors[i] = FALSE;
which[i] = -1;
if (ISANCHOR(b->data[i])) {
anchors[i] = TRUE;
which[i] = i;
} else if (ISDIST(b->data[i])) {
j = i - b->data[i];
next[j] = i;
which[i] = which[j];
}
}
/*
* For each anchor, do an array-based BFS to find all the
* places we can slide it to.
*/
for (i = 0; i < wh; i++) {
if (!anchors[i])
continue;
mqhead = mqtail = 0;
for (j = 0; j < wh; j++)
movereached[j] = FALSE;
movequeue[mqtail++] = i;
while (mqhead < mqtail) {
int pos = movequeue[mqhead++];
/*
* Try to move in each direction from here.
*/
for (dir = 0; dir < 4; dir++) {
int dx = (dir == 0 ? -1 : dir == 1 ? +1 : 0);
int dy = (dir == 2 ? -1 : dir == 3 ? +1 : 0);
int offset = dy*w + dx;
int newpos = pos + offset;
int d = newpos - i;
/*
* For each square involved in this block,
* check to see if the square d spaces away
* from it is either empty or part of the same
* block.
*/
for (j = i; j >= 0; j = next[j]) {
int jy = (pos+j-i) / w + dy, jx = (pos+j-i) % w + dx;
if (jy >= 0 && jy < h && jx >= 0 && jx < w &&
((b->data[j+d] == EMPTY || which[j+d] == i) &&
(b->data[i] == MAINANCHOR || !forcefield[j+d])))
/* ok */;
else
break;
}
if (j >= 0)
continue; /* this direction wasn't feasible */
/*
* If we've already tried moving this piece
* here, leave it.
*/
if (movereached[newpos])
continue;
movereached[newpos] = TRUE;
movequeue[mqtail++] = newpos;
/*
* We have a viable move. Make it.
*/
b2 = newboard(w, h, b->data);
for (j = i; j >= 0; j = next[j])
b2->data[j] = EMPTY;
for (j = i; j >= 0; j = next[j])
b2->data[j+d] = b->data[j];
b3 = add234(sorted, b2);
if (b3 != b2) {
sfree(b2); /* we already got one */
} else {
b2->dist = b->dist + 1;
b2->prev = b;
addpos234(queue, b2, qlen++);
if (b2->data[ty*w+tx] == MAINANCHOR)
goto done; /* search completed! */
}
}
}
}
}
b2 = NULL;
done:
if (b2) {
ret = b2->dist;
if (moveout) {
/*
* Now b2 represents the solved position. Backtrack to
* output the solution.
*/
*moveout = snewn(ret * 2, int);
j = ret * 2;
while (b2->prev) {
int from = -1, to = -1;
b = b2->prev;
/*
* Scan b and b2 to find out which piece has
* moved.
*/
for (i = 0; i < wh; i++) {
if (ISANCHOR(b->data[i]) && !ISANCHOR(b2->data[i])) {
assert(from == -1);
from = i;
} else if (!ISANCHOR(b->data[i]) && ISANCHOR(b2->data[i])){
assert(to == -1);
to = i;
}
}
assert(from >= 0 && to >= 0);
assert(j >= 2);
(*moveout)[--j] = to;
(*moveout)[--j] = from;
b2 = b;
}
assert(j == 0);
}
} else {
ret = -1; /* no solution */
if (moveout)
*moveout = NULL;
}
freetree234(queue);
while ((b = delpos234(sorted, 0)) != NULL)
sfree(b);
freetree234(sorted);
sfree(next);
sfree(anchors);
sfree(movereached);
sfree(movequeue);
sfree(which);
return ret;
}
/* ----------------------------------------------------------------------
* Random board generation.
*/
static void generate_board(int w, int h, int *rtx, int *rty, int *minmoves,
random_state *rs, unsigned char **rboard,
unsigned char **rforcefield, int movelimit)
{
int wh = w*h;
unsigned char *board, *board2, *forcefield;
unsigned char *tried_merge;
int *dsf;
int *list, nlist, pos;
int tx, ty;
int i, j;
int moves = 0; /* placate optimiser */
/*
* Set up a board and fill it with singletons, except for a
* border of walls.
*/
board = snewn(wh, unsigned char);
forcefield = snewn(wh, unsigned char);
board2 = snewn(wh, unsigned char);
memset(board, ANCHOR, wh);
memset(forcefield, FALSE, wh);
for (i = 0; i < w; i++)
board[i] = board[i+w*(h-1)] = WALL;
for (i = 0; i < h; i++)
board[i*w] = board[i*w+(w-1)] = WALL;
tried_merge = snewn(wh * wh, unsigned char);
memset(tried_merge, 0, wh*wh);
dsf = snew_dsf(wh);
/*
* Invent a main piece at one extreme. (FIXME: vary the
* extreme, and the piece.)
*/
board[w+1] = MAINANCHOR;
board[w+2] = DIST(1);
board[w*2+1] = DIST(w-1);
board[w*2+2] = DIST(1);
/*
* Invent a target position. (FIXME: vary this too.)
*/
tx = w-2;
ty = h-3;
forcefield[ty*w+tx+1] = forcefield[(ty+1)*w+tx+1] = TRUE;
board[ty*w+tx+1] = board[(ty+1)*w+tx+1] = EMPTY;
/*
* Gradually remove singletons until the game becomes soluble.
*/
for (j = w; j-- > 0 ;)
for (i = h; i-- > 0 ;)
if (board[i*w+j] == ANCHOR) {
/*
* See if the board is already soluble.
*/
if ((moves = solve_board(w, h, board, forcefield,
tx, ty, movelimit, NULL)) >= 0)
goto soluble;
/*
* Otherwise, remove this piece.
*/
board[i*w+j] = EMPTY;
}
assert(!"We shouldn't get here");
soluble:
/*
* Make a list of all the inter-block edges on the board.
*/
list = snewn(wh*2, int);
nlist = 0;
for (i = 0; i+1 < w; i++)
for (j = 0; j < h; j++)
list[nlist++] = (j*w+i) * 2 + 0; /* edge to the right of j*w+i */
for (j = 0; j+1 < h; j++)
for (i = 0; i < w; i++)
list[nlist++] = (j*w+i) * 2 + 1; /* edge below j*w+i */
/*
* Now go through that list in random order, trying to merge
* the blocks on each side of each edge.
*/
shuffle(list, nlist, sizeof(*list), rs);
while (nlist > 0) {
int x1, y1, p1, c1;
int x2, y2, p2, c2;
pos = list[--nlist];
y1 = y2 = pos / (w*2);
x1 = x2 = (pos / 2) % w;
if (pos % 2)
y2++;
else
x2++;
p1 = y1*w+x1;
p2 = y2*w+x2;
/*
* Immediately abandon the attempt if we've already tried
* to merge the same pair of blocks along a different
* edge.
*/
c1 = dsf_canonify(dsf, p1);
c2 = dsf_canonify(dsf, p2);
if (tried_merge[c1 * wh + c2])
continue;
/*
* In order to be mergeable, these two squares must each
* either be, or belong to, a non-main anchor, and their
* anchors must also be distinct.
*/
if (!ISBLOCK(board[p1]) || !ISBLOCK(board[p2]))
continue;
while (ISDIST(board[p1]))
p1 -= board[p1];
while (ISDIST(board[p2]))
p2 -= board[p2];
if (board[p1] == MAINANCHOR || board[p2] == MAINANCHOR || p1 == p2)
continue;
/*
* We can merge these blocks. Try it, and see if the
* puzzle remains soluble.
*/
memcpy(board2, board, wh);
j = -1;
while (p1 < wh || p2 < wh) {
/*
* p1 and p2 are the squares at the head of each block
* list. Pick the smaller one and put it on the output
* block list.
*/
i = min(p1, p2);
if (j < 0) {
board[i] = ANCHOR;
} else {
assert(i - j <= MAXDIST);
board[i] = DIST(i - j);
}
j = i;
/*
* Now advance whichever list that came from.
*/
if (i == p1) {
do {
p1++;
} while (p1 < wh && board[p1] != DIST(p1-i));
} else {
do {
p2++;
} while (p2 < wh && board[p2] != DIST(p2-i));
}
}
j = solve_board(w, h, board, forcefield, tx, ty, movelimit, NULL);
if (j < 0) {
/*
* Didn't work. Revert the merge.
*/
memcpy(board, board2, wh);
tried_merge[c1 * wh + c2] = tried_merge[c2 * wh + c1] = TRUE;
} else {
int c;
moves = j;
dsf_merge(dsf, c1, c2);
c = dsf_canonify(dsf, c1);
for (i = 0; i < wh; i++)
tried_merge[c*wh+i] = (tried_merge[c1*wh+i] |
tried_merge[c2*wh+i]);
for (i = 0; i < wh; i++)
tried_merge[i*wh+c] = (tried_merge[i*wh+c1] |
tried_merge[i*wh+c2]);
}
}
sfree(dsf);
sfree(list);
sfree(tried_merge);
sfree(board2);
*rtx = tx;
*rty = ty;
*rboard = board;
*rforcefield = forcefield;
*minmoves = moves;
}
/* ----------------------------------------------------------------------
* End of solver/generator code.
*/
static char *new_game_desc(const game_params *params, random_state *rs,
char **aux, int interactive)
{
int w = params->w, h = params->h, wh = w*h;
int tx, ty, minmoves;
unsigned char *board, *forcefield;
char *ret, *p;
int i;
generate_board(params->w, params->h, &tx, &ty, &minmoves, rs,
&board, &forcefield, params->maxmoves);
#ifdef GENERATOR_DIAGNOSTICS
{
char *t = board_text_format(params->w, params->h, board);
printf("%s\n", t);
sfree(t);
}
#endif
/*
* Encode as a game ID.
*/
ret = snewn(wh * 6 + 40, char);
p = ret;
i = 0;
while (i < wh) {
if (ISDIST(board[i])) {
p += sprintf(p, "d%d", board[i]);
i++;
} else {
int count = 1;
int b = board[i], f = forcefield[i];
int c = (b == ANCHOR ? 'a' :
b == MAINANCHOR ? 'm' :
b == EMPTY ? 'e' :
/* b == WALL ? */ 'w');
if (f) *p++ = 'f';
*p++ = c;
i++;
while (i < wh && board[i] == b && forcefield[i] == f)
i++, count++;
if (count > 1)
p += sprintf(p, "%d", count);
}
}
p += sprintf(p, ",%d,%d,%d", tx, ty, minmoves);
ret = sresize(ret, p+1 - ret, char);
sfree(board);
sfree(forcefield);
return ret;
}
static char *validate_desc(const game_params *params, const char *desc)
{
int w = params->w, h = params->h, wh = w*h;
int *active, *link;
int mains = 0;
int i, tx, ty, minmoves;
char *ret;
active = snewn(wh, int);
link = snewn(wh, int);
i = 0;
while (*desc && *desc != ',') {
if (i >= wh) {
ret = "Too much data in game description";
goto done;
}
link[i] = -1;
active[i] = FALSE;
if (*desc == 'f' || *desc == 'F') {
desc++;
if (!*desc) {
ret = "Expected another character after 'f' in game "
"description";
goto done;
}
}
if (*desc == 'd' || *desc == 'D') {
int dist;
desc++;
if (!isdigit((unsigned char)*desc)) {
ret = "Expected a number after 'd' in game description";
goto done;
}
dist = atoi(desc);
while (*desc && isdigit((unsigned char)*desc)) desc++;
if (dist <= 0 || dist > i) {
ret = "Out-of-range number after 'd' in game description";
goto done;
}
if (!active[i - dist]) {
ret = "Invalid back-reference in game description";
goto done;
}
link[i] = i - dist;
active[i] = TRUE;
active[link[i]] = FALSE;
i++;
} else {
int c = *desc++;
int count = 1;
if (!strchr("aAmMeEwW", c)) {
ret = "Invalid character in game description";
goto done;
}
if (isdigit((unsigned char)*desc)) {
count = atoi(desc);
while (*desc && isdigit((unsigned char)*desc)) desc++;
}
if (i + count > wh) {
ret = "Too much data in game description";
goto done;
}
while (count-- > 0) {
active[i] = (strchr("aAmM", c) != NULL);
link[i] = -1;
if (strchr("mM", c) != NULL) {
mains++;
}
i++;
}
}
}
if (mains != 1) {
ret = (mains == 0 ? "No main piece specified in game description" :
"More than one main piece specified in game description");
goto done;
}
if (i < wh) {
ret = "Not enough data in game description";
goto done;
}
/*
* Now read the target coordinates.
*/
i = sscanf(desc, ",%d,%d,%d", &tx, &ty, &minmoves);
if (i < 2) {
ret = "No target coordinates specified";
goto done;
/*
* (but minmoves is optional)
*/
}
ret = NULL;
done:
sfree(active);
sfree(link);
return ret;
}
static game_state *new_game(midend *me, const game_params *params,
const char *desc)
{
int w = params->w, h = params->h, wh = w*h;
game_state *state;
int i;
state = snew(game_state);
state->w = w;
state->h = h;
state->board = snewn(wh, unsigned char);
state->lastmoved = state->lastmoved_pos = -1;
state->movecount = 0;
state->imm = snew(struct game_immutable_state);
state->imm->refcount = 1;
state->imm->forcefield = snewn(wh, unsigned char);
i = 0;
while (*desc && *desc != ',') {
int f = FALSE;
assert(i < wh);
if (*desc == 'f') {
f = TRUE;
desc++;
assert(*desc);
}
if (*desc == 'd' || *desc == 'D') {
int dist;
desc++;
dist = atoi(desc);
while (*desc && isdigit((unsigned char)*desc)) desc++;
state->board[i] = DIST(dist);
state->imm->forcefield[i] = f;
i++;
} else {
int c = *desc++;
int count = 1;
if (isdigit((unsigned char)*desc)) {
count = atoi(desc);
while (*desc && isdigit((unsigned char)*desc)) desc++;
}
assert(i + count <= wh);
c = (c == 'a' || c == 'A' ? ANCHOR :
c == 'm' || c == 'M' ? MAINANCHOR :
c == 'e' || c == 'E' ? EMPTY :
/* c == 'w' || c == 'W' ? */ WALL);
while (count-- > 0) {
state->board[i] = c;
state->imm->forcefield[i] = f;
i++;
}
}
}
/*
* Now read the target coordinates.
*/
state->tx = state->ty = 0;
state->minmoves = -1;
i = sscanf(desc, ",%d,%d,%d", &state->tx, &state->ty, &state->minmoves);
if (state->board[state->ty*w+state->tx] == MAINANCHOR)
state->completed = 0; /* already complete! */
else
state->completed = -1;
state->cheated = FALSE;
state->soln = NULL;
state->soln_index = -1;
return state;
}
static game_state *dup_game(const game_state *state)
{
int w = state->w, h = state->h, wh = w*h;
game_state *ret = snew(game_state);
ret->w = state->w;
ret->h = state->h;
ret->board = snewn(wh, unsigned char);
memcpy(ret->board, state->board, wh);
ret->tx = state->tx;
ret->ty = state->ty;
ret->minmoves = state->minmoves;
ret->lastmoved = state->lastmoved;
ret->lastmoved_pos = state->lastmoved_pos;
ret->movecount = state->movecount;
ret->completed = state->completed;
ret->cheated = state->cheated;
ret->imm = state->imm;
ret->imm->refcount++;
ret->soln = state->soln;
ret->soln_index = state->soln_index;
if (ret->soln)
ret->soln->refcount++;
return ret;
}
static void free_game(game_state *state)
{
if (--state->imm->refcount <= 0) {
sfree(state->imm->forcefield);
sfree(state->imm);
}
if (state->soln && --state->soln->refcount <= 0) {
sfree(state->soln->moves);
sfree(state->soln);
}
sfree(state->board);
sfree(state);
}
static char *solve_game(const game_state *state, const game_state *currstate,
const char *aux, char **error)
{
int *moves;
int nmoves;
int i;
char *ret, *p, sep;
/*
* Run the solver and attempt to find the shortest solution
* from the current position.
*/
nmoves = solve_board(state->w, state->h, state->board,
state->imm->forcefield, state->tx, state->ty,
-1, &moves);
if (nmoves < 0) {
*error = "Unable to find a solution to this puzzle";
return NULL;
}
if (nmoves == 0) {
*error = "Puzzle is already solved";
return NULL;
}
/*
* Encode the resulting solution as a move string.
*/
ret = snewn(nmoves * 40, char);
p = ret;
sep = 'S';
for (i = 0; i < nmoves; i++) {
p += sprintf(p, "%c%d-%d", sep, moves[i*2], moves[i*2+1]);
sep = ',';
}
sfree(moves);
assert(p - ret < nmoves * 40);
ret = sresize(ret, p+1 - ret, char);
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 board_text_format(state->w, state->h, state->board,
state->imm->forcefield);
}
struct game_ui {
int dragging;
int drag_anchor;
int drag_offset_x, drag_offset_y;
int drag_currpos;
unsigned char *reachable;
int *bfs_queue; /* used as scratch in interpret_move */
};
static game_ui *new_ui(const game_state *state)
{
int w = state->w, h = state->h, wh = w*h;
game_ui *ui = snew(game_ui);
ui->dragging = FALSE;
ui->drag_anchor = ui->drag_currpos = -1;
ui->drag_offset_x = ui->drag_offset_y = -1;
ui->reachable = snewn(wh, unsigned char);
memset(ui->reachable, 0, wh);
ui->bfs_queue = snewn(wh, int);
return ui;
}
static void free_ui(game_ui *ui)
{
sfree(ui->bfs_queue);
sfree(ui->reachable);
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)
{
}
#define PREFERRED_TILESIZE 32
#define TILESIZE (ds->tilesize)
#define BORDER (TILESIZE/2)
#define COORD(x) ( (x) * TILESIZE + BORDER )
#define FROMCOORD(x) ( ((x) - BORDER + TILESIZE) / TILESIZE - 1 )
#define BORDER_WIDTH (1 + TILESIZE/20)
#define HIGHLIGHT_WIDTH (1 + TILESIZE/16)
#define FLASH_INTERVAL 0.10F
#define FLASH_TIME 3*FLASH_INTERVAL
struct game_drawstate {
int tilesize;
int w, h;
unsigned long *grid; /* what's currently displayed */
int started;
};
static char *interpret_move(const game_state *state, game_ui *ui,
const game_drawstate *ds,
int x, int y, int button)
{
int w = state->w, h = state->h, wh = w*h;
int tx, ty, i, j;
int qhead, qtail;
if (button == LEFT_BUTTON) {
tx = FROMCOORD(x);
ty = FROMCOORD(y);
if (tx < 0 || tx >= w || ty < 0 || ty >= h ||
!ISBLOCK(state->board[ty*w+tx]))
return NULL; /* this click has no effect */
/*
* User has clicked on a block. Find the block's anchor
* and register that we've started dragging it.
*/
i = ty*w+tx;
while (ISDIST(state->board[i]))
i -= state->board[i];
assert(i >= 0 && i < wh);
ui->dragging = TRUE;
ui->drag_anchor = i;
ui->drag_offset_x = tx - (i % w);
ui->drag_offset_y = ty - (i / w);
ui->drag_currpos = i;
/*
* Now we immediately bfs out from the current location of
* the anchor, to find all the places to which this block
* can be dragged.
*/
memset(ui->reachable, FALSE, wh);
qhead = qtail = 0;
ui->reachable[i] = TRUE;
ui->bfs_queue[qtail++] = i;
for (j = i; j < wh; j++)
if (state->board[j] == DIST(j - i))
i = j;
while (qhead < qtail) {
int pos = ui->bfs_queue[qhead++];
int x = pos % w, y = pos / w;
int dir;
for (dir = 0; dir < 4; dir++) {
int dx = (dir == 0 ? -1 : dir == 1 ? +1 : 0);
int dy = (dir == 2 ? -1 : dir == 3 ? +1 : 0);
int newpos;
if (x + dx < 0 || x + dx >= w ||
y + dy < 0 || y + dy >= h)
continue;
newpos = pos + dy*w + dx;
if (ui->reachable[newpos])
continue; /* already done this one */
/*
* Now search the grid to see if the block we're
* dragging could fit into this space.
*/
for (j = i; j >= 0; j = (ISDIST(state->board[j]) ?
j - state->board[j] : -1)) {
int jx = (j+pos-ui->drag_anchor) % w;
int jy = (j+pos-ui->drag_anchor) / w;
int j2;
if (jx + dx < 0 || jx + dx >= w ||
jy + dy < 0 || jy + dy >= h)
break; /* this position isn't valid at all */
j2 = (j+pos-ui->drag_anchor) + dy*w + dx;
if (state->board[j2] == EMPTY &&
(!state->imm->forcefield[j2] ||
state->board[ui->drag_anchor] == MAINANCHOR))
continue;
while (ISDIST(state->board[j2]))
j2 -= state->board[j2];
assert(j2 >= 0 && j2 < wh);
if (j2 == ui->drag_anchor)
continue;
else
break;
}
if (j < 0) {
/*
* If we got to the end of that loop without
* disqualifying this position, mark it as
* reachable for this drag.
*/
ui->reachable[newpos] = TRUE;
ui->bfs_queue[qtail++] = newpos;
}
}
}
/*
* And that's it. Update the display to reflect the start
* of a drag.
*/
return "";
} else if (button == LEFT_DRAG && ui->dragging) {
int dist, distlimit, dx, dy, s, px, py;
tx = FROMCOORD(x);
ty = FROMCOORD(y);
tx -= ui->drag_offset_x;
ty -= ui->drag_offset_y;
/*
* Now search outwards from (tx,ty), in order of Manhattan
* distance, until we find a reachable square.
*/
distlimit = w+tx;
distlimit = max(distlimit, h+ty);
distlimit = max(distlimit, tx);
distlimit = max(distlimit, ty);
for (dist = 0; dist <= distlimit; dist++) {
for (dx = -dist; dx <= dist; dx++)
for (s = -1; s <= +1; s += 2) {
dy = s * (dist - abs(dx));
px = tx + dx;
py = ty + dy;
if (px >= 0 && px < w && py >= 0 && py < h &&
ui->reachable[py*w+px]) {
ui->drag_currpos = py*w+px;
return "";
}
}
}
return NULL; /* give up - this drag has no effect */
} else if (button == LEFT_RELEASE && ui->dragging) {
char data[256], *str;
/*
* Terminate the drag, and if the piece has actually moved
* then return a move string quoting the old and new
* locations of the piece's anchor.
*/
if (ui->drag_anchor != ui->drag_currpos) {
sprintf(data, "M%d-%d", ui->drag_anchor, ui->drag_currpos);
str = dupstr(data);
} else
str = ""; /* null move; just update the UI */
ui->dragging = FALSE;
ui->drag_anchor = ui->drag_currpos = -1;
ui->drag_offset_x = ui->drag_offset_y = -1;
memset(ui->reachable, 0, wh);
return str;
} else if (button == ' ' && state->soln) {
/*
* Make the next move in the stored solution.
*/
char data[256];
int a1, a2;
a1 = state->soln->moves[state->soln_index*2];
a2 = state->soln->moves[state->soln_index*2+1];
if (a1 == state->lastmoved_pos)
a1 = state->lastmoved;
sprintf(data, "M%d-%d", a1, a2);
return dupstr(data);
}
return NULL;
}
static int move_piece(int w, int h, const unsigned char *src,
unsigned char *dst, unsigned char *ff, int from, int to)
{
int wh = w*h;
int i, j;
if (!ISANCHOR(dst[from]))
return FALSE;
/*
* Scan to the far end of the piece's linked list.
*/
for (i = j = from; j < wh; j++)
if (src[j] == DIST(j - i))
i = j;
/*
* Remove the piece from its old location in the new
* game state.
*/
for (j = i; j >= 0; j = (ISDIST(src[j]) ? j - src[j] : -1))
dst[j] = EMPTY;
/*
* And put it back in at the new location.
*/
for (j = i; j >= 0; j = (ISDIST(src[j]) ? j - src[j] : -1)) {
int jn = j + to - from;
if (jn < 0 || jn >= wh)
return FALSE;
if (dst[jn] == EMPTY && (!ff[jn] || src[from] == MAINANCHOR)) {
dst[jn] = src[j];
} else {
return FALSE;
}
}
return TRUE;
}
static game_state *execute_move(const game_state *state, const char *move)
{
int w = state->w, h = state->h /* , wh = w*h */;
char c;
int a1, a2, n, movesize;
game_state *ret = dup_game(state);
while (*move) {
c = *move;
if (c == 'S') {
/*
* This is a solve move, so we just set up a stored
* solution path.
*/
if (ret->soln && --ret->soln->refcount <= 0) {
sfree(ret->soln->moves);
sfree(ret->soln);
}
ret->soln = snew(struct game_solution);
ret->soln->nmoves = 0;
ret->soln->moves = NULL;
ret->soln->refcount = 1;
ret->soln_index = 0;
ret->cheated = TRUE;
movesize = 0;
move++;
while (1) {
if (sscanf(move, "%d-%d%n", &a1, &a2, &n) != 2) {
free_game(ret);
return NULL;
}
/*
* Special case: if the first move in the solution
* involves the piece for which we already have a
* partial stored move, adjust the source point to
* the original starting point of that piece.
*/
if (ret->soln->nmoves == 0 && a1 == ret->lastmoved)
a1 = ret->lastmoved_pos;
if (ret->soln->nmoves >= movesize) {
movesize = (ret->soln->nmoves + 48) * 4 / 3;
ret->soln->moves = sresize(ret->soln->moves,
2*movesize, int);
}
ret->soln->moves[2*ret->soln->nmoves] = a1;
ret->soln->moves[2*ret->soln->nmoves+1] = a2;
ret->soln->nmoves++;
move += n;
if (*move != ',')
break;
move++; /* eat comma */
}
} else if (c == 'M') {
move++;
if (sscanf(move, "%d-%d%n", &a1, &a2, &n) != 2 ||
!move_piece(w, h, state->board, ret->board,
state->imm->forcefield, a1, a2)) {
free_game(ret);
return NULL;
}
if (a1 == ret->lastmoved) {
/*
* If the player has moved the same piece as they
* moved last time, don't increment the move
* count. In fact, if they've put the piece back
* where it started from, _decrement_ the move
* count.
*/
if (a2 == ret->lastmoved_pos) {
ret->movecount--; /* reverted last move */
ret->lastmoved = ret->lastmoved_pos = -1;
} else {
ret->lastmoved = a2;
/* don't change lastmoved_pos */
}
} else {
ret->lastmoved = a2;
ret->lastmoved_pos = a1;
ret->movecount++;
}
/*
* If we have a stored solution path, see if we've
* strayed from it or successfully made the next move
* along it.
*/
if (ret->soln && ret->lastmoved_pos >= 0) {
if (ret->lastmoved_pos !=
ret->soln->moves[ret->soln_index*2]) {
/* strayed from the path */
ret->soln->refcount--;
assert(ret->soln->refcount > 0);
/* `state' at least still exists */
ret->soln = NULL;
ret->soln_index = -1;
} else if (ret->lastmoved ==
ret->soln->moves[ret->soln_index*2+1]) {
/* advanced along the path */
ret->soln_index++;
if (ret->soln_index >= ret->soln->nmoves) {
/* finished the path! */
ret->soln->refcount--;
assert(ret->soln->refcount > 0);
/* `state' at least still exists */
ret->soln = NULL;
ret->soln_index = -1;
}
}
}
if (ret->board[a2] == MAINANCHOR &&
a2 == ret->ty * w + ret->tx && ret->completed < 0)
ret->completed = ret->movecount;
move += n;
} else {
free_game(ret);
return NULL;
}
if (*move == ';')
move++;
else if (*move) {
free_game(ret);
return NULL;
}
}
return ret;
}
/* ----------------------------------------------------------------------
* Drawing routines.
*/
static void game_compute_size(const game_params *params, int tilesize,
int *x, int *y)
{
/* fool the macros */
struct dummy { int tilesize; } dummy, *ds = &dummy;
dummy.tilesize = tilesize;
*x = params->w * TILESIZE + 2*BORDER;
*y = params->h * TILESIZE + 2*BORDER;
}
static void game_set_size(drawing *dr, game_drawstate *ds,
const game_params *params, int tilesize)
{
ds->tilesize = tilesize;
}
static void raise_colour(float *target, float *src, float *limit)
{
int i;
for (i = 0; i < 3; i++)
target[i] = (2*src[i] + limit[i]) / 3;
}
static float *game_colours(frontend *fe, int *ncolours)
{
float *ret = snewn(3 * NCOLOURS, float);
game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
/*
* When dragging a tile, we light it up a bit.
*/
raise_colour(ret+3*COL_DRAGGING,
ret+3*COL_BACKGROUND, ret+3*COL_HIGHLIGHT);
raise_colour(ret+3*COL_DRAGGING_HIGHLIGHT,
ret+3*COL_HIGHLIGHT, ret+3*COL_HIGHLIGHT);
raise_colour(ret+3*COL_DRAGGING_LOWLIGHT,
ret+3*COL_LOWLIGHT, ret+3*COL_HIGHLIGHT);
/*
* The main tile is tinted blue.
*/
ret[COL_MAIN * 3 + 0] = ret[COL_BACKGROUND * 3 + 0];
ret[COL_MAIN * 3 + 1] = ret[COL_BACKGROUND * 3 + 1];
ret[COL_MAIN * 3 + 2] = ret[COL_HIGHLIGHT * 3 + 2];
game_mkhighlight_specific(fe, ret, COL_MAIN,
COL_MAIN_HIGHLIGHT, COL_MAIN_LOWLIGHT);
/*
* And we light that up a bit too when dragging.
*/
raise_colour(ret+3*COL_MAIN_DRAGGING,
ret+3*COL_MAIN, ret+3*COL_MAIN_HIGHLIGHT);
raise_colour(ret+3*COL_MAIN_DRAGGING_HIGHLIGHT,
ret+3*COL_MAIN_HIGHLIGHT, ret+3*COL_MAIN_HIGHLIGHT);
raise_colour(ret+3*COL_MAIN_DRAGGING_LOWLIGHT,
ret+3*COL_MAIN_LOWLIGHT, ret+3*COL_MAIN_HIGHLIGHT);
/*
* The target area on the floor is tinted green.
*/
ret[COL_TARGET * 3 + 0] = ret[COL_BACKGROUND * 3 + 0];
ret[COL_TARGET * 3 + 1] = ret[COL_HIGHLIGHT * 3 + 1];
ret[COL_TARGET * 3 + 2] = ret[COL_BACKGROUND * 3 + 2];
game_mkhighlight_specific(fe, ret, COL_TARGET,
COL_TARGET_HIGHLIGHT, COL_TARGET_LOWLIGHT);
*ncolours = NCOLOURS;
return ret;
}
static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
{
int w = state->w, h = state->h, wh = w*h;
struct game_drawstate *ds = snew(struct game_drawstate);
int i;
ds->tilesize = 0;
ds->w = w;
ds->h = h;
ds->started = FALSE;
ds->grid = snewn(wh, unsigned long);
for (i = 0; i < wh; i++)
ds->grid[i] = ~(unsigned long)0;
return ds;
}
static void game_free_drawstate(drawing *dr, game_drawstate *ds)
{
sfree(ds->grid);
sfree(ds);
}
#define BG_NORMAL 0x00000001UL
#define BG_TARGET 0x00000002UL
#define BG_FORCEFIELD 0x00000004UL
#define FLASH_LOW 0x00000008UL
#define FLASH_HIGH 0x00000010UL
#define FG_WALL 0x00000020UL
#define FG_MAIN 0x00000040UL
#define FG_NORMAL 0x00000080UL
#define FG_DRAGGING 0x00000100UL
#define FG_SHADOW 0x00000200UL
#define FG_SOLVEPIECE 0x00000400UL
#define FG_MAINPIECESH 11
#define FG_SHADOWSH 19
#define PIECE_LBORDER 0x00000001UL
#define PIECE_TBORDER 0x00000002UL
#define PIECE_RBORDER 0x00000004UL
#define PIECE_BBORDER 0x00000008UL
#define PIECE_TLCORNER 0x00000010UL
#define PIECE_TRCORNER 0x00000020UL
#define PIECE_BLCORNER 0x00000040UL
#define PIECE_BRCORNER 0x00000080UL
#define PIECE_MASK 0x000000FFUL
/*
* Utility function.
*/
#define TYPE_MASK 0xF000
#define COL_MASK 0x0FFF
#define TYPE_RECT 0x0000
#define TYPE_TLCIRC 0x4000
#define TYPE_TRCIRC 0x5000
#define TYPE_BLCIRC 0x6000
#define TYPE_BRCIRC 0x7000
static void maybe_rect(drawing *dr, int x, int y, int w, int h,
int coltype, int col2)
{
int colour = coltype & COL_MASK, type = coltype & TYPE_MASK;
if (colour > NCOLOURS)
return;
if (type == TYPE_RECT) {
draw_rect(dr, x, y, w, h, colour);
} else {
int cx, cy, r;
clip(dr, x, y, w, h);
cx = x;
cy = y;
r = w-1;
if (type & 0x1000)
cx += r;
if (type & 0x2000)
cy += r;
if (col2 == -1 || col2 == coltype) {
assert(w == h);
draw_circle(dr, cx, cy, r, colour, colour);
} else {
/*
* We aim to draw a quadrant of a circle in two
* different colours. We do this using Bresenham's
* algorithm directly, because the Puzzles drawing API
* doesn't have a draw-sector primitive.
*/
int bx, by, bd, bd2;
int xm = (type & 0x1000 ? -1 : +1);
int ym = (type & 0x2000 ? -1 : +1);
by = r;
bx = 0;
bd = 0;
while (by >= bx) {
/*
* Plot the point.
*/
{
int x1 = cx+xm*bx, y1 = cy+ym*bx;
int x2, y2;
x2 = cx+xm*by; y2 = y1;
draw_rect(dr, min(x1,x2), min(y1,y2),
abs(x1-x2)+1, abs(y1-y2)+1, colour);
x2 = x1; y2 = cy+ym*by;
draw_rect(dr, min(x1,x2), min(y1,y2),
abs(x1-x2)+1, abs(y1-y2)+1, col2);
}
bd += 2*bx + 1;
bd2 = bd - (2*by - 1);
if (abs(bd2) < abs(bd)) {
bd = bd2;
by--;
}
bx++;
}
}
unclip(dr);
}
}
static void draw_wallpart(drawing *dr, game_drawstate *ds,
int tx, int ty, unsigned long val,
int cl, int cc, int ch)
{
int coords[6];
draw_rect(dr, tx, ty, TILESIZE, TILESIZE, cc);
if (val & PIECE_LBORDER)
draw_rect(dr, tx, ty, HIGHLIGHT_WIDTH, TILESIZE,
ch);
if (val & PIECE_RBORDER)
draw_rect(dr, tx+TILESIZE-HIGHLIGHT_WIDTH, ty,
HIGHLIGHT_WIDTH, TILESIZE, cl);
if (val & PIECE_TBORDER)
draw_rect(dr, tx, ty, TILESIZE, HIGHLIGHT_WIDTH, ch);
if (val & PIECE_BBORDER)
draw_rect(dr, tx, ty+TILESIZE-HIGHLIGHT_WIDTH,
TILESIZE, HIGHLIGHT_WIDTH, cl);
if (!((PIECE_BBORDER | PIECE_LBORDER) &~ val)) {
draw_rect(dr, tx, ty+TILESIZE-HIGHLIGHT_WIDTH,
HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH, cl);
clip(dr, tx, ty+TILESIZE-HIGHLIGHT_WIDTH,
HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH);
coords[0] = tx - 1;
coords[1] = ty + TILESIZE - HIGHLIGHT_WIDTH - 1;
coords[2] = tx + HIGHLIGHT_WIDTH;
coords[3] = ty + TILESIZE - HIGHLIGHT_WIDTH - 1;
coords[4] = tx - 1;
coords[5] = ty + TILESIZE;
draw_polygon(dr, coords, 3, ch, ch);
unclip(dr);
} else if (val & PIECE_BLCORNER) {
draw_rect(dr, tx, ty+TILESIZE-HIGHLIGHT_WIDTH,
HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH, ch);
clip(dr, tx, ty+TILESIZE-HIGHLIGHT_WIDTH,
HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH);
coords[0] = tx - 1;
coords[1] = ty + TILESIZE - HIGHLIGHT_WIDTH - 1;
coords[2] = tx + HIGHLIGHT_WIDTH;
coords[3] = ty + TILESIZE - HIGHLIGHT_WIDTH - 1;
coords[4] = tx - 1;
coords[5] = ty + TILESIZE;
draw_polygon(dr, coords, 3, cl, cl);
unclip(dr);
}
if (!((PIECE_TBORDER | PIECE_RBORDER) &~ val)) {
draw_rect(dr, tx+TILESIZE-HIGHLIGHT_WIDTH, ty,
HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH, cl);
clip(dr, tx+TILESIZE-HIGHLIGHT_WIDTH, ty,
HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH);
coords[0] = tx + TILESIZE - HIGHLIGHT_WIDTH - 1;
coords[1] = ty - 1;
coords[2] = tx + TILESIZE;
coords[3] = ty - 1;
coords[4] = tx + TILESIZE - HIGHLIGHT_WIDTH - 1;
coords[5] = ty + HIGHLIGHT_WIDTH;
draw_polygon(dr, coords, 3, ch, ch);
unclip(dr);
} else if (val & PIECE_TRCORNER) {
draw_rect(dr, tx+TILESIZE-HIGHLIGHT_WIDTH, ty,
HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH, ch);
clip(dr, tx+TILESIZE-HIGHLIGHT_WIDTH, ty,
HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH);
coords[0] = tx + TILESIZE - HIGHLIGHT_WIDTH - 1;
coords[1] = ty - 1;
coords[2] = tx + TILESIZE;
coords[3] = ty - 1;
coords[4] = tx + TILESIZE - HIGHLIGHT_WIDTH - 1;
coords[5] = ty + HIGHLIGHT_WIDTH;
draw_polygon(dr, coords, 3, cl, cl);
unclip(dr);
}
if (val & PIECE_TLCORNER)
draw_rect(dr, tx, ty, HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH, ch);
if (val & PIECE_BRCORNER)
draw_rect(dr, tx+TILESIZE-HIGHLIGHT_WIDTH,
ty+TILESIZE-HIGHLIGHT_WIDTH,
HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH, cl);
}
static void draw_piecepart(drawing *dr, game_drawstate *ds,
int tx, int ty, unsigned long val,
int cl, int cc, int ch)
{
int x[6], y[6];
/*
* Drawing the blocks is hellishly fiddly. The blocks don't
* stretch to the full size of the tile; there's a border
* around them of size BORDER_WIDTH. Then they have bevelled
* borders of size HIGHLIGHT_WIDTH, and also rounded corners.
*
* I tried for some time to find a clean and clever way to
* figure out what needed drawing from the corner and border
* flags, but in the end the cleanest way I could find was the
* following. We divide the grid square into 25 parts by
* ruling four horizontal and four vertical lines across it;
* those lines are at BORDER_WIDTH and BORDER_WIDTH +
* HIGHLIGHT_WIDTH from the top, from the bottom, from the
* left and from the right. Then we carefully consider each of
* the resulting 25 sections of square, and decide separately
* what needs to go in it based on the flags. In complicated
* cases there can be up to five possibilities affecting any
* given section (no corner or border flags, just the corner
* flag, one border flag, the other border flag, both border
* flags). So there's a lot of very fiddly logic here and all
* I could really think to do was give it my best shot and
* then test it and correct all the typos. Not fun to write,
* and I'm sure it isn't fun to read either, but it seems to
* work.
*/
x[0] = tx;
x[1] = x[0] + BORDER_WIDTH;
x[2] = x[1] + HIGHLIGHT_WIDTH;
x[5] = tx + TILESIZE;
x[4] = x[5] - BORDER_WIDTH;
x[3] = x[4] - HIGHLIGHT_WIDTH;
y[0] = ty;
y[1] = y[0] + BORDER_WIDTH;
y[2] = y[1] + HIGHLIGHT_WIDTH;
y[5] = ty + TILESIZE;
y[4] = y[5] - BORDER_WIDTH;
y[3] = y[4] - HIGHLIGHT_WIDTH;
#define RECT(p,q) x[p], y[q], x[(p)+1]-x[p], y[(q)+1]-y[q]
maybe_rect(dr, RECT(0,0),
(val & (PIECE_TLCORNER | PIECE_TBORDER |
PIECE_LBORDER)) ? -1 : cc, -1);
maybe_rect(dr, RECT(1,0),
(val & PIECE_TLCORNER) ? ch : (val & PIECE_TBORDER) ? -1 :
(val & PIECE_LBORDER) ? ch : cc, -1);
maybe_rect(dr, RECT(2,0),
(val & PIECE_TBORDER) ? -1 : cc, -1);
maybe_rect(dr, RECT(3,0),
(val & PIECE_TRCORNER) ? cl : (val & PIECE_TBORDER) ? -1 :
(val & PIECE_RBORDER) ? cl : cc, -1);
maybe_rect(dr, RECT(4,0),
(val & (PIECE_TRCORNER | PIECE_TBORDER |
PIECE_RBORDER)) ? -1 : cc, -1);
maybe_rect(dr, RECT(0,1),
(val & PIECE_TLCORNER) ? ch : (val & PIECE_LBORDER) ? -1 :
(val & PIECE_TBORDER) ? ch : cc, -1);
maybe_rect(dr, RECT(1,1),
(val & PIECE_TLCORNER) ? cc : -1, -1);
maybe_rect(dr, RECT(1,1),
(val & PIECE_TLCORNER) ? ch | TYPE_TLCIRC :
!((PIECE_TBORDER | PIECE_LBORDER) &~ val) ? ch | TYPE_BRCIRC :
(val & (PIECE_TBORDER | PIECE_LBORDER)) ? ch : cc, -1);
maybe_rect(dr, RECT(2,1),
(val & PIECE_TBORDER) ? ch : cc, -1);
maybe_rect(dr, RECT(3,1),
(val & PIECE_TRCORNER) ? cc : -1, -1);
maybe_rect(dr, RECT(3,1),
(val & (PIECE_TBORDER | PIECE_RBORDER)) == PIECE_TBORDER ? ch :
(val & (PIECE_TBORDER | PIECE_RBORDER)) == PIECE_RBORDER ? cl :
!((PIECE_TBORDER|PIECE_RBORDER) &~ val) ? cl | TYPE_BLCIRC :
(val & PIECE_TRCORNER) ? cl | TYPE_TRCIRC :
cc, ch);
maybe_rect(dr, RECT(4,1),
(val & PIECE_TRCORNER) ? ch : (val & PIECE_RBORDER) ? -1 :
(val & PIECE_TBORDER) ? ch : cc, -1);
maybe_rect(dr, RECT(0,2),
(val & PIECE_LBORDER) ? -1 : cc, -1);
maybe_rect(dr, RECT(1,2),
(val & PIECE_LBORDER) ? ch : cc, -1);
maybe_rect(dr, RECT(2,2),
cc, -1);
maybe_rect(dr, RECT(3,2),
(val & PIECE_RBORDER) ? cl : cc, -1);
maybe_rect(dr, RECT(4,2),
(val & PIECE_RBORDER) ? -1 : cc, -1);
maybe_rect(dr, RECT(0,3),
(val & PIECE_BLCORNER) ? cl : (val & PIECE_LBORDER) ? -1 :
(val & PIECE_BBORDER) ? cl : cc, -1);
maybe_rect(dr, RECT(1,3),
(val & PIECE_BLCORNER) ? cc : -1, -1);
maybe_rect(dr, RECT(1,3),
(val & (PIECE_BBORDER | PIECE_LBORDER)) == PIECE_BBORDER ? cl :
(val & (PIECE_BBORDER | PIECE_LBORDER)) == PIECE_LBORDER ? ch :
!((PIECE_BBORDER|PIECE_LBORDER) &~ val) ? ch | TYPE_TRCIRC :
(val & PIECE_BLCORNER) ? ch | TYPE_BLCIRC :
cc, cl);
maybe_rect(dr, RECT(2,3),
(val & PIECE_BBORDER) ? cl : cc, -1);
maybe_rect(dr, RECT(3,3),
(val & PIECE_BRCORNER) ? cc : -1, -1);
maybe_rect(dr, RECT(3,3),
(val & PIECE_BRCORNER) ? cl | TYPE_BRCIRC :
!((PIECE_BBORDER | PIECE_RBORDER) &~ val) ? cl | TYPE_TLCIRC :
(val & (PIECE_BBORDER | PIECE_RBORDER)) ? cl : cc, -1);
maybe_rect(dr, RECT(4,3),
(val & PIECE_BRCORNER) ? cl : (val & PIECE_RBORDER) ? -1 :
(val & PIECE_BBORDER) ? cl : cc, -1);
maybe_rect(dr, RECT(0,4),
(val & (PIECE_BLCORNER | PIECE_BBORDER |
PIECE_LBORDER)) ? -1 : cc, -1);
maybe_rect(dr, RECT(1,4),
(val & PIECE_BLCORNER) ? ch : (val & PIECE_BBORDER) ? -1 :
(val & PIECE_LBORDER) ? ch : cc, -1);
maybe_rect(dr, RECT(2,4),
(val & PIECE_BBORDER) ? -1 : cc, -1);
maybe_rect(dr, RECT(3,4),
(val & PIECE_BRCORNER) ? cl : (val & PIECE_BBORDER) ? -1 :
(val & PIECE_RBORDER) ? cl : cc, -1);
maybe_rect(dr, RECT(4,4),
(val & (PIECE_BRCORNER | PIECE_BBORDER |
PIECE_RBORDER)) ? -1 : cc, -1);
#undef RECT
}
static void draw_tile(drawing *dr, game_drawstate *ds,
int x, int y, unsigned long val)
{
int tx = COORD(x), ty = COORD(y);
int cc, ch, cl;
/*
* Draw the tile background.
*/
if (val & BG_TARGET)
cc = COL_TARGET;
else
cc = COL_BACKGROUND;
ch = cc+1;
cl = cc+2;
if (val & FLASH_LOW)
cc = cl;
else if (val & FLASH_HIGH)
cc = ch;
draw_rect(dr, tx, ty, TILESIZE, TILESIZE, cc);
if (val & BG_FORCEFIELD) {
/*
* Cattle-grid effect to indicate that nothing but the
* main block can slide over this square.
*/
int n = 3 * (TILESIZE / (3*HIGHLIGHT_WIDTH));
int i;
for (i = 1; i < n; i += 3) {
draw_rect(dr, tx,ty+(TILESIZE*i/n), TILESIZE,HIGHLIGHT_WIDTH, cl);
draw_rect(dr, tx+(TILESIZE*i/n),ty, HIGHLIGHT_WIDTH,TILESIZE, cl);
}
}
/*
* Draw the tile midground: a shadow of a block, for
* displaying partial solutions.
*/
if (val & FG_SHADOW) {
draw_piecepart(dr, ds, tx, ty, (val >> FG_SHADOWSH) & PIECE_MASK,
cl, cl, cl);
}
/*
* Draw the tile foreground, i.e. some section of a block or
* wall.
*/
if (val & FG_WALL) {
cc = COL_BACKGROUND;
ch = cc+1;
cl = cc+2;
if (val & FLASH_LOW)
cc = cl;
else if (val & FLASH_HIGH)
cc = ch;
draw_wallpart(dr, ds, tx, ty, (val >> FG_MAINPIECESH) & PIECE_MASK,
cl, cc, ch);
} else if (val & (FG_MAIN | FG_NORMAL)) {
if (val & FG_DRAGGING)
cc = (val & FG_MAIN ? COL_MAIN_DRAGGING : COL_DRAGGING);
else
cc = (val & FG_MAIN ? COL_MAIN : COL_BACKGROUND);
ch = cc+1;
cl = cc+2;
if (val & FLASH_LOW)
cc = cl;
else if (val & (FLASH_HIGH | FG_SOLVEPIECE))
cc = ch;
draw_piecepart(dr, ds, tx, ty, (val >> FG_MAINPIECESH) & PIECE_MASK,
cl, cc, ch);
}
draw_update(dr, tx, ty, TILESIZE, TILESIZE);
}
static unsigned long find_piecepart(int w, int h, int *dsf, int x, int y)
{
int i = y*w+x;
int canon = dsf_canonify(dsf, i);
unsigned long val = 0;
if (x == 0 || canon != dsf_canonify(dsf, i-1))
val |= PIECE_LBORDER;
if (y== 0 || canon != dsf_canonify(dsf, i-w))
val |= PIECE_TBORDER;
if (x == w-1 || canon != dsf_canonify(dsf, i+1))
val |= PIECE_RBORDER;
if (y == h-1 || canon != dsf_canonify(dsf, i+w))
val |= PIECE_BBORDER;
if (!(val & (PIECE_TBORDER | PIECE_LBORDER)) &&
canon != dsf_canonify(dsf, i-1-w))
val |= PIECE_TLCORNER;
if (!(val & (PIECE_TBORDER | PIECE_RBORDER)) &&
canon != dsf_canonify(dsf, i+1-w))
val |= PIECE_TRCORNER;
if (!(val & (PIECE_BBORDER | PIECE_LBORDER)) &&
canon != dsf_canonify(dsf, i-1+w))
val |= PIECE_BLCORNER;
if (!(val & (PIECE_BBORDER | PIECE_RBORDER)) &&
canon != dsf_canonify(dsf, i+1+w))
val |= PIECE_BRCORNER;
return val;
}
static void game_redraw(drawing *dr, game_drawstate *ds,
const game_state *oldstate, const game_state *state,
int dir, const game_ui *ui,
float animtime, float flashtime)
{
int w = state->w, h = state->h, wh = w*h;
unsigned char *board;
int *dsf;
int x, y, mainanchor, mainpos, dragpos, solvepos, solvesrc, solvedst;
if (!ds->started) {
/*
* The initial contents of the window are not guaranteed
* and can vary with front ends. To be on the safe side,
* all games should start by drawing a big
* background-colour rectangle covering the whole window.
*/
draw_rect(dr, 0, 0, 10*ds->tilesize, 10*ds->tilesize, COL_BACKGROUND);
ds->started = TRUE;
}
/*
* Construct the board we'll be displaying (which may be
* different from the one in state if ui describes a drag in
* progress).
*/
board = snewn(wh, unsigned char);
memcpy(board, state->board, wh);
if (ui->dragging) {
int mpret = move_piece(w, h, state->board, board,
state->imm->forcefield,
ui->drag_anchor, ui->drag_currpos);
assert(mpret);
}
if (state->soln) {
solvesrc = state->soln->moves[state->soln_index*2];
solvedst = state->soln->moves[state->soln_index*2+1];
if (solvesrc == state->lastmoved_pos)
solvesrc = state->lastmoved;
if (solvesrc == ui->drag_anchor)
solvesrc = ui->drag_currpos;
} else
solvesrc = solvedst = -1;
/*
* Build a dsf out of that board, so we can conveniently tell
* which edges are connected and which aren't.
*/
dsf = snew_dsf(wh);
mainanchor = -1;
for (y = 0; y < h; y++)
for (x = 0; x < w; x++) {
int i = y*w+x;
if (ISDIST(board[i]))
dsf_merge(dsf, i, i - board[i]);
if (board[i] == MAINANCHOR)
mainanchor = i;
if (board[i] == WALL) {
if (x > 0 && board[i-1] == WALL)
dsf_merge(dsf, i, i-1);
if (y > 0 && board[i-w] == WALL)
dsf_merge(dsf, i, i-w);
}
}
assert(mainanchor >= 0);
mainpos = dsf_canonify(dsf, mainanchor);
dragpos = ui->drag_currpos > 0 ? dsf_canonify(dsf, ui->drag_currpos) : -1;
solvepos = solvesrc >= 0 ? dsf_canonify(dsf, solvesrc) : -1;
/*
* Now we can construct the data about what we want to draw.
*/
for (y = 0; y < h; y++)
for (x = 0; x < w; x++) {
int i = y*w+x;
int j;
unsigned long val;
int canon;
/*
* See if this square is part of the target area.
*/
j = i + mainanchor - (state->ty * w + state->tx);
while (j >= 0 && j < wh && ISDIST(board[j]))
j -= board[j];
if (j == mainanchor)
val = BG_TARGET;
else
val = BG_NORMAL;
if (state->imm->forcefield[i])
val |= BG_FORCEFIELD;
if (flashtime > 0) {
int flashtype = (int)(flashtime / FLASH_INTERVAL) & 1;
val |= (flashtype ? FLASH_LOW : FLASH_HIGH);
}
if (board[i] != EMPTY) {
canon = dsf_canonify(dsf, i);
if (board[i] == WALL)
val |= FG_WALL;
else if (canon == mainpos)
val |= FG_MAIN;
else
val |= FG_NORMAL;
if (canon == dragpos)
val |= FG_DRAGGING;
if (canon == solvepos)
val |= FG_SOLVEPIECE;
/*
* Now look around to see if other squares
* belonging to the same block are adjacent to us.
*/
val |= find_piecepart(w, h, dsf, x, y) << FG_MAINPIECESH;
}
/*
* If we're in the middle of showing a solution,
* display a shadow piece for the target of the
* current move.
*/
if (solvepos >= 0) {
int si = i - solvedst + solvesrc;
if (si >= 0 && si < wh && dsf_canonify(dsf, si) == solvepos) {
val |= find_piecepart(w, h, dsf,
si % w, si / w) << FG_SHADOWSH;
val |= FG_SHADOW;
}
}
if (val != ds->grid[i]) {
draw_tile(dr, ds, x, y, val);
ds->grid[i] = val;
}
}
/*
* Update the status bar.
*/
{
char statusbuf[256];
sprintf(statusbuf, "%sMoves: %d",
(state->completed >= 0 ?
(state->cheated ? "Auto-solved. " : "COMPLETED! ") :
(state->cheated ? "Auto-solver used. " : "")),
(state->completed >= 0 ? state->completed : state->movecount));
if (state->minmoves >= 0)
sprintf(statusbuf+strlen(statusbuf), " (min %d)",
state->minmoves);
status_bar(dr, statusbuf);
}
sfree(dsf);
sfree(board);
}
static float game_anim_length(const game_state *oldstate,
const game_state *newstate, int dir, game_ui *ui)
{
return 0.0F;
}
static float game_flash_length(const game_state *oldstate,
const game_state *newstate, int dir, game_ui *ui)
{
if (oldstate->completed < 0 && newstate->completed >= 0)
return FLASH_TIME;
return 0.0F;
}
static int game_status(const game_state *state)
{
return state->completed ? +1 : 0;
}
static int game_timing_state(const game_state *state, game_ui *ui)
{
return TRUE;
}
static void game_print_size(const game_params *params, float *x, float *y)
{
}
static void game_print(drawing *dr, const game_state *state, int tilesize)
{
}
#ifdef COMBINED
#define thegame slide
#endif
const struct game thegame = {
"Slide", NULL, NULL,
default_params,
game_fetch_preset, NULL,
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_TILESIZE, game_compute_size, game_set_size,
game_colours,
game_new_drawstate,
game_free_drawstate,
game_redraw,
game_anim_length,
game_flash_length,
game_status,
FALSE, FALSE, game_print_size, game_print,
TRUE, /* wants_statusbar */
FALSE, game_timing_state,
0, /* flags */
};
#ifdef STANDALONE_SOLVER
#include <stdarg.h>
int main(int argc, char **argv)
{
game_params *p;
game_state *s;
char *id = NULL, *desc, *err;
int count = FALSE;
int ret;
int *moves;
while (--argc > 0) {
char *p = *++argv;
/*
if (!strcmp(p, "-v")) {
verbose = TRUE;
} else
*/
if (!strcmp(p, "-c")) {
count = TRUE;
} else if (*p == '-') {
fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p);
return 1;
} else {
id = p;
}
}
if (!id) {
fprintf(stderr, "usage: %s [-c | -v] <game_id>\n", argv[0]);
return 1;
}
desc = strchr(id, ':');
if (!desc) {
fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]);
return 1;
}
*desc++ = '\0';
p = default_params();
decode_params(p, id);
err = validate_desc(p, desc);
if (err) {
fprintf(stderr, "%s: %s\n", argv[0], err);
return 1;
}
s = new_game(NULL, p, desc);
ret = solve_board(s->w, s->h, s->board, s->imm->forcefield,
s->tx, s->ty, -1, &moves);
if (ret < 0) {
printf("No solution found\n");
} else {
int index = 0;
if (count) {
printf("%d moves required\n", ret);
return 0;
}
while (1) {
int moveret;
char *text = board_text_format(s->w, s->h, s->board,
s->imm->forcefield);
game_state *s2;
printf("position %d:\n%s", index, text);
if (index >= ret)
break;
s2 = dup_game(s);
moveret = move_piece(s->w, s->h, s->board,
s2->board, s->imm->forcefield,
moves[index*2], moves[index*2+1]);
assert(moveret);
free_game(s);
s = s2;
index++;
}
}
return 0;
}
#endif
|