File: sudoku.c

package info (click to toggle)
sudoku 1.0.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 344 kB
  • ctags: 373
  • sloc: ansic: 3,483; makefile: 49
file content (4086 lines) | stat: -rwxr-xr-x 128,504 bytes parent folder | download
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
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
/* sudoku.c - sudoku game
 *
 * Writing a fun Su-Do-Ku game has turned out to be a difficult exercise.
 * The biggest difficulty is keeping the game fun - and this means allowing
 * the user to make mistakes. The game is not much fun if it prevents the
 * user from making moves, or if it informs them of an incorrect move.
 * With movement constraints, the 'game' is little more than an automated
 * solver (and no fun at all).
 *
 * Another challenge is generating good puzzles that are entertaining to
 * solve. It is certainly true that there is an art to creating good
 * Su-Do-Ku puzzles, and that good hand generated puzzles are more
 * entertaining than many computer generated puzzles - I just hope that
 * the algorithm implemented here provides fun puzzles. It is an area
 * that needs work. The puzzle classification is very simple, and could
 * also do with work. Finally, understanding the automatically generated
 * hints is sometimes more work than solving the puzzle - a better, and
 * more human friendly, mechanism is needed.
 *
 * Comments, suggestions, and contributions are always welcome - send email
 * to: mike 'at' laurasia.com.au. Note that this code assumes a single
 * threaded process, makes extensive use of global variables, and has
 * not been written to be reused in other applications. The code makes no
 * use of dynamic memory allocation, and hence, requires no heap. It should
 * also run with minimal stack space.
 *
 * This code and accompanying files have been placed into the public domain
 * by Michael Kennett, July 2005. It is provided without any warranty
 * whatsoever, and in no event shall Michael Kennett be liable for
 * any damages of any kind, however caused, arising from this software.
 */

#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#include <curses.h>
#include <stddef.h>

/* Default file locations */
#if !defined (TEMPLATE)
#define TEMPLATE "/usr/share/sudoku/template"
#endif
#if !defined (PRECANNED)
#define PRECANNED "/usr/share/sudoku/precanned"
#endif
#define TEMPLATE_FALLBACK "template"
#define PRECANNED_FALLBACK "precanned"
#define DEFAULT_BOARD_NAME "board"

#if defined (_MSC_VER)
#pragma warning(disable: 4996)
#endif

#define BC0 0x00
#define BC1 0x01
#define BC2 0x04
#define BC3 0x05
#define BC4 0x08
#define BC5 0x09
#define BC6 0x0B
#define BC7 0x10
#define BC8 0x11
#define BC9 0x14
#define BCa 0x15
#define BCb 0x18
#define BCc 0x19
#define BCd 0x1B
#define BCe 0x40
#define BCf 0x41
#define BCg 0x44
#define BCh 0x45
#define BCi 0x48
#define BCj 0x49
#define BCk 0x4B
#define BCl 0x50
#define BCm 0x51
#define BCn 0x54
#define BCo 0x55
#define BCp 0x58
#define BCq 0x59
#define BCr 0x5B
#define BCs 0x80
#define BCt 0x81
#define BCu 0x84
#define BCv 0x85
#define BCw 0x90
#define BCx 0x91
#define BCy 0x94
#define BCz 0x95

/* Default template */
#define HC(a) BC##a
#define TB(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w)    \
            HC(a),HC(b),HC(c),HC(d),HC(e),HC(f),HC(g),HC(h), \
            HC(i),HC(j),HC(k),HC(l),HC(m),HC(n),HC(o),HC(p), \
            HC(q),HC(r),HC(s),HC(t),HC(u),HC(v),HC(w)

#define T2(a1,b1,c1,d1,e1,f1,g1,h1,i1,j1,k1,       \
           l1,m1,n1,o1,p1,q1,r1,s1,t1,u1,v1,w1,    \
           a2,b2,c2,d2,e2,f2,g2,h2,i2,j2,k2,       \
           l2,m2,n2,o2,p2,q2,r2,s2,t2,u2,v2,w2)    \
          TB(a1,b1,c1,d1,e1,f1,g1,h1,i1,j1,k1,     \
             l1,m1,n1,o1,p1,q1,r1,s1,t1,u1,v1,w1), \
          TB(a2,b2,c2,d2,e2,f2,g2,h2,i2,j2,k2,     \
             l2,m2,n2,o2,p2,q2,r2,s2,t2,u2,v2,w2)

static const char dtct[4] = {
    '.',    /* 0 */
    '*',    /* 1 */
    '\n',   /* 2 */
    '%'     /* 3 */
};

static const unsigned char default_template [] = {
 T2(d,h,x,l,9,i,e,w,1,m,i,0,u,9,1,c,2,u,l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,l,l,4,g,s,9,9,4,f,v,2,e,5,8,s),
 T2(k,2,w,e,e,q,2,s,g,8,5,0,s,8,h,4,e,y,3,2,b,e,u, d,m,w,g,0,j,2,u,1,e,4,h,t,2,0,j,e,u,1,g,b,9,t),
 T2(k,e,y,7,9,4,0,w,h,7,b,g,w,7,g,c,0,s,l,7,p,2,u, 6,g,s,n,n,i,1,v,e,2,b,0,w,e,2,i,1,v,n,n,4,g,s),
 T2(d,m,w,7,a,4,0,s,7,f,b,1,x,2,8,4,0,s,l,8,b,9,x, d,9,s,1,m,i,e,s,1,l,5,g,s,a,0,5,2,u,9,1,5,l,w),
 TB(d,0,u,f,9,4,e,y,a,2,4,1,t,e,l,q,2,s,l,2,j,0,w)
};

static const char * program;        /* argv[0] */

static const char * requested_class = NULL;
static time_t start_time;

/* Common state encoding in a 32-bit integer:
 *   bits  0-6    index
 *         7-15   state  [bit high signals digits not possible]
 *        16-19   digit
 *           20   fixed  [set if digit initially fixed]
 *           21   choice [set if solver chose this digit]
 *           22   ignore [set if ignored by reapply()]
 *           23   unused
 *        24-26   hint
 *        27-31   unused
 */
#define INDEX_MASK              0x0000007f
#define GET_INDEX(val)          (INDEX_MASK&(val))
#define SET_INDEX(val)          (val)

#define STATE_MASK              0x0000ff80
#define STATE_SHIFT             (7-1)                        /* digits 1..9 */
#define DIGIT_STATE(digit)      (1<<(STATE_SHIFT+(digit)))

#define DIGIT_MASK              0x000f0000
#define DIGIT_SHIFT             16
#define GET_DIGIT(val)          (((val)&DIGIT_MASK)>>(DIGIT_SHIFT))
#define SET_DIGIT(val)          ((val)<<(DIGIT_SHIFT))

#define FIXED                   0x00100000
#define CHOICE                  0x00200000
#define IGNORED                 0x00400000

/* Hint codes (c.f. singles(), pairs(), findmoves()) */
#define HINT_ROW                0x01000000
#define HINT_COLUMN             0x02000000
#define HINT_BLOCK              0x04000000

/* For a general board it may be necessary to do backtracking (i.e. to
 * rewind the board to an earlier state), and make choices during the
 * solution process. This can be implemented naturally using recursion,
 * but it is more efficient to maintain a single board.
 */
static int board[81];

/* Addressing board elements: linear array 0..80 */
#define ROW(idx)                ((idx)/9)
#define COLUMN(idx)             ((idx)%9)
#define BLOCK(idx)              (3*(ROW(idx)/3)+(COLUMN(idx)/3))
#define INDEX(row,col)          (9*(row)+(col))

/* Blocks indexed 0..9 */
#define IDX_BLOCK(row,col)      (3*((row)/3)+((col)/3))
#define TOP_LEFT(block)         (INDEX(block/3,block%3))

/* Board state */
#define STATE(idx)              ((board[idx])&STATE_MASK)
#define DIGIT(idx)              (GET_DIGIT(board[idx]))
#define HINT(idx)               ((board[idx])&HINT_MASK)
#define IS_EMPTY(idx)           (0 == DIGIT(idx))
#define DISALLOWED(idx,digit)   ((board[idx])&DIGIT_STATE(digit))
#define IS_FIXED(idx)           (board[idx]&FIXED)

/* Record move history, and maintain a counter for the current
 * move number. Concessions are made for the user interface, and
 * allow digit 0 to indicate clearing a square. The move history
 * is used to support 'undo's for the user interface, and hence
 * is larger than required - there is sufficient space to solve
 * the puzzle, undo every move, and then redo the puzzle - and
 * if the user requires more space, then the full history will be
 * lost.
 */
static int idx_history;
static int history[3 * 81];

/* Possible moves for a given board (c.f. fillmoves()).
 * Also used by choice() when the deterministic solver has failed,
 * and for calculating user hints. The number of hints is stored
 * in num_hints, or -1 if no hints calculated. The number of hints
 * requested by the user since their last move is stored in req_hints;
 * if the user keeps requesting hints, start giving more information.
 * Finally, record the last hint issued to the user; attempt to give
 * different hints each time.
 */
static int idx_possible;
static int possible[81];
static int num_hints;
static int req_hints;
static int last_hint;

static int pass;    /* count # passes of deterministic solver */

/* Support for template file */
static FILE * ftmplt;
static int n_tmplt;                 /* Number of templates in file */
static int tmplt[81];             /* Template indices */
static int len_tmplt;               /* Number of template indices */

/* Command line options */
static enum opt_format_enum
{
    fStandard,
    fCompact,
    fCSV,
    fPostScript,
    fHTML
} opt_format = fStandard;

static int opt_describe = 0;
static int opt_generate = 0;
static int num_generate = 1;  /* Number boards to generate w/ -g */
static int opt_random = 1;
static int opt_statistics = 0;
static int opt_spoilerhint = 0;
static int opt_solve = 0;
static int opt_restrict = 0;

static FILE default_template_file;  /* We will use only the address of it. */
static int default_template_siz = 0;
static int default_template_pos = 0;

/* Special modified versions of template-file handling functions */
static
FILE *
topen (const char *     filename,
       const char *     mode)
{
    FILE * stream = 0;
    if (mode != 0
        && strcmp(mode, "r") == 0) {
        stream = fopen(filename, "r");
        if (0 == stream && strcmp(TEMPLATE_FALLBACK, filename) == 0) {
            stream = &default_template_file;
            default_template_siz = sizeof(default_template) * 4;
            default_template_pos = 0;
        }
    }
    return stream;
}

static
int
tclose (FILE *  stream)
{
    int ret = -1;
    if (stream == &default_template_file) {
        default_template_siz = 0;
        default_template_pos = 0;
        ret = 0;
    } else if (0 != stream) {
        ret = fclose(stream);
    }
    return ret;
}

static
int
tseek (FILE *   stream,
       long     offset,
       int      origin)
{
    int ret = -1;
    if (stream != &default_template_file) {
        ret = fseek(stream,
                    offset,
                    origin);
    } else {
        switch (origin) {
            case SEEK_CUR:
                if (default_template_siz > default_template_pos + offset
                    && default_template_pos + offset >= 0) {
                    default_template_pos = offset;
                    ret = 0;
                }
                break;
            case SEEK_END:
                if (default_template_siz - offset >= 0 && offset <= 0) {
                    default_template_pos = offset;
                    ret = 0;
                }
                break;
            case SEEK_SET:
                if (default_template_siz > offset && offset >= 0) {
                    default_template_pos = offset;
                    ret = 0;
                }
                break;
            default:
                /* Invalid origin */
                break;
        }
    }
    return ret;
}

static
char *
tgets (char *   str,
       int      n,
       FILE *    stream)
{
    char * ret = 0;
    if (stream == &default_template_file) {
        int i = 0;
        if (str != 0
            && n > 0
            && default_template_siz > default_template_pos
            && default_template_pos >= 0) {
            char ch = 0;
            while (default_template_pos < default_template_siz
                   && n > i) {
                unsigned char ci = default_template[default_template_pos >> 2];
                int j = (default_template_pos++ & 3);
                if (j > 0) {
                    ci >>= 2 * j;
                }
                ch = dtct[ci & 3];
                str[i++] = ch;
                if (ch == '\n') {
                    break;
                }
            }
            if (i < n) {
                str[i++] = 0;
                ret = str;
            } else if (i == n && 0 < n) {
                str[n - 1] = 0;
                ret = str;
            }
        }
    } else {
        ret = fgets (str,
                     n,
                     stream);
    }
    return ret;
}

/* Write default template
 */
static
int
write_default_template(const char *     template_path)
{
    int     ret = -1;
    FILE *  f   = fopen(template_path, "r");
    if (0 == f) {
        f = fopen(template_path, "w");
        if (0 != f) {
            size_t i;
            for (i = 0; i < sizeof(default_template); ++i) {
                unsigned char ch = default_template[i];
                int j;
                for (j = 0; j < 4; ++j) {
                    fprintf(f, "%c", dtct[ch & 3]);
                    ch >>= 2;
                }
            }
            ret = fclose(f);
        }
    } else {
        fclose(f);
    }
    return ret;
}

/* Reset global state */
static
void
reset (void)
{
    memset(board, 0x00, sizeof(board));
    memset(history, 0x00, sizeof(history));
    idx_history = 0;
    pass = 0;
}

/* Write text representation to given file */
static
void
text (FILE * f, const char * title)
{
    int i;
    if (fCSV != opt_format) {
        if (0 != title) {
            fprintf(f, "%% %s\n", title);
        }
        for (i = 0; i < 81; ++i) {
            if (IS_EMPTY(i)) {
                fprintf(f, fStandard == opt_format ? " ." : ".");
            } else {
                fprintf(f, fStandard == opt_format ? "%2d" : "%d",
                            GET_DIGIT(board[i]));
            }
            if (8 == COLUMN(i)) {
                fprintf(f, "\n");
                if (fStandard == opt_format && i != 80 && 2 == ROW(i) % 3) {
                    fprintf(f, "-------+-------+-------\n");
                }
            } else if (fStandard == opt_format && 2 == COLUMN(i) % 3) {
                fprintf(f, " |");
            }
        }
    } else {
        for (i = 0; i < 81; ++i) {
            if (!IS_EMPTY(i)) {
                fprintf (f, "%d", GET_DIGIT(board[i]));
            }
            if (8 == COLUMN(i)) {
                fprintf(f, "\n");
            } else {
                fprintf(f, ",");
            }
        }
    }
}

/* Write PostScript representation to given file */
static
void
postscript (FILE * f, const char * title)
{
#define PS_WIDTH          20   /* Size of each box (points) */
#define PS_MARGIN         5    /* Margin around board (points) */
#define PS_THICK          3    /* Width of thick lines (points) */
#define PS_THIN           1    /* Width of thin lines (points) */
#define PS_BASELINE       5    /* Offset of character base line */

#define PS_TOTWIDTH  (9*PS_WIDTH+2*PS_MARGIN)  /* Total board width */

/* Page size */
#define PS_A4_WIDTH       612
#define PS_A4_HEIGHT      792

#define PS_LEFT_OFFSET    ((PS_A4_WIDTH - PS_TOTWIDTH)/2)
#define PS_BASE_OFFSET    ((PS_A4_HEIGHT - PS_TOTWIDTH)/2)

#define _STR(x)      #x
#define STR(x)       _STR(x)

    int i;
    time_t t;

    time(&t);
    fprintf(f,
            "%%!PS-Adobe-3.0 EPSF-3.0\n"
            "%%%%BoundingBox: %d %d %d %d\n"
            "%%%%Creator: Sudoku by Michael Kennett\n"
            "%%%%CreationDate: %s",
            PS_LEFT_OFFSET,
            PS_BASE_OFFSET,
            PS_LEFT_OFFSET + PS_TOTWIDTH,
            PS_BASE_OFFSET + PS_TOTWIDTH,
            ctime(&t));
    if (0 != title) {
        fprintf(f, "%%%%Title: %s\n", title);
    }
    fprintf(f, "%%%%EndComments\n");

    /* Write the board contents as a string */
    fprintf(f, "(");
    for (i = 0; i < 81; ++i) {
        if (!IS_EMPTY(i)) {
            fprintf(f, "%d", GET_DIGIT(board[i]));
        } else {
            fprintf(f, " ");
        }
    }
    fprintf(f, ")\n");

    /* Co-ordinate transform */
    fprintf(f, "%d %d translate\n", PS_LEFT_OFFSET, PS_BASE_OFFSET);

    /* Draw board - thin lines first, then thick lines */
    fprintf(f,
            "0 setgray\n"
             STR(PS_THIN)
             " setlinewidth "
             "1 8 "                         /* index, followed by loop count */
             "{dup "                                           /* keep index */
              STR(PS_WIDTH) " mul "
              STR(PS_MARGIN) " add "                     /* Compute position */
              "dup dup dup\n"                                    /* 4 copies */
              "  " STR(PS_MARGIN) " moveto "                /* vertical line */
              "%d lineto "
              STR(PS_MARGIN) " exch moveto "              /* horizontal line */
              "%d exch lineto "
              "1 add"                                        /* update index */
             "} repeat pop stroke\n"
             STR(PS_THICK) " setlinewidth "   /* Repeat code for thick lines */
             "1 2 "
             "{dup "
              "%d mul " STR(PS_MARGIN) " add "
              "dup dup dup\n"
              "  " STR(PS_MARGIN) " moveto "
              "%d lineto "
              STR(PS_MARGIN) " exch moveto "
              "%d exch lineto "
              "1 add"
             "} repeat pop stroke\n"
             /* Draw outside border */
             "1 setlinejoin "
             STR(PS_MARGIN) " " STR(PS_MARGIN) " moveto "
             STR(PS_MARGIN) " %d lineto "
             "%d %d lineto "
             "%d " STR(PS_MARGIN) " lineto closepath stroke\n",
            PS_TOTWIDTH - PS_MARGIN,
            PS_TOTWIDTH - PS_MARGIN,
            3 * PS_WIDTH,
            PS_TOTWIDTH - PS_MARGIN,
            PS_TOTWIDTH - PS_MARGIN,
            PS_TOTWIDTH - PS_MARGIN,
            PS_TOTWIDTH - PS_MARGIN,
            PS_TOTWIDTH - PS_MARGIN,
            PS_TOTWIDTH - PS_MARGIN);
    /* Now the code for drawing digits */
    fprintf(f, 
            "/Helvetica-Bold findfont 12 scalefont setfont\n"
              "0 81 "                       /* index, followed by loop count */
              "{2 copy 1 getinterval "                     /* load character */
               "dup stringwidth pop\n"              /* and compute the width */
               "  " STR(PS_WIDTH) " exch sub 2 div "      /* and the x-delta */
               "2 index "                                    /* reload index */
               "9 mod " STR(PS_WIDTH) " mul add "
               STR(PS_MARGIN) " add\n"               /* compute x coordinate */
               "  8 3 index 9 idiv sub "
               STR(PS_WIDTH) " mul %d add\n"         /* compute y coordinate */
               "  moveto show "                                    /* render */
               "1 add"                                       /* update index */
              "} repeat pop pop\n",
            PS_MARGIN + PS_BASELINE);
}

static
void
html (FILE * f, const char * title)
{
    int i;

    fprintf(f, "<html><head>");
    if (0 != title) {
        fprintf(f, "<title>%s</title>", title);
    }
    fprintf(f,
            "</head><body>"
            "<table"
                " align=\"center\" border=\"1\""
                " cellpadding=\"3\" cellspacing=\"1\""
                " rules=\"all\""
            " >\n");

    for (i = 0; i < 81; ++i) {
        if (0 == i % 9) {
            fprintf(f, "<tr>");
        }
        fprintf(f, "<td>");
        if (IS_EMPTY(i)) {
            fprintf(f, "&nbsp;&nbsp;&nbsp;");
        } else {
            fprintf(f, "&nbsp;%d&nbsp;", GET_DIGIT(board[i]));
        }
        fprintf(f, "</td>");
        if (8 == i % 9) {
            fprintf(f, "</tr>\n");
        }
    }
    fprintf(f,
            "</table>"
            "</body>"
            "</html>\n");
}

static
void
print (FILE * f, const char * title)
{
    switch (opt_format) {
        case fStandard:
        case fCompact:
        case fCSV:
            text(f, title);
            break;
        case fPostScript:
            postscript(f, title);
            break;
        case fHTML:
            html(f, title);
            break;
    }
}

/* Describe solution history */
static
void
describe (FILE * f)
{
    int i, j;
    for (i = j = 0; i < idx_history; ++i) {
        if (0 == (history[i] & FIXED)) {
            if (i < idx_history && 0 < j) {
                fprintf(f, 0 == j % 6 ? "\n" : ", ");
            }
            fprintf(f, "%d %c> (%d,%d)", GET_DIGIT(history[i]),
                                      history[i] & CHOICE ? '*' : '-',
                                      1 + ROW(GET_INDEX(history[i])),
                                      1 + COLUMN(GET_INDEX(history[i])));
            ++j;
        }
    }
    fprintf(f, "\n");
}

/* Management of the move history - compression */
static
void
compress(int limit)
{
    int i, j, k;
    /* Find the first ignored history item, if there is any. */
    for (k = 0; k < idx_history; ++k) {
        if (0 != (history[k] & IGNORED)) {
            break;
        }
    }
    /* Move the rest to compress the history */
    for (i = j = k; i < idx_history && j < limit; ++i) {
        if (!(history[i] & IGNORED)) {
            history[j++] = history[i];
        }
    }
    for (; i < idx_history; ++i) {
        history[j++] = history[i];
    }
    idx_history = j;
}

/* Management of the move history - adding a move */
static
void
add_move (int idx, int digit, int choice)
{
    int i;

    if (sizeof(history) / sizeof(int) - 1 <= idx_history) {
        compress(81);
    }
    /* Never ignore the last move */
    history[idx_history++] = SET_INDEX(idx) | SET_DIGIT(digit) | choice;

    /* Ignore all previous references to idx */
    for (i = idx_history - 2; 0 <= i; --i) {
        if (GET_INDEX(history[i]) == idx) {
            history[i] |= IGNORED;
            break;
        }
    }
}

/* Iteration over rows/columns/blocks handled by specialised code.
 * Each function returns a block index - call must manage element/idx.
 */
static
int
idx_row (int el, int idx)      /* Index within a row */
{
    return INDEX(el, idx);
}

static
int
idx_column (int el, int idx)   /* Index within a column */
{
    return INDEX (idx, el);
}

static
int
idx_block (int el, int idx)    /* Index within a block */
{
    return INDEX(3 * (el / 3) + idx / 3, 3 * (el % 3) + idx % 3);
}

/* Update board state after setting a digit (clearing not handled)
 */
static
void
update (int idx)
{
    const int row = ROW(idx);
    const int col = COLUMN(idx);
    const int block = IDX_BLOCK(row, col);
    const int mask = DIGIT_STATE(DIGIT(idx));
    int i;

    board[idx] |= STATE_MASK;  /* filled - no choice possible */

    /* Digit cannot appear in row, column or block */
    for (i = 0; i < 9; ++i) {
        board[idx_row(row, i)] |= mask;
        board[idx_column(col, i)] |= mask;
        board[idx_block(block, i)] |= mask;
    }
}

/* Refresh board state, given move history. Note that this can yield
 * an incorrect state if the user has made errors - return -1 if an
 * incorrect state is generated; else return 0 for a correct state.
 */
static
int
reapply (void)
{
    int digit, idx, j;
    int allok = 0;
    memset(board, 0x00, sizeof(board));
    for (j = 0; j < idx_history; ++j) {
        if (!(history[j] & IGNORED) && 0 != GET_DIGIT(history[j])) {
            idx = GET_INDEX(history[j]);
            digit = GET_DIGIT(history[j]);
            if (!IS_EMPTY(idx) || DISALLOWED(idx, digit)) {
                allok = -1;
            }
            board[idx] = SET_DIGIT(digit);
            if (history[j] & FIXED) {
                board[idx] |= FIXED;
            }
            update(idx);
        }
    }
    return allok;
}

/* Clear moves, leaving fixed squares
 */
static
void
clear_moves (void)
{
    for (idx_history = 0; history[idx_history] & FIXED; ++idx_history) {
       ;
    }
    reapply();
}

static int digits[9];  /* # digits expressed in element square */
static int counts[9];  /* Counts of the allowed positions indexed by digit-1 */

/* Count # set bits (within STATE_MASK) */
static
int
numset (int mask)
{
    int i, n = 0;
    for (i = STATE_SHIFT + 1; i <= STATE_SHIFT + 9; ++i) {
        if (mask & (1<<i)) {
            ++n;
        } else {
            ++counts[i - STATE_SHIFT - 1];
        }
    }
    return n;
}

static
void
count_set_digits (int el, int (*idx_fn)(int, int))
{
    int i;
    memset(counts, 0x00, sizeof(counts));
    for (i = 0; i < 9; ++i) {
        digits[i] = numset(board[(*idx_fn)(el, i)]);
    }
}

/* Fill square with given digit, and update state.
 * Returns 0 on success, else -1 on error (i.e. invalid fill)
 */
static
int
fill (int idx, int digit)
{
    assert(0 != digit);

    if (!IS_EMPTY(idx)) {
        return (DIGIT(idx) == digit) ? 0 : -1;
    }
    if (DISALLOWED(idx, digit)) {
        return -1;
    }

    board[idx] = SET_DIGIT(digit);
    update(idx);
    add_move(idx, digit, 0);

    return 0;
}

/* User-level fill square; allowing clears and overwrites, and
 * invalid moves...
 */
static
void
fillx(int idx, int digit)
{
    /* Nothing to do if digit already set or cleared */
    if (DIGIT(idx) == digit) {
        return;
    }
    if (0 != digit && IS_EMPTY(idx)) {
        board[idx] = SET_DIGIT(digit) | SET_INDEX(idx);
        update(idx);
        add_move(idx, digit, 0);
    } else {
        /* Clearing or overwriting is more time consuming */
        add_move(idx, 0, 0);
        reapply();

        /* Always apply moves - even in invalid */
        if (0 != digit) {
            if (idx_history > 0) {
                history[idx_history - 1] |= SET_DIGIT(digit);
                reapply();
            } else {
                beep();
            }
        }
    }
}

/* Find all squares with a single digit allowed -- do not mutate board
 * Additionally check for each digit the contradiction whether there
 * are all squares forbidden while there is no such digit placed yet
 * Return -1 on contradiction, and 0 otherwise.
 */
static
int
singles (int el, int (*idx_fn)(int, int), int hintcode)
{
    int i;

    count_set_digits(el, idx_fn);

    for (i = 0; i < 9; ++i) {
        int c = i;
        if (0 == counts[c]) {
            /* No allowed position left for digit 'c+1' in the element. */
            int nd = 0;
            int j;
            for (j = 0; j < 9; ++j) {
                if (DIGIT((*idx_fn)(el, j)) == c + 1) {
                    /* The element already contains this digit */
                    ++nd;
                    break;
                }
            }
            if (nd == 0) {
                /* Contradiction found, this element can't have this  digit. */
                break;
            }
        }
        if (1 == counts[c] &&
            idx_possible < 81) {
            /* One allowed position left for digit 'c+1' in the element. */
            int j;
            for (j = 0; j < 9; ++j) {
                /* Let's find the place. */
                int idx = (*idx_fn)(el, j);
                if (!DISALLOWED(idx, c + 1)) {
                    possible[idx_possible++] = SET_INDEX(idx)
                                               | SET_DIGIT(c + 1)
                                               | hintcode;
                    /* We have found that single position, safe to break. */
                    break;
                }
            }
        }
        if (8 == digits[i] &&
            idx_possible < 81) {
            /* 8 digits are masked at this position - just one remaining */
            int idx = (*idx_fn)(el, i);
            int sta = (STATE_MASK & ~STATE(idx));
            int d = 0;
            /* Let's find the appropriate digit */
            for (sta >>= STATE_SHIFT + 1; 0 != sta; sta >>= 1) {
                ++d;
            }
            assert (0 < d && d < 10 && !DISALLOWED(idx, d));
            if (0 < d && d < 10) {
                /* It seems to be an appropriate digit */
                possible[idx_possible++] = SET_INDEX(idx)
                                            | SET_DIGIT(d)
                                            | hintcode;
            }
        }
    }
    return -(i < 9);
}

/* Given the board state, find all possible 'moves' (i.e. squares with just
 * a single digit).
 *
 * Returns the number of (deterministic) moves (and fills the moves array),
 * or 0 if no moves are possible. This function does not mutate the board
 * state, and hence, can return the same move multiple times (with
 * different hints).
 */
static
int
findmoves (void)
{
    int el;

    idx_possible = 0;
    for (el = 0; el < 9; ++el) {
        if (-1 == singles(el, idx_row, HINT_ROW)        ||
            -1 == singles(el, idx_column, HINT_COLUMN)  ||
            -1 == singles(el, idx_block, HINT_BLOCK)) {
            return -1;
        }
    }
    return idx_possible;
}

/* Strategies for refining the board state
 *  - 'pairs'     if there are two unfilled squares in a given row/column/
 *                block with the same state, and just two possibilities,
 *                then all other unfilled squares in the row/column/block
 *                CANNOT be either of these digits.
 *  - 'block'     if the unknown squares in a block all appear in the same
 *                row or column, then all unknown squares outside the block
 *                and in the same row/column cannot be any of the unknown
 *                squares in the block.
 *  - 'common'    if all possible locations for a digit in a block appear
 *                in a row or column, then that digit cannot appear outside
 *                the block in the same row or column.
 *  - 'position2' if the positions of 2 unknown digits in a block match
 *                identically in precisely 2 positions, then those 2 positions
 *                can only contain the 2 unknown digits.
 *
 * Recall that each state bit uses a 1 to prevent a digit from
 * filling that square.
 */

static
void
pairs (int el, int (*idx_fn)(int, int))
{
    int i, j, k, mask, idx;
    for (i = 0; i < 8; ++i) {
        if (7 == digits[i]) { /* 2 digits unknown */
            for (j = i + 1; j < 9; ++j) {
                idx = (*idx_fn)(el, i);
                if (STATE(idx) == STATE((*idx_fn)(el, j))) {
                    /* Found a row/column pair - mask other entries */
                    mask = STATE_MASK ^ (STATE_MASK & board[idx]);
                    for (k = 0; k < i; ++k) {
                        board[(*idx_fn)(el, k)] |= mask;
                    }
                    for (k = i + 1; k < j; ++k) {
                        board[(*idx_fn)(el, k)] |= mask;
                    }
                    for (k = j + 1; k < 9; ++k) {
                        board[(*idx_fn)(el, k)] |= mask;
                    }
                    digits[j] = -1; /* now processed */
                }
            }
        }
    }
}

/* Worker: mask elements outside block */
static
void
exmask (int mask, int block, int el, int (*idx_fn)(int, int))
{
    int i, idx;

    for (i = 0; i < 9; ++i) {
        idx = (*idx_fn)(el, i);
        if (block != BLOCK(idx) && IS_EMPTY(idx)) {
            board[idx] |= mask;
        }
    }
}

/* Worker for block() */
static
void
exblock (int block, int el, int (*idx_fn)(int, int))
{
    int i, idx, mask;

    /* By assumption, all unknown squares in the block appear in the
     * same row/column, so to construct a mask for these squares, it
     * is sufficient to invert the mask for the known squares in the
     * block.
     */
    mask = 0;
    for (i = 0; i < 9; ++i) {
        idx = idx_block(block, i);
        if (!IS_EMPTY(idx)) {
            mask |= DIGIT_STATE(DIGIT(idx));
        }
    }
    exmask(mask ^ STATE_MASK, block, el, idx_fn);
}

static
void
block (int el)
{
    int i, idx, row, col;

    /* Find first unknown square */
    for (i = 0; i < 9 && !IS_EMPTY(idx = idx_block(el, i)); ++i) {
       ;
    }
    if (i < 9) {
        assert(IS_EMPTY(idx));
        row = ROW(idx);
        col = COLUMN(idx);
        for (++i; i < 9; ++i) {
            idx = idx_block(el, i);
            if (IS_EMPTY(idx)) {
                if (ROW(idx) != row) {
                    row = -1;
                }
                if (COLUMN(idx) != col) {
                    col = -1;
                }
            }
        }
        if (0 <= row) {
            exblock(el, row, idx_row);
        }
        if (0 <= col) {
            exblock(el, col, idx_column);
        }
    }
}

static
void
common (int el)
{
    int i, idx, row, col, digit, mask;

    for (digit = 1; digit <= 9; ++digit) {
        mask = DIGIT_STATE(digit);
        row = col = -1;  /* Value '9' indicates invalid */
        for (i = 0; i < 9; ++i) {
            /* Digit possible? */
            idx = idx_block(el, i);
            if (IS_EMPTY(idx) && 0 == (board[idx] & mask)) {
                if (row < 0) {
                    row = ROW(idx);
                } else if (row != ROW(idx)) {
                    row = 9; /* Digit appears in multiple rows */
                }
                if (col < 0) {
                    col = COLUMN(idx);
                } else if (col != COLUMN(idx)) {
                    col = 9; /* Digit appears in multiple columns */
                }
            }
        }
        if (-1 != row && row < 9) {
            exmask(mask, el, row, idx_row);
        }
        if (-1 != col && col < 9) {
            exmask(mask, el, col, idx_column);
        }
    }
}

/* Encoding of positions of a digit (c.f. position2()) - abuse DIGIT_STATE */
static int posn_digit[10];

static
void
position2 (int el)
{
    int digit, digit2, i, mask, mask2, posn, count, idx;

    /* Calculate positions of each digit within block */
    for (digit = 1; digit <= 9; ++digit) {
        mask = DIGIT_STATE(digit);
        posn_digit[digit] = count = posn = 0;
        for (i = 0; i < 9; ++i) {
            if (0 == (mask & board[idx_block(el, i)])) {
                ++count;
                posn |= DIGIT_STATE(i);
            }
        }
        if (2 == count) {
            posn_digit[digit] = posn;
        }
    }
    /* Find pairs of matching positions, and mask */
    for (digit = 1; digit < 9; ++digit) {
        if (0 != posn_digit[digit]) {
            for (digit2 = digit + 1; digit2 <= 9; ++digit2) {
                if (posn_digit[digit] == posn_digit[digit2]) {
                    mask = STATE_MASK
                           ^ (DIGIT_STATE(digit) | DIGIT_STATE(digit2));
                    mask2 = DIGIT_STATE(digit);
                    for (i = 0; i < 9; ++i) {
                        idx = idx_block(el, i);
                        if (0 == (mask2 & board[idx])) {
                            assert(0 == (DIGIT_STATE(digit2) & board[idx]));
                            board[idx] |= mask;
                        }
                    }
                    posn_digit[digit] = posn_digit[digit2] = 0;
                    break;
                }
            }
        }
    }
}

/* Find some moves for the board; starts with a simple approach (finding
 * singles), and if no moves found, starts using more involved strategies
 * until a move is found. The more advanced strategies can mask states
 * in the board, making this an efficient mechanism, but difficult for
 * a human to understand.
 */
static
int
allmoves (void)
{
    int i, n;

    n = findmoves();
    if (0 != n) {
        return n;
    }

    for (i = 0; i < 9; ++i) {
        count_set_digits(i, idx_row);
        pairs(i, idx_row);

        count_set_digits(i, idx_column);
        pairs(i, idx_column);

        count_set_digits(i, idx_block);
        pairs(i, idx_block);
    }
    n = findmoves();
    if (0 != n) {
        return n;
    }
    for (i = 0; i < 9; ++i) {
        block(i);
        common(i);
        position2(i);
    }
    return findmoves();
}

/* Helper: sort based on index */
static
int
cmpindex (const void * a, const void * b)
{
    return GET_INDEX(*((const int *)b)) - GET_INDEX(*((const int *)a));
}

/* Return number of hints. The hints mechanism should attempt to find
 * 'easy' moves first, and if none are possible, then try for more
 * cryptic moves.
 */
int
findhints (void)
{
    int i, n, mutated = 0;

    n = findmoves();
    if (n < 2) {
        /* Each call to pairs() can mutate the board state, making the
         * hints very, very cryptic... so later undo the mutations.
         */
        for (i = 0; i < 9; ++i) {
            count_set_digits(i, idx_row);
            pairs(i, idx_row);

            count_set_digits(i, idx_column);
            pairs(i, idx_column);

            count_set_digits(i, idx_block);
            pairs(i, idx_block);
        }
        mutated = 1;
        n = findmoves();
    }
    if (n < 2) {
        for (i = 0; i < 9; ++i) {
            block(i);
            common(i);
        }
        mutated = 1;
        n = findmoves();
    }

    /* Sort the possible moves, and allow just one hint per square */
    if (0 < n) {
        int i, j;

        qsort(possible, n, sizeof(int), cmpindex);
        for (i = 0, j = 1; j < n; ++j) {
            if (GET_INDEX(possible[i]) == GET_INDEX(possible[j])) {
                /* Let the user make mistakes - do not assume the
                 * board is in a consistent state.
                 */
                if (GET_DIGIT(possible[i]) == GET_DIGIT(possible[j])) {
                    possible[i] |= possible[j];
                }
            } else {
                i = j;
            }
        }
        n = i + 1;
    }

    /* Undo any mutations of the board state */
    if (mutated) {
        reapply();
    }
    return n;
}

/* Deterministic solver; return 0 on success, else -1 on error.
 */
static
int
deterministic (void)
{
    int i, n;

    n = allmoves();
    while (0 < n) {
        ++pass;
        for (i = 0; i < n; ++i) {
            if (-1 == fill(GET_INDEX(possible[i]),
                            GET_DIGIT(possible[i]))) {
                return -1;
            }
        }
        n = allmoves();
    }
    return n;
}

/* Return index of square for choice.
 *
 * If no choice is possible (i.e. board solved or inconsistent),
 * return -1.
 *
 * The current implementation finds a square with the minimum
 * number of unknown digits (i.e. maximum # masked digits).
 */
static
int
cmp (const void * e1, const void * e2)
{
    return GET_DIGIT(*(const int *)e2) - GET_DIGIT(*(const int *)e1);
}

static
int
choice (void)
{
    int i, n;
    for (n = i = 0; i < 81; ++i) {
        if (IS_EMPTY(i)) {
            possible[n] = SET_INDEX(i) | SET_DIGIT(numset(board[i]));

            /* Inconsistency if square unknown, but nothing possible */
            if (9 == GET_DIGIT(possible[n]))
                return -2;
            ++n;
        }
    }
    if (0 == n) {
        return -1;      /* All squares known */
    }
    qsort(possible, n, sizeof(possible[0]), cmp);
    return GET_INDEX(possible[0]);
}

/* Choose a digit for the given square.
 * The starting digit is passed as a parameter.
 * Returns -1 if no choice possible.
 */
static
int
choose (int idx, int digit)
{
    for (; digit <= 9; ++digit) {
        if (!DISALLOWED(idx, digit)) {
            board[idx] = SET_DIGIT(digit);
            update(idx);
            add_move(idx, digit, CHOICE);
            return digit;
        }
    }
    return -1;
}

/* Backtrack to a previous choice point, and attempt to reseed
 * the search. Return -1 if no further choice possible, or
 * the index of the changed square.
 *
 * Assumes that the move history and board are valid.
 */
static
int
backtrack (void)
{
    int digit, idx;

    for (; 0 < --idx_history;) {
        if (history[idx_history] & CHOICE) {
            /* Remember the last choice, and advance */
            idx = GET_INDEX(history[idx_history]);
            digit = GET_DIGIT(history[idx_history]) + 1;
            reapply();
            if (-1 != choose(idx, digit)) {
                return idx;
            }
        }
    }
    return -1;
}

/* Attempt to solve 'board'; return 0 on success else -1 on error.
 *
 * The solution process attempts to fill-in deterministically as
 * much of the board as possible. Once that is no longer possible,
 * need to choose a square to fill in.
 */
static
int
solve (void)
{
    int idx;

    for (;;) {
        if (0 == deterministic()) {
            /* Solved, make a new choice, or rewind a previous choice */
            idx = choice();
            if (-1 == idx) {
                idx = 0;
                break;
            } else if ((idx < 0 || -1 == choose(idx, 1))
                       && -1 == backtrack()) {
                idx = -1;
                break;
            }
        } else {
            /* rewind to a previous choice */
            if (-1 == backtrack()) {
                idx = -1;
                break;
            }
        }
    }
    return idx;
}

/* Find all solutions to a given board, and return the number of
 * solutions (0 if none found).
 */
static
int
number_solutions (void)
{
    int count = 0;
    if (-1 != solve()) {
        do {
            ++count;
        } while (-1 != backtrack() && -1 != solve());
    }
    return count;
}

/* Build/modify internal representation from file
 *
 *  - lines starting with '#' are ignored
 *  - puzzles start with a line beginning with '%' (with optional title)
 *  - two formats are support: compact, verbose
 *  - templates are always compact
 *  - compact boards have no spaces or block separators
 *  - verbose boards have spaces and block separators
 *
 * When is_tmplt is TRUE, a template is read.
 *
 * Return 0 on success; else -1 on error
 */

static char line[80];
static char title[80];

#define COMPACT  0
#define VERBOSE  1

static
int
read_board (FILE * f, int is_tmplt)
{
    char * p, * q;
    int i, row, col, type = COMPACT;

    reset();
    len_tmplt = 0;

    /* Skip lines until a '%' is found */
    line[0] = ' ';
    while ('%' != line[0]) {
        if (0 == tgets(line, sizeof(line), f)) {
            return -1;
        }
    }
    /* Read optional title, and removing trailing whitespace */
    for (p = line + 1; *p && isspace(*p); ++p) {
       ;
    }
    if (*p) {
        for (q = title; '\0' != (*q++ = *p++);) {
           ;
        }
        --q;
        while (isspace(*--q)) {
           ;
        }
        *++q = '\0';
    } else {
        strcpy(title, "(untitled)");
    }
    /* Consume comment lines - no leading spaces allowed */
    line[0] = '#';
    while ('#' == line[0]) {
        if (0 == tgets(line, sizeof(line), f)) {
            return -1;
        }
    }
    /* Analyse first line to determine the 'type' - default is COMPACT */
    if (0 == is_tmplt) {
        for (p = line; *p && COMPACT == type; ++p) {
            if ('|' == *p) {
                type = VERBOSE;
            }
        }
    }
    /* Consume grid - allow leading spaces and comments at end */
    for (row = 0; row < 9; ++row) {
        /* Assume line already loaded into buffer; skip leading spaces */
        for (p = line; *p && isspace(*p); ++p) {
           ;
        }
        for (col = 0; *p && col < 9; ++col, ++p) {
            if (is_tmplt) {
                if ('*' == *p) {
                    tmplt[len_tmplt++] = INDEX(row, col);
                }
            } else {
                if (VERBOSE == type) {
                    while (*p && (isspace(*p) || '|' == *p)) {
                        ++p;
                    }
                }
                if (isdigit(*p)) {
                    if (0 != fill(INDEX(row, col), *p - '0')) {
                        return -1;
                    }
                    board[INDEX(row, col)] |= FIXED;
                }
                /* else assume blank square */
            }
        }
        /* Don't complain about any trailing characters - ignored silently */

        /* Load next line (if needed) */
        if (row < 8) {
            if (0 == tgets(line, sizeof(line), f)) {
                return -1;
            }
            if (VERBOSE == type
                && 2 == row % 3
                && 0 == tgets(line, sizeof(line), f)) {
                return -1; /* Skip separators */
            }
        }
    }

    /* Construct move history for a template */
    if (is_tmplt) {
        idx_history = 0;
        for (i = 0; i < 81; ++i) {
            if (0 != DIGIT(i)) {
                history[idx_history++] = i | (DIGIT(i) << 8);
            }
        }
    }
    /* Finally, markup all of these moves as 'fixed' */
    for (i = 0; i < idx_history; ++i) {
        history[i] |= FIXED;
    }
    return 0;
}

/**
 **  Curses screen interface
 **/

/* Screen geometry */
#define SUDOKU_LINE   2
#define TITLE_LINE    4
#define TOP           6
#define LEFT_LEFT     0
#define LEFT_MIDDLE  15
#define LEFT         27
#define SUDOKU_POS   35
#define BOTTOM       (TOP+3*4)
#define RIGHT        (LEFT+3*8)
#define LINE_SIZE    80
#define STATUS_LINE  20
#define FILE_LINE    21
#define LAST_LINE    23

/* Maintain some global state - current cursor position */
static int curx;
static int cury;

static int have_status;  /* True (non-zero) if status line set */
static int have_hint;    /* True (non-zero) If hint displayed */

static char statusline[LINE_SIZE];  /* Buffer for status line */

/* Render board background - assume 24x80 screen */
static
void
draw_screen (void)
{
    int i;

    wclear(stdscr);
    attron(A_BOLD);
    mvaddstr(SUDOKU_LINE, SUDOKU_POS, "Su-Do-Ku!");
    attroff(A_BOLD);

    for (i = 0; i < 3; ++i) {
        mvaddstr(TOP + 0 + 4 * i, LEFT, "+-------+-------+-------+");
        mvaddstr(TOP + 1 + 4 * i, LEFT, "|       |       |       |");
        mvaddstr(TOP + 2 + 4 * i, LEFT, "|       |       |       |");
        mvaddstr(TOP + 3 + 4 * i, LEFT, "|       |       |       |");
    }
    mvaddstr(TOP + 4 * 3, LEFT, "+-------+-------+-------+");

    mvaddstr(TOP + 2, LEFT_LEFT + 1, "Rules:");
    mvaddstr(TOP + 3, LEFT_LEFT + 2, "Fill the grid so that");
    mvaddstr(TOP + 4, LEFT_LEFT + 2, "every column, row and");
    mvaddstr(TOP + 5, LEFT_LEFT + 2, "3x3 box contains each");
    mvaddstr(TOP + 6, LEFT_LEFT + 2, "of the digits 1 to 9.");
    i = TOP + 7;
    if (0 == opt_restrict) {
        mvaddstr(++i, LEFT_LEFT + 1, "File:");
        mvaddstr(++i, LEFT_LEFT + 2, "s   save board");
        mvaddstr(++i, LEFT_LEFT + 2, "w   write template");
        mvaddstr(++i, LEFT_LEFT + 2, "o   open board");
        mvaddstr(++i, LEFT_LEFT + 2, "t   set board title");
    }

/*    mvaddstr(TOP - 1, RIGHT + 4, "Keys:"); */
    mvaddstr(TOP + 0, RIGHT + 8, "k");
    mvaddstr(TOP + 1, RIGHT + 4, "  h   l move cursor");
    mvaddstr(TOP + 2, RIGHT + 8, "j");
    mvaddstr(TOP + 3, RIGHT + 7, "1-9  place digit");
    mvaddstr(TOP + 4, RIGHT + 7, "0 .  clear digit");
    mvaddstr(TOP + 5, RIGHT + 8, "c   clear board");
    mvaddstr(TOP + 6, RIGHT + 8, "d   redraw the board");
    mvaddstr(TOP + 7, RIGHT + 8, "f   fix squares");
    mvaddstr(TOP + 8, RIGHT + 8, "n   new board");
    mvaddstr(TOP + 9, RIGHT + 8, "q   quit game");
    i = TOP + 9;
    mvaddstr(++i, RIGHT + 8, "r   restart");
    mvaddstr(++i, RIGHT + 8, "u   undo last move");
    mvaddstr(++i, RIGHT + 8, "v   solve");
    mvaddstr(++i, RIGHT + 8, "?   request hint");
}

/* Write board title */
static
void
write_title(const char * title)
{
    move(TITLE_LINE, LEFT_LEFT);
    wclrtoeol(stdscr);
    if (0 != title) {
        mvaddstr(TITLE_LINE,
                 (int)((LINE_SIZE - strlen(title)) / 2),
                 title);
    }
}

/* Move cursor to grid position, and force refresh */
static
void
move_to (int x, int y)
{
    curx = x;
    cury = y;

    move(TOP + 1 + y + y / 3,
         LEFT + 2 + 2 * (x + x / 3));

    wrefresh(stdscr);
}

/* Move cursor to next non-fixed square, and force refresh */
void
static
move_next (void)
{
    int is_loop = 0;
    do {
        if (curx < 8) {
            move_to(curx + 1, cury);
        } else if (cury < 8) {
            move_to(0, cury + 1);
        } else {
            move_to(0, 0);
            if (is_loop++ > 0) {
                /* We can get here if the user attempts to load a fully
                 * populated board. In that case it's better to break.  */
                break;
            }
        }
     } while (IS_FIXED(INDEX(cury, curx)));
}

/* Render status line */
static
void
set_status (const char * txt)
{
    mvaddstr(STATUS_LINE,
             (int)((LINE_SIZE - strlen(txt)) / 2),
             (char *)txt);

    move_to(curx, cury);
    wrefresh(stdscr);
    have_status = 1;
}

static
void
clear_status (void)
{
    move(STATUS_LINE, LEFT_LEFT);
    wclrtoeol(stdscr);
    move_to(curx, cury);
    have_status = 0;
}

/* Show meassge in the status line for 2 seconds */
static
void
status_message (const char * txt)
{
    beep();
    set_status(txt);
    usleep(2000000);
    clear_status();
}

/* Beep and show meassge in the status line for 2 seconds */
static
void
beep_status_message (const char * txt)
{
    beep();
    status_message(txt);
}

/* Render current board */
static
void
render (void)
{
    int i, x, y;

    for (i = 0; i < 81; ++i) {
        x = LEFT + 2 + 2 * (COLUMN(i) + COLUMN(i) / 3);
        y = TOP + 1 + ROW(i) + ROW(i) / 3;
        assert(0 <= DIGIT(i));
        assert(DIGIT(i) <= 9); /* XXX FAILING */
        if (IS_FIXED(i)) {
            attron(A_BOLD);
        }
        if (IS_EMPTY(i)) {
            mvaddch(y, x, '.');
        } else {
            mvaddch(y, x, '0' + DIGIT(i));
        }
        if (IS_FIXED(i)) {
            attroff(A_BOLD);
        }
    }
}

static
void
row_hint (int row)
{
    mvaddch(TOP + 1 + row + row / 3, LEFT - 2, '>');
    mvaddch(TOP + 1 + row + row / 3, RIGHT + 2, '<');
    move_to(curx, cury);
    have_hint = 1;
}

static
void
column_hint (int col)
{
    mvaddch(TOP - 1, LEFT + 2 + 2 * (col + col / 3), 'v');
    mvaddch(BOTTOM + 1, LEFT + 2 + 2 * (col + col / 3), '^');
    move_to(curx, cury);
    have_hint = 1;
}

static
void
block_hint (int block)
{
    int i, j;
    for (i = 0; i < 3; ++i) {
        j = 3 * (block / 3) + i;
        mvaddch(TOP + 1 + j + j / 3, LEFT - 2, '>');
        mvaddch(TOP + 1 + j + j / 3, RIGHT + 2, '<');
        j = 3 * (block % 3) + i;
        mvaddch(TOP - 1, LEFT + 2 + 2 * (j + j / 3), 'v');
        mvaddch(BOTTOM + 1, LEFT + 2 + 2 * (j + j / 3), '^');
    }
    move_to(curx, cury);
    have_hint = 1;
}

static
void
clear_hints (void)
{
    int i;
    for (i = 0; i < 9; ++i) {
        mvaddch(TOP + 1 + i + i / 3, LEFT - 2, ' ');
        mvaddch(TOP + 1 + i + i / 3, RIGHT + 2, ' ');
        mvaddch(TOP - 1, LEFT + 2 + 2 * (i + i / 3), ' ');
        mvaddch(BOTTOM + 1, LEFT + 2 + 2 * (i + i / 3), ' ');
    }
    have_hint = 0;
    move_to(curx, cury);
}

/* Fix all squares - if possible.
 * Returns -1 on error, in which case there is an error in the
 * current board; otherwise returns 0 if all is OK.
 */
static
int
fix (void)
{
    int i;

    if (0 == reapply()) {
        compress(idx_history);
        for (i = 0; i < idx_history; ++i) {
            history[i] |= FIXED;
        }
        reapply();
        render();
        if (idx_history < 81 && IS_FIXED(INDEX(cury, curx))) {
            move_next();
        } else {
            move_to(curx, cury);
        }
        return 0;
    } else {
        return -1;
    }
}

/* Limit factor definitions for the classification. */
#define LIMIT_FACTOR_VERY_EASY  15
#define LIMIT_FACTOR_EASY       11
#define LIMIT_FACTOR_MEDIUM     7
#define LIMIT_FACTOR_HARD       4
#define LIMIT_FACTOR_FIENDISH   0

static const char *     NAME_VERY_EASY  = "very easy";
static const char *     NAME_EASY       = "easy";
static const char *     NAME_MEDIUM     = "medium";
static const char *     NAME_HARD       = "hard";
static const char *     NAME_FIENDISH   = "fiendish";

/* Class limit and name for the classification. */
#define CL(ct)   ((LIMIT_FACTOR_##ct) * pass)
#define CN(ct)    NAME_##ct

static
const char *
class_name_by_score (int score)
{
    const char * name;
    if (CL(VERY_EASY) < score) {
        name = CN(VERY_EASY);
    } else if (CL(EASY) < score) {
        name = CN(EASY);
    } else if (CL(MEDIUM) < score) {
        name = CN(MEDIUM);
    } else if (CL(HARD) < score) {
        name = CN(HARD);
    } else {
        name = CN(FIENDISH);
    }
    return name;
}

/* Classify a SuDoKu, given its solution.
 *
 * The classification is based on the average number of possible moves
 * for each pass of the deterministic solver - it is a rather simplistic
 * measure, but gives reasonable results. Note also that the classification
 * is based on the first solution found (but does handle the pathological
 * case of multiple solutions). Note that the average moves per pass
 * depends just on the number of squares initially set... this simplifies
 * the statistics collection immensely, requiring just the number of passes
 * to be counted.
 *
 * Return 0 on error, else a string classification.
 */

static
const char *
classify (void)
{
    int i, score;

    pass = 0;
    clear_moves();
    if (-1 == solve()) {
        return 0;
    }

    score = 81;
    for (i = 0; i < 81; ++i) {
        if (IS_FIXED(i)) {
            --score;
        }
    }
    assert(81 == idx_history);

    for (i = 0; i < 81; ++i) {
        if (history[i] & CHOICE) {
            score -= 5;
        }
    }
    return  class_name_by_score(score);
}

/* exchange disjoint, identical length blocks of data */
static
void
exchange (int * a, int * b, int len)
{
    int i, tmp;
    for (i = 0; i < len; ++i) {
        tmp = a[i];
        a[i] = b[i];
        b[i] = tmp;
    }
}

/* rotate left */
static
void
rotate1_left (int * a, int len)
{
    int i, tmp;
    tmp = a[0];
    for (i = 1; i < len; ++i) {
        a[i - 1] = a[i];
    }
    a[len - 1] = tmp;
}

/* rotate right */
static
void
rotate1_right (int * a, int len)
{
    int i, tmp;
    tmp = a[len - 1];
    for (i = len - 1; 0 < i; --i) {
        a[i] = a[i - 1];
    }
    a[0] = tmp;
}

/* Generalised left rotation - there is a naturally recursive
 * solution that is best implementation using iteration.
 * Note that it is not necessary to do repeated unit rotations.
 *
 * This function is analogous to 'cutting' a 'pack of cards'.
 *
 * On entry: 0 < idx < len
 */
static
void
rotate (int * a, int len, int idx)
{
    int xdi = len - idx;
    int delta = idx - xdi;

    while (0 != delta && 0 != idx) {
        if (delta < 0) {
            if (1 == idx) {
                rotate1_left(a, len);
                idx = 0;
            } else {
                exchange(a, a + xdi, idx);
                len = xdi;
            }
        } else {
            /* 0 < delta */
            if (1 == xdi) {
                rotate1_right(a, len);
                idx = 0;
            } else {
                exchange(a, a + idx, xdi);
                a += xdi;
                len = idx;
                idx -= xdi;
            }
        }
        xdi = len - idx;
        delta = idx - xdi;
    }
    if (0 < idx) {
        exchange(a, a + idx, idx);
    }
}

/* Shuffle an array of integers */
static
void
shuffle (int * a, int len)
{
    int i, j, tmp;

    i = len;
    while (1 <= i) {
        j = rand() % i;
        tmp = a[--i];
        a[i] = a[j];
        a[j] = tmp;
    }
}

/* Generate a SuDoKu puzzle
 *
 * The generation process selects a random template, and then attempts
 * to fill in the exposed squares to generate a board. The order of the
 * digits and of filling in the exposed squares are random.
 */

/* Select random template; sets tmplt, len_tmplt */
static
void
select_template (void)
{
    int i = n_tmplt > 0 ? rand() % n_tmplt : 0;
    tseek(ftmplt, 0, SEEK_SET);
    while (0 <= i && 0 == read_board(ftmplt, 1)) {
        --i;
    }
}

static
void
generate (void)
{
    static int digits[9];

    int i;

    for (;;) {
        for (i = 0; i < 9; ++i) {
            digits[i] = i + 1;
        }

        rotate(digits, 9, 1 + rand() % 8);
        shuffle(digits, 9);
        select_template();

        rotate(tmplt, len_tmplt, 1 + rand() % (len_tmplt - 1));
        shuffle(tmplt, len_tmplt);

        reset();  /* construct a new board */

        for (i = 0; i < len_tmplt; ++i) {
            fill(tmplt[i], digits[i % 9]);
        }
        if (0 != solve() || idx_history < 81) {
            continue;
        }
        for (i = 0; i < len_tmplt; ++i) {
            board[tmplt[i]] |= FIXED;
        }
        /* Construct fixed squares */
        for (idx_history = i = 0; i < 81; ++i) {
            if (IS_FIXED(i)) {
                history[idx_history++] = SET_INDEX(i)
                                         | SET_DIGIT(DIGIT(i))
                                         | FIXED;
            }
        }
        clear_moves();

        if (0 != solve() || idx_history < 81) {
            continue;
        }
        if (-1 != backtrack() && 0 == solve()) {
            continue;
        }
        if (NULL != requested_class) {
            const char * generated_class = classify();
            sprintf(title, "randomly generated - %s", generated_class);
            write_title(title);
            wrefresh(stdscr);
            if (0 != strcmp(requested_class, generated_class)) {
                continue;
            }
        }

        break;
    }

    strcpy(title, "randomly generated - ");
    strcat(title, classify());

    clear_moves();
    time(&start_time);
}

/* Support for explicitly opened board */
static FILE * opened;

/* Support for precanned board (optional) */
static FILE * precanned;
static int n_precanned;
static int completed;

static
int
open_precanned (const char * filename)
{
    n_precanned = 0;
    precanned = fopen(filename, "r");
    if (0 != precanned) {
        while (0 == read_board(precanned, 0)) {
            ++n_precanned;
        }
    }
    /* return 0 on success, -1 on failure. */
    return (n_precanned > 0) - 1;
}

static
void
open_template (const char * filename)
{
    n_tmplt = 0;
    ftmplt = topen(filename, "r");
    if (0 != ftmplt) {
        while (0 == read_board(ftmplt, 1)) {
            ++n_tmplt;
        }
    }
}

static
int
is_complete (void)
{
    int i;
    for (i = 0; i < 81 && !IS_EMPTY(i); ++i) {
        /* counting the non-empty squares. */
    }
   return (81 == i);
}

/* load a new board - this could be a precanned board,
 * or a randomly generated board - chose between these
 * randomly -- 1 in 3 chance of loading a precanned
 * board (if they exist).
 */
static
void
load_board (void)
{
    int i = 0;
    if (opened) {
        /* Select next board */
        i = read_board(opened, 0);
        if (i--) {
            fclose(opened);
            opened = NULL;
        }
    } else if (precanned &&
               (0 == opt_random ||
                (0 == rand() % 3 && 0 < n_precanned))) {
        /* Select random board */
        i = rand() % n_precanned;
        fseek(precanned, 0, SEEK_SET);
        while (0 <= i && 0 == read_board(precanned, 0)) {
            --i;
        }
    }
    if (i != -1) {
        set_status("generating a random board... (please wait)");
        generate();
        clear_status();
    }

    /* Shameless plug... */
    set_status("Su-Do-Ku by Michael Kennett");

    render();
    write_title(title);

    curx = cury = 8;    /* move_next() takes care of this... */
    move_next();
    completed = is_complete();
    num_hints = -1;
    time(&start_time);
}

/* Manage rich text input (define 'virtual' key codes)
 * See the console(4) manpage.
 */

#define VKEY_IGNORE     (256)
#define VKEY_UP         (256+'A')
#define VKEY_DOWN       (256+'B')
#define VKEY_RIGHT      (256+'C')
#define VKEY_LEFT       (256+'D')
#define VKEY_HOME       (256+'H')
#define VKEY_INSERT     (256+'2')
#define VKEY_DELETE     (256+'3')
#define VKEY_END        (256+'4')
#define VKEY_PGUP       (256+'5')
#define VKEY_PGDOWN     (256+'6')
#define VKEY_BACK       (0x08)


static
int
getkey (void)
{
    int ch = wgetch(stdscr);
    if (0x1b == ch) {
        ch = wgetch(stdscr);
        if ('[' == ch) {
            ch = wgetch(stdscr);
            switch (ch) {
                case 'A': ch = VKEY_UP;     break;
                case 'B': ch = VKEY_DOWN;   break;
                case 'C': ch = VKEY_RIGHT;  break;
                case 'D': ch = VKEY_LEFT;   break;
                case 'H': ch = VKEY_HOME;   break;
                case '2': ch = VKEY_INSERT; break;
                case '3': ch = VKEY_DELETE; break;
                case '4': ch = VKEY_END;    break;
                case '5': ch = VKEY_PGUP;   break;
                case '6': ch = VKEY_PGDOWN; break;
                default:  ch = VKEY_IGNORE;
            }
            if (ch == VKEY_HOME ||
                ch == VKEY_DELETE ||
                ch == VKEY_INSERT ||
                ch == VKEY_END ||
                ch == VKEY_PGUP ||
                ch == VKEY_PGDOWN) {
                int ch4 = wgetch(stdscr);
                if (ch4 != '~') {
                    ch = VKEY_IGNORE;
                }
            }
        } else {
            /* Enabling the Esc Esc sequence to produce one Esc character */
            if (0x1b != ch) {
                ch = VKEY_IGNORE;
            }
        }
    }
    return ch;
}

/* Save board representation - the difficulty is managing the user interface
 * (i.e. finding where to save), with a tad more work involved in allowing
 * the board to be written to an arbitrary process.
 *
 * Persist the user filename.
 */
#if !defined (PATH_MAX)
/* If it is not defined by the operating system, we can safely define it,
 * since the code below has no strong dependence on the current working
 * directory, for which it is solely used. */
#define PATH_MAX 1024
#endif

static char userfile[PATH_MAX];
static char templatefile[PATH_MAX];

typedef struct edit_s {
    char *  eb;         /* edit buffer                                  */
    size_t  ebs;        /* edit buffer size                             */
    size_t  efs;        /* edit field horizontal size                   */
    int     efx;        /* edit field horizontal position               */
    int     efy;        /* edit field vertical position                 */
    int     ecn;        /* actual number of characters (including '\0') */
    int     ecp;        /* edit position in the buffer                  */
    int     x_0;        /* prompt position on the display               */
    int     x_1;        /* first text position on the display           */
    int     x_2;        /* last txt position on display                 */
    int     x_3;        /* continuation marker position on display      */
    int     x_mv;       /* maximal number of visible chars on display   */
    int     m_0;        /* first editable char position in the buffer   */
    int     m_1;        /* first visible char position in the buffer    */
    int     prompt;     /* currently displayed prompt character         */
    int     dprompt;    /* default prompt character                     */
    int     eol_mark;   /* line continuation marker                     */
    int     ins_mode;   /* insert mode                                  */
    int     ch;         /* actual character for processing              */
} edit_t;


/* Init edit text data.
 * The ep pointer must point to a valid memory location.
 */
static
void
el_init (edit_t *   ep,
         int        edit_field_y,
         int        edit_field_x,
         char *     edit_buffer,
         size_t     edit_buffer_size,
         size_t     edit_field_size)
{
    ep->eb          = edit_buffer;
    ep->ebs         = edit_buffer_size;
    ep->efs         = edit_field_size;
    ep->efx         = edit_field_x;
    ep->efy         = edit_field_y;
    ep->ecn         = (int)(strlen(edit_buffer));
    ep->ecp         = ep->ecn;
    ep->x_0         = edit_field_x;
    ep->x_1         = edit_field_x + 1;
    ep->x_2         = (int)edit_field_size - 2;
    ep->x_3         = ep->x_2 + 1;
    ep->x_mv        = ep->x_2 - ep->x_1;
    ep->m_0         = 0;
    ep->m_1         = 0;
    ep->prompt      = '$';
    ep->dprompt     = '$';
    ep->eol_mark    = '*';
    ep->ins_mode    = 1;
    ep->ch          = edit_buffer[0];

    /* Find out what should be the prompt character. */
    if (ep->ch == '>' ||
        ep->ch == '|' ||
        ep->ch == '%') {
        /* Learn the prompt and set the minimal 
         * index of the string buffer to 1.
         */
        ep->dprompt = ep->ch;
        ep->m_0     = 1;
        ep->m_1     = 1;
    }
    /* Calculate the visible part of the pathname. */
    if (ep->x_mv < ep->ecn - ep->m_0) {
        /* set the buffer position of the first visible character */
        ep->m_1 = ep->ecn - ep->x_mv;
    }

    if (ep->m_1 > 1) {
        /* give visual feedback about that there are more characters */
        ep->prompt = '<';
    } else {
        /* set the default prompt */
        ep->prompt = ep->dprompt;
    }
}

/* Functions to classify the actual character.
 * The ep pointer must point to a valid memory location.
 */
static
int
el_is_silent_ignore_char (edit_t *  ep)
{
    return (VKEY_UP == ep->ch ||
            VKEY_DOWN == ep->ch ||
            VKEY_IGNORE == ep->ch);
}

static
int
el_is_loud_ignore_char (edit_t *    ep)
{
    return ('|' == ep->ch ||
            '>' == ep->ch ||
            '<' == ep->ch);
}

static
int
el_is_changeable_prompt_char (edit_t *  ep)
{
    return (('%' != ep->prompt &&
             '$' != ep->prompt)
            && ('|' == ep->ch ||
                '>' == ep->ch));
}

/* Functions to classify the actual cursor position.
 * The ep pointer must point to a valid memory location.
 */
static
int
el_is_cursor_at_rightmost_position (edit_t *   ep)
{
    return (ep->ecp == ep->m_1 &&
            ep->m_1 == 1);
}

/* Functions to classify the actual cursor position.
 * The ep pointer must point to a valid memory location.
 */
static
void
el_adjust_markers (edit_t *   ep)
{
    int first_invisible = ep->m_1 + ep->x_mv;
    if (ep->m_0 == ep->m_1) {
        /* The first visible is the first editable character. */
        if (ep->prompt != ep->dprompt) {
            /* Show the default prompt if it is not shown yet. */
            ep->prompt = ep->dprompt;
            mvaddch(ep->efy, ep->x_0, ep->prompt);
        }
    } else {
        /* The first visible is not the first editable character. */
        if (ep->prompt != '<') {
            ep->prompt = '<';
            /* Show the '<' prompt. */
            mvaddch(ep->efy, ep->x_0, ep->prompt);
        }
    }
    if (ep->ecn > first_invisible) {
        /* The last visible is not the last editable character. */
        if (ep->eol_mark != '<') {
            ep->eol_mark = '<';
            mvaddch(ep->efy, ep->x_3, ep->eol_mark);
        }
    } else {
        if (ep->eol_mark != ' ') {
            ep->eol_mark = ' ';
            mvaddch(ep->efy, ep->x_3, ep->eol_mark);
        }
    }
}

/* Process change the prompt character.
 * The ep pointer must point to a valid memory location.
 */
static
void
el_process_change_prompt (edit_t *  ep)
{
    /* change the prompt */
    ep->dprompt = ep->ch;
    ep->prompt = ep->dprompt;
    /* Display the prompt character. */
    mvaddch(ep->efy, ep->x_0, ep->prompt);
}

/* Show the actual character.
 * The ep pointer must point to a valid memory location.
 */
static
void
el_show_actual_char (edit_t *  ep)
{
    if (ep->m_0 <= ep->m_1 &&
        ep->m_1 <= ep->ecp &&
        ep->ecp <= ep->ecn) {

        int first_invisible = ep->m_1 + ep->x_mv;

        if (ep->ecp < first_invisible) {
            /* Display the character */
            mvaddch(ep->efy,
                    ep->ecp - ep->m_1 + ep->x_1,
                    ep->eb[ep->ecp]);
        } else {
            if (ep->ecp != first_invisible ||
                ep->eb[ep->ecp] != '\0') {
                /* This should never happen. */
                beep();
            }
        }
    } else {
        /* This should never happen. */
        beep();
    }
}

/* Highlight the actual character in overwrite mode.
 * The ep pointer must point to a valid memory location.
 */
static
void
el_highlight_in_overwrite_mode (edit_t *    ep)
{
    if (!ep->ins_mode) {
        /* Overwrite mode, highlight the character at the current position. */
        attron(A_BOLD);
        el_show_actual_char(ep);
        attroff(A_BOLD);
    }
}

/* Remove the highlight at the current position in overwrite mode.
 * The ep pointer must point to a valid memory location.
 */
static
void
el_remove_highlight_in_overwrite_mode (edit_t *    ep)
{
    if (!ep->ins_mode) {
        /* Overwrite mode, remove the highlight at the current position. */
        el_show_actual_char(ep);
    }
}

/* Show the actual character and highlight in overwrite mode.
 * The ep pointer must point to a valid memory location.
 */
static
void
el_show_actual_char_with_highlight (edit_t *    ep)
{
    if (ep->ins_mode) {
        /* Insert mode */
        el_show_actual_char(ep);
    } else {
        /* Overwrite mode */
        el_highlight_in_overwrite_mode(ep);
    }
}

/* Process toggle insert mode.
 * The ep pointer must point to a valid memory location.
 */
static
void
el_process_toggle_insert_mode (edit_t *    ep)
{
    /* toggle insert mode. */
    ep->ins_mode = !(ep->ins_mode);
    el_show_actual_char_with_highlight(ep);
}

/* Process delete character at the current position.
 * The ep pointer must point to a valid memory location.
 */
static
void
el_process_delete_ch_at_current_position (edit_t *    ep)
{
    /* check if there is a character to delete at the current position */
    if (ep->m_0 <= ep->ecp && ep->ecp < ep->ecn) {
        /* delete character at the current position */
        --ep->ecn;
        if (ep->ecp < ep->ecn && ep->ecn > ep->m_0) {
            int first_invisible = ep->m_1 + ep->x_mv;
            int is_scroll_from_left = (ep->m_0 < ep->m_1 &&
                                       ep->ecn < first_invisible);
            /* The character is in middle of the string:
             *  remove the character at the cursor from the buffer
             */
            memmove(&(ep->eb[ep->ecp]),
                    &(ep->eb[ep->ecp + 1]),
                    (ep->ecn - ep->ecp) + 1);

            /* partial display of the right side
             * scroll left if it is not the last character,
             * or scroll right if it was the last
             */
            if (is_scroll_from_left) {
                /* Decrease the left limit, show one more from left. */
                --ep->m_1;
                /* redisplay the left side */
                if (ep->x_mv < ep->ecn - ep->m_1) {
                    /* partial display of the right side */
                    char che = ep->eb[ep->ecp];
                    ep->eb[ep->ecp] = 0;
                    mvaddstr(ep->efy, ep->x_1, &(ep->eb[ep->m_1]));
                    ep->eb[ep->ecp] = che;
                    el_show_actual_char(ep);
                } else {
                    mvaddstr(ep->efy, ep->x_1, &(ep->eb[ep->m_1]));
                }
            } else {
                /* redisplay the right side */
                if (ep->x_mv < ep->ecn - ep->m_1) {
                    /* partial display of the right side */
                    int first_invisible = ep->m_1 + ep->x_mv;
                    char che = ep->eb[first_invisible];
                    ep->eb[first_invisible] = 0;

                    mvaddstr(ep->efy,
                             ep->ecp - ep->m_1 + ep->x_1,
                             &(ep->eb[ep->ecp]));

                    ep->eb[first_invisible] = che;
                } else {
                    mvaddstr(ep->efy,
                             ep->ecp - ep->m_1 + ep->x_1,
                             &(ep->eb[ep->ecp]));
                }
            }
            /* Higliht the char in overwrite mode at the current position */
            el_highlight_in_overwrite_mode(ep);
        }
        ep->eb[ep->ecn] = '\0';
        if (ep->x_mv > ep->ecn - ep->m_1) {
            /* clear the deleted last character on the display */
            mvaddch(ep->efy, ep->ecn - ep->m_1 + ep->x_1, ' ');
        }
        /* Make sure that the proper markers displayed. */
        el_adjust_markers (ep);
    } else {
        beep();
    }
}

/* Process delete character at the current position.
 * The ep pointer must point to a valid memory location.
 */
static
void
el_process_move_cursor_left_or_right (edit_t *    ep)
{
    if ((ep->m_0 < ep->ecp && VKEY_LEFT == ep->ch) ||
        (ep->ecp < ep->ecn && VKEY_RIGHT == ep->ch)) {
        /* Remove the highlight at the previous position if the removal is needed. */
        int first_invisible = ep->m_1 + ep->x_mv;
        el_remove_highlight_in_overwrite_mode(ep);
        ep->ecp += (VKEY_RIGHT == ep->ch) - (VKEY_LEFT == ep->ch);
        if (ep->ecp < ep->m_1
            || ep->ecp > first_invisible
            || (ep->ecp == first_invisible && ep->ecp != ep->ecn)) {
            /* scroll right or left as needed */
            ep->m_1 += (ep->ecp >= first_invisible) - (ep->ecp < ep->m_1);
            first_invisible = ep->m_1 + ep->x_mv;
            if (first_invisible < (int)ep->ebs) {
                char chx = ep->eb[first_invisible];
                ep->eb[first_invisible] = 0;
                mvaddstr(ep->efy, ep->x_1, &(ep->eb[ep->m_1]));
                ep->eb[first_invisible] = chx;
            } else {
                mvaddstr(ep->efy, ep->x_1, &(ep->eb[ep->m_1]));
            }
        }
        /* Higliht the char in overwrite mode at the current position */
        el_highlight_in_overwrite_mode(ep);
        /* Make sure that the proper markers displayed. */
        el_adjust_markers (ep);
    } else {
        beep();
    }
}

/* Process destructive backspace.
 * The ep pointer must point to a valid memory location.
 */
static
void
el_process_destructive_backspace (edit_t *   ep)
{
    if (ep->m_0 < ep->ecp) {
        int first_invisible = ep->m_1 + ep->x_mv;
        int is_scroll_from_left = (ep->m_0 < ep->m_1);
        assert(ep->ecn >= ep->ecp);
        --ep->ecn;
        --ep->ecp;
        if (is_scroll_from_left) {
            /* Decrease the left limit, show one more from left. */
            --ep->m_1;
            --first_invisible;
        }
        if (ep->ecp <= ep->ecn && ep->ecn > ep->m_0) {
            /* Remove the deleted character. */

            memmove(&(ep->eb[ep->ecp]),
                    &(ep->eb[ep->ecp + 1]),
                    (ep->ecn - ep->ecp) + 1);

            if (is_scroll_from_left ||
                first_invisible < ep->ecn) {
                /* There is still a hidden part on the right side.
                 * Remove the highlight and show the modified string.
                 */
                if (is_scroll_from_left) {
                    /* scroll from left, redraw the left side */
                    char chk = ep->eb[ep->ecp];
                    ep->eb[ep->ecp] = 0;

                    mvaddstr(ep->efy,
                             ep->x_1,
                             &(ep->eb[ep->m_1]));

                    ep->eb[ep->ecp] = chk;

                    mvaddch(ep->efy,
                            ep->x_1 + ep->ecp - ep->m_1,
                            chk);

                } else {
                    /* Learn the first invisible character and replace
                     * it with a terminating 0.
                     */
                    char chr = ep->eb[first_invisible];
                    ep->eb[first_invisible] = 0;
                    /* Scroll from right, redraw the right side. */
                    mvaddstr(ep->efy,
                            ep->ecp - ep->m_1 + ep->x_1,
                            &(ep->eb[ep->ecp]));
                    /* Restore the hidden character in the buffer. */
                    ep->eb[first_invisible] = chr;
                }
            } else {
                /* The whole string is visible redraw the right side. */
                mvaddstr(ep->efy,
                        ep->ecp - ep->m_1 + ep->x_1,
                        &(ep->eb[ep->ecp]));
            }

            /* Higliht the char in overwrite mode at the current position */
            el_highlight_in_overwrite_mode(ep);
        }

        ep->eb[ep->ecn] = '\0';
        if (ep->ecn <= first_invisible) {
            /* Clear the earlier last character position if it is visible */
            mvaddch(ep->efy,
                    ep->ecn - ep->m_1 + ep->x_1,
                    ' ');
        }
        /* Make sure that the proper markers displayed. */
        el_adjust_markers (ep);
    } else {
        beep();
    }
}

/* Process visible character.
 * The ep pointer must point to a valid memory location.
 */
static
void
el_put_visible_char_in_the_middle (edit_t *    ep)
{
    int first_invisible     = ep->m_1 + ep->x_mv;
    int is_more_on_left     = (first_invisible < ep->ecn || ep->ins_mode);
    int is_scroll_to_left   = (first_invisible <= ep->ecp + is_more_on_left);
    int nsl                 = !(is_scroll_to_left);

    if (ep->ins_mode) {
        /* Right-shift the previous content. */
        memmove(&ep->eb[ep->ecp + 1],
                &ep->eb[ep->ecp],
                (ep->ecn - ep->ecp) + 1);
        ++ep->ecn;
    }
    ep->eb[ep->ecp] = (char)ep->ch;
    ep->eb[ep->ecn] = '\0';
    el_show_actual_char(ep);
    ++ep->ecp;

    if (is_scroll_to_left) {
        /* Scroll to left */
        ++ep->m_1;
        ++first_invisible;
    }
    /* Remove the previous highlight if there
     * was any, and show the modified text.
     */
    if (first_invisible < ep->ecn) {
        /* partial display from the cursor
         * or from the beginning of the visible part
         */
        char    che = ep->eb[first_invisible];

        ep->eb[first_invisible] = 0;

        mvaddstr(ep->efy,
                 nsl * (ep->ecp - ep->m_1) + ep->x_1,
                 &(ep->eb[nsl * (ep->ecp - ep->m_1) + ep->m_1]));

        ep->eb[first_invisible] = che;
    } else {
        /* display the entire right side from the cursor
         * or from the beginning of the visible part
         */
        mvaddstr(ep->efy,
                 nsl * (ep->ecp - ep->m_1) + ep->x_1,
                 &(ep->eb[nsl * (ep->ecp - ep->m_1) + ep->m_1]));
    }

    /* Higliht the char in overwrite mode at the current position */
    el_highlight_in_overwrite_mode(ep);
    /* Make sure that the proper markers displayed. */
    el_adjust_markers (ep);
}

/* Process a visible character.
 * The ep pointer must point to a valid memory location.
 */
static
void
el_process_visible_char (edit_t *    ep)
{
    if (ep->ecn < (int)ep->ebs - 1) {
        /* There is enough room in the input buffer. */
        if (ep->ecp < ep->ecn) {
            /* In the middle of the text. */
            assert(ep->ecp >= ep->m_1 && ep->m_1 >= ep->m_0);
            el_put_visible_char_in_the_middle (ep);
        } else if (ep->ecp == ep->ecn) {
            /* At the end of the text, insert and overwrite does the same. */
            int first_invisible = ep->m_1 + ep->x_mv;

            if (ep->ecp >= first_invisible) {
                if (ep->ecp == first_invisible) {
                    /* Scroll left. */
                    ++ep->m_1;
                    ep->eb[ep->ecp] = (char)ep->ch;
                    ep->ecn = ++ep->ecp;
                    ep->eb[ep->ecn] = '\0';
                    /* Display it. */
                    mvaddstr(ep->efy,
                             ep->x_1,
                             &(ep->eb[ep->m_1]));
                } else {    
                    /* Attempt to write way beyond the end of the visible area.
                     * This should never happen.
                     */
                    beep();
                }
            } else {
                /* There is enough room on the display. */
                ep->eb[ep->ecp] = (char)ep->ch;
                el_show_actual_char(ep);
                ep->ecn = ++ep->ecp;
                ep->eb[ep->ecn] = '\0';
            }
            /* Make sure that the proper markers displayed. */
            el_adjust_markers (ep);
        } else {
            /* Attempt to write beyond the end of the text.
             * This should never happen.
             */
            beep();
        }
    } else {
        /* The input buffer is full, no more character can be stored.
         * This might happen.
         */
        beep();
    }
}

/* Edit text at a given screen position.
 * The edit_buffer must point to a valid memory location.
 *
 * The first character has a special role, it can specify the prompt.
 * By default we are using the '$' character for the prompt.
 * If the text is longer than the edit filed size, than we change the
 * prompt to '<' and only the end of the text will be displayed first.
 */
static
int
edit_line (int      edit_field_y,
           int      edit_field_x,
           char *   edit_buffer,
           size_t   edit_buffer_size,
           size_t   edit_field_size)
{
    edit_t es;

    el_init (&es,
             edit_field_y,
             edit_field_x,
             edit_buffer,
             edit_buffer_size,
             edit_field_size);

    move(es.efy, es.efx);
    wclrtoeol(stdscr);
    /* Display the prompt character. */
    mvaddch(es.efy, es.x_0, es.prompt);

    /* Display the visible part of the pathname. */
    move(es.efy, es.x_1);
    mvaddstr(es.efy, es.x_1, &es.eb[es.m_1]);

    es.ch = VKEY_IGNORE;
    while ('\r' != es.ch && es.ch != '\n') {
        /* Break if Esc pressed
         * Esc should be pressed twice, otherwise we cannot get it.
         */
        if (es.ch == 0x1B) {
            /* Use the zero length to propagate the end condition. */
            es.ecn = 0;
            break;
        }
        if (el_is_silent_ignore_char(&es)) {
            /* Silently ignore these characters. */
        } else if (el_is_loud_ignore_char(&es)) {
            if (el_is_changeable_prompt_char(&es) && 
                el_is_cursor_at_rightmost_position(&es)) {
                /* change the prompt */
                el_process_change_prompt(&es);
            } else {
                /* Loudly ignore these characters. */
                beep();
            }
        } else if (VKEY_INSERT == es.ch) {
            /* toggle insert mode. */
            el_process_toggle_insert_mode(&es);
        } else if (VKEY_DELETE == es.ch) {
            /* delete character at the current position */
            el_process_delete_ch_at_current_position(&es);
        } else if (VKEY_LEFT == es.ch ||
                   VKEY_RIGHT == es.ch) {
            /* nondestructive left or right move */
            el_process_move_cursor_left_or_right(&es);
        } else if (VKEY_BACK == es.ch || 0x7F == es.ch) {
            /* destructive backspace */
            el_process_destructive_backspace(&es);
        } else if (isprint(es.ch)) {
            /* visible character */
            el_process_visible_char(&es);
        } else {
            beep();
        }
        move(es.efy, es.ecp - es.m_1 + es.x_1);
        wrefresh(stdscr);
        es.ch = getkey();
    }
    es.eb[es.ecn] = '\0';
    return es.ecn;
}

typedef
void
(*pathname_function) (char *        working_buffer,
                      size_t        working_buffsize,
                      const char *  default_name);

/* Get string. There is no formal check performed.
 * The buffer is assumed to represent a valid memory location,
 * violation asserted, but checked against the null pointer value.
 *
 * The returned filename might contain a prompt character, therefore
 * it should be first trimmed by an 'trim_string' function.
 */
static
int
get_string (char *                  buffer,
           size_t                   buffsize,
           const char *             edit_field_name,
           const char *             default_name,
           const char *             operation_name,
           char                     default_prompt,
           pathname_function        get_pathname)
{
    int i = 0;
    char * temp_str = NULL;
    int line;

    if (buffsize > 0) {
        temp_str = (char *)calloc(buffsize, sizeof(char));
    }
    assert (buffer != NULL && buffsize > 0);
    if (buffer != NULL && temp_str != NULL) {
        #if (LINE_SIZE - LEFT_MIDDLE + 1) < 60
        #error "Bad parameters, there isn't enough room for status display."
        #endif
        char abort_message[LINE_SIZE - LEFT_MIDDLE + 1];
        size_t  storage_size = sizeof(abort_message);
        const char * first_part = "Press Esc key twice to abort the '";
        const char * last_part  = "' operation.";

        strncpy(abort_message, first_part, storage_size);
        abort_message[sizeof(abort_message) - 1]= '\0';
        storage_size -= strlen(abort_message) + 1;
        strncat (abort_message, operation_name, storage_size);
        storage_size = sizeof(abort_message) - strlen(abort_message) - 1;
        strncat (abort_message, last_part, storage_size);

        strncpy (temp_str, buffer, buffsize);
        /* Just a safety measure. */
        temp_str[buffsize - 1]= '\0';

        if (get_pathname != NULL) {
            get_pathname (temp_str,
                          buffsize,
                          default_name);
        }

        if ('\0' == *temp_str) {
            size_t dns = ((default_name != NULL) ? strlen(default_name) : 0);
            temp_str[0] = default_prompt;
            if (0 < dns && dns < buffsize - 1) {
                strcpy(temp_str + 1, default_name);
            }
        }

        clear_status();
        mvaddstr(STATUS_LINE, LEFT_LEFT, edit_field_name);
        mvaddstr(STATUS_LINE, LEFT_MIDDLE, abort_message);

        i = edit_line(FILE_LINE,
                      LEFT_LEFT,
                      temp_str,
                      buffsize,
                      LINE_SIZE - LEFT_LEFT - 3);

        if (i > 0) {
            /* We have a nonzero length string, and the user
             * didn't abort by pressing the Esc key twice.
             * Let's copy the new filename to the output buffer. */
            strcpy (buffer, temp_str);
        } else {
            i = 0;
        }

        /* Reset screen */
        clear_status();
        for (line = FILE_LINE; line <= LAST_LINE; ++line) {
            /* Clear the filename and below from the seen. */
            move(line, LEFT_LEFT);
            wclrtoeol(stdscr);
        }
        move_to(curx, cury);
    }
    free(temp_str);
    return (i > 0) - 1;
}

/* Get title string. There is no formal check performed.
 * The buffer is assumed to represent a valid memory location,
 * violation asserted, but checked against the null pointer value.
 *
 * The returned filename might contain the '>' prompt therefore
 * it should be first trimmed by the 'trim_title' function.
 */
static
int
get_title (char *           buffer,
           size_t           buffsize,
           const char *     default_name,
           const char *     operation_name)
{
    return get_string (buffer,
                       buffsize,
                       "Title:",
                       default_name,
                       operation_name,
                       '%',
                       NULL);
}

static
void
get_filepath (char *            working_buffer,
              size_t            working_buffsize,
              const char *      default_name)
{
    char *          wbp = working_buffer;
    size_t          wbs = working_buffsize;
    const char *    dn  = default_name;
    /* Just for the sake of having shorter names. */
    assert (wbp != NULL && wbs > 0);
    if (wbp != NULL && wbs > 0) {

        if ('\0' == *wbp) {
            wbp[0] = '>';
            assert(dn != NULL && strlen(dn) < 6 + 3);
            if (0 == getcwd(wbp + 1,
                            (int)wbs - 1 - 1 - 6 - 3)) {
                /* The path is probably too long, use the filename alone. */
                strcpy(wbp + 1, dn);
            } else {
                if (dn != NULL) {
                    /* If the path would make it longer than the line length
                     * we can use just the filename. If the file name itself
                     * is too long, than we cannot avoid the wrap around, so
                     * it's better to be prepared for handling that.
                     */
                    if (strlen(wbp) + strlen(dn) > wbs) {
                        wbp[1] = '\0';
                    } else {
                        strcat(wbp, "/");
                    }
                    strcat(wbp, dn);
                }
            }
            wbp[wbs - 1] = '\0';
        }
    }
}

/* Get a file name. There is no formal check performed.
 * The buffer is assumed to represent a valid memory location,
 * violation asserted, but checked against the null pointer value.
 *
 * The returned filename might contain the '>' prompt therefore
 * it should be first trimmed by the 'trim_filename' function.
 */
static
int
get_filename (char *        buffer,
              size_t        buffsize,
              const char *  default_name,
              const char *  operation_name)
{
    return get_string(buffer,
                      buffsize,
                      "Filename:",
                      default_name,
                      operation_name,
                      '$',
                      get_filepath);
}

static
const char * 
trim_string (const char *   buffer,
             size_t         buffsize,
             const char *   prefixes)
{
    const char * p = buffer;
    if (p && buffsize > 0) {
        if (prefixes != NULL &&
            strchr(prefixes,*p) != NULL) {
            ++p;
            --buffsize;
        }
        while (buffsize > 0 && ' ' == *p) {
            ++p;
            --buffsize;
        }
    }
    if (buffsize == 0) {
        p = NULL;
    }
    return p;
}

static
const char * 
trim_titlename (const char *    buffer,
                size_t          buffsize)
{
    return trim_string(buffer, buffsize, "%");
}

static
const char * 
trim_filename (const char *     buffer,
               size_t           buffsize)
{
    return trim_string(buffer, buffsize, ">|");
}

static
void
save_board (void)
{
    const char * p;
    FILE * f;

    /* Read character input (raw processing mode) */

    if (0 == get_filename(userfile,
                          sizeof(userfile),
                          DEFAULT_BOARD_NAME,
                          "Save board")) {

        p = trim_filename(userfile,
                          sizeof(userfile));

        if (p != NULL && *p) {
            if ('|' == userfile[0]) {
                f = popen(p, "w");
                if (0 != f) {
                    print(f, title);
                    pclose(f);
                }
            } else {
                /* Append for standard formats, else separate file */
                switch (opt_format) {
                    case fCompact:
                    case fStandard:
                        f = fopen(p, "a");
                        break;
                    default:
                        f = fopen(p, "w");
                        break;
                }
                if (0 != f) {
                    print(f, title);
                    fclose(f);
                }
            }
            if (0 == f) {
                set_status("Error: failed to write the file!");
            }
        } else {
            set_status("Error: no valid file name found!");
        }
    } else {
        status_message("Save board operation aborted by the user.");
    }
}

/* Write default template to a user-given filename
 */
static
void
write_template (void)
{
    if (0 == get_filename(templatefile,
                            sizeof(templatefile),
                            TEMPLATE_FALLBACK,
                            "Write template")) {

            const char * p = trim_filename(templatefile,
                                            sizeof(templatefile));

        if (p != NULL && *p) {
            if (0 == write_default_template(p)) {
                status_message("Template file successfully created!");
            } else {
                beep_status_message("Template file exists"
                                    " or write error occurred!");
            }
        } else {
            beep_status_message("No valid file name found!");
        }
    } else {
        status_message("Write default template operation aborted by the user.");
    }
}

/* Open saved board(s)
 */
static
void
open_board (void)
{
    if (0 == get_filename(userfile,
                          sizeof(userfile),
                          DEFAULT_BOARD_NAME,
                          "Open board")) {

        const char * p = trim_filename(userfile,
                                       sizeof(userfile));

        if (p != NULL && *p) {
            if (opened != NULL) {
                fclose(opened);
                opened = NULL;
            }
            opened = fopen(p, "r");
            if (opened != NULL) {
                load_board();
            } else {
                set_status("Error: failed to open the board!");
            }
        } else {
            beep_status_message("No valid file name found!");
        }
    } else {
        status_message("Open board operation aborted by the user.");
    }
}

/* Rename the board title.
 * Returns a process exit code.
 */
static
void
rename_board_title (void)
{
    char new_title[80];
    new_title[0]='\0';
    if (0 == get_title(new_title,
                        sizeof(new_title),
                        title,
                        "Rename the board title")) {
        const char *    nt;
        size_t          ts = sizeof(new_title);
        if (ts > sizeof(title)) {
            ts = sizeof(title);
        }
        nt = trim_titlename(new_title, ts);
        if (nt != NULL && *nt != '\0') {
            strcpy(title, nt);
            write_title(title);
        }
        move_to(curx, cury);
    }
}

/* Generate statistics from boards in 'filename', and/or solve them.
 * Returns a process exit code.
 */
static
int
gen_statistics (void)
{
    int retval = 0;
    if (0 == precanned) {
        fprintf(stderr, "Error: no precanned boards loaded\n");
        retval = -1;
    } else {
        const char * classification;

        fseek (precanned, 0, SEEK_SET);
        while (0 == read_board(precanned, 0)) {
            /* Ignore insoluble boards */
            if (-1 == solve()) {
                printf("Board '%s' has no solution\n", title);
                continue;
            }

            /* If statistics only, ignore boards with multiple solutions */
            if (0 == opt_solve && -1 != backtrack() && 0 == solve()) {
                printf("Board '%s' has multiple solutions\n", title);
                continue;
            }

            classification = classify();
            if (0 == opt_solve) {
                printf("%2d %-12s : %s\n", pass, classification, title);
            } else {
                printf("Solution(s) to '%s' [%s]\n", title, classification);
                clear_moves();
                if (-1 != solve()) {
                    do {
                        print(stdout, title);
                        if (opt_describe) {
                            printf("Solution history:\n");
                            describe(stdout);
                        }
                    } while (-1 != backtrack() && -1 != solve());
                }
            }
        }
    }
    return retval;
}

/* cleanup curses */
static
void
cleanup_curses_and_more (void)
{
    if (opened != NULL) {
        fclose(opened);
        opened = NULL;
    }
    if (precanned != NULL) {
        fclose(precanned);
        precanned = NULL;
    }
    tclose(ftmplt);
    ftmplt = NULL;
    move(LAST_LINE, 0);
    wrefresh(stdscr);
    endwin();
}

/* Signal catchers - cleanup curses and terminate */
static
void
cleanup (int ignored)
{
    ignored = 1;
    cleanup_curses_and_more();
    exit(ignored);
}

/* Establish signal handlers */
static
void
signals (void)
{
    sigset_t sigset;
    struct sigaction sighandler;
    struct termios tp;

    sigemptyset(&sigset);
    sighandler.sa_handler = cleanup;
    sighandler.sa_mask = sigset;
    sighandler.sa_flags = 0;

    sigaction(SIGINT,  &sighandler, 0);
    sigaction(SIGABRT, &sighandler, 0);
    sigaction(SIGTERM, &sighandler, 0);
#ifdef SIGHUP
    sigaction(SIGHUP,  &sighandler, 0);
#endif
#ifdef SIGQUIT
    sigaction(SIGQUIT, &sighandler, 0);
#endif
#ifdef SIGKILL
    sigaction(SIGKILL, &sighandler, 0);
#endif

    /* Re-enable signal processing */
    if (0 == tcgetattr(0, &tp)) {
        tp.c_lflag |= ISIG;
        tcsetattr(0, TCSANOW, &tp);
    }
}

static
void
usage (void)
{
    fprintf(stderr,
            "Usage: %s [options] [<filename>]\n"
            "Supported options:\n"
            "    -c<class>    generate a board until it finds a board of the\n"
            "                 specified class. Supported classes are:\n"
            "                    %s, %s, %s, %s, and %s\n"
            "    -d           describe solution steps (with -v)\n"
            "    -f<format>   set output format; supported formats are:\n"
            "                    standard   (std)    <default format>\n"
            "                    compact\n"
            "                    csv                 [comma separated file]\n"
            "                    postscript (ps)\n"
            "                    html\n"
            "    -g[<num>]    generate <num> board(s), and print on stdout\n"
            "    -n           no random boards (requires precanned boards)\n"
            "    -r           restricted: don't allow boards to be saved\n"
            "    -s           calculate statistics for precanned boards\n"
            "    -t<filename> template file\n"
            "    -v           solve precanned boards\n"
            "    -w           write out the default template\n"
            "                 to the current directory\n"
            "    <filename>   'precanned' sudoku boards\n",
            program,
            CN(VERY_EASY),
            CN(EASY),
            CN(MEDIUM),
            CN(HARD),
            CN(FIENDISH));
}

void request_hint (void)
{
    ++req_hints;
    if (have_hint) {
        clear_hints();
    }
    if (-1 == num_hints) {
        last_hint = -1;
        num_hints = findhints();
    }
    if (0 == num_hints) {
        set_status("No hints available!");
    } else {
        int i, idx;
        int n = 0;

        if (1 < num_hints) {
            do {
                i = rand() % num_hints;
            } while (i == last_hint);
        } else {
            i = 0;
        }
        idx = GET_INDEX(possible[last_hint = i]);

        /* Count # possible ways of expressing hint */
        if (0 != (HINT_ROW & possible[i])) {
            ++n;
        }
        if (0 != (HINT_COLUMN & possible[i])) {
            ++n;
        }
        if (0 != (HINT_BLOCK & possible[i])) {
            ++n;
        }
        assert(0 < n);
        if (1 < n) {
            n = 1 + rand() % n;
        }
        if (0 != (HINT_ROW & possible[i])) {
            if (0 == --n) {
                row_hint(ROW(idx));
            }
        }
        if (0 != (HINT_COLUMN & possible[i])) {
            if (0 == --n) {
                column_hint(COLUMN(idx));
            }
        }
        if (0 != (HINT_BLOCK & possible[i])) {
            if (0 == --n) {
                block_hint(IDX_BLOCK(ROW(idx), COLUMN(idx)));
            }
        }
        if (opt_spoilerhint) {
            /* Useful for testing... */
            sprintf(statusline, "%d @ row %d, column %d",
                        GET_DIGIT(possible[i]),
                        ROW(idx)+1, COLUMN(idx)+1);
            set_status(statusline);
        } else if (10 < req_hints || 2 * num_hints < req_hints) {
            sprintf(statusline, "(try the digit %d)",
                        GET_DIGIT(possible[i]));
            set_status(statusline);
        }
    }
}

int
evaluate_options (int argc, char **argv)
{
    int ret_err = 0;
    if (1 < argc) {
        char * arg;
        while (0 < --argc) {
            arg = *++argv;
            if ('-' != *arg) {
                if (0 != precanned) {
                    fprintf(stderr, "Error: only 1 precanned file allowed\n");
                    ret_err = 1;
                    /* will exit */
                    break;
                }
                open_precanned(arg);
                strcpy(userfile, arg);  /* Save the filename */
                if (0 == precanned || 0 == n_precanned) {
                    fprintf(stderr, "Error: failed to open '%s'\n", arg);
                    ret_err = 1;
                    /* will exit */
                    break;
                }
            } else {
                while ('\0' != *++arg) {
                    switch (*arg) {
                        case 'c':
                            /* No need for strdup, since the underlying
                             * memory in the environment will not go away. */
                            requested_class = &arg[1];
                            if( 0 != strcmp(requested_class, CN(VERY_EASY)) &&
                                0 != strcmp(requested_class, CN(EASY))      &&
                                0 != strcmp(requested_class, CN(MEDIUM))    &&
                                0 != strcmp(requested_class, CN(HARD))      &&
                                0 != strcmp(requested_class, CN(FIENDISH))) {
                                fprintf(stderr,
                                        "Error:"
                                        " the class must be one of"
                                        " '%s', '%s', '%s', '%s', '%s'\n",
                                        CN(VERY_EASY),
                                        CN(EASY),
                                        CN(MEDIUM),
                                        CN(HARD),
                                        CN(FIENDISH));
                                ret_err = 1;
                                /* will exit */
                            }
                            /* dummy to force termination */
                            arg = "x";
                            break;
                        case 'd':
                            opt_describe = 1;
                            break;
                        case 'f':
                            if ('\0' == arg[1]) {
                                arg = *++argv;
                                --argc;
                            } else {
                                ++arg;
                            }
                            if (0 == strcmp("compact", arg)) {
                                opt_format = fCompact;
                            } else if (0 == strcmp("standard", arg)
                                    || 0 == strcmp("std", arg)) {
                                opt_format = fStandard;
                            } else if (0 == strcmp("csv", arg)) {
                                opt_format = fCSV;
                            } else if (0 == strcmp("postscript", arg) ||
                                       0 == strcmp("ps", arg)) {
                                    opt_format = fPostScript;
                            } else if (0 == strcmp("html", arg)) {
                                opt_format = fHTML;
                            } else {
                                fprintf(stderr,
                                        "Error: '%s' is an unknown format\n",
                                        arg);
                                ret_err = 1;
                                /* will exit */
                            }
                            /* dummy to force termination */
                            arg = "x";
                            break;

                        case 'g':
                            opt_generate = 1;
                            if (isdigit(arg[1])) {
                                num_generate = atoi(arg + 1);
                                /* dummy to force termination */
                                arg = "x";
                            } else if ('\0' == arg[1]
                                      && 0 != *(argv+1)
                                      && isdigit(**(argv+1))) {
                                num_generate = atoi(*++argv);
                                --argc;
                            }
                            break;
                        case 'h': opt_spoilerhint = 1; break;
                        case 'n': opt_random = 0; break;
                        case 'r': opt_restrict = 1; break;
                        case 's': opt_statistics = 1; break;
                        case 't':
                            if ('\0' == arg[1]) {
                                if (0 == *(argv+1)) {
                                    fprintf(stderr,
                                            "Error:"
                                            " expected argument after '-t'\n");
                                    ret_err = 1;
                                    /* will exit */
                                    /* dummy to force termination */
                                    arg = "x";
                                    break;
                                }
                                arg = *++argv;
                                open_template(arg);
                                --argc;
                            } else {
                                open_template(++arg);
                            }
                            if (0 == ftmplt) {
                                fprintf(stderr,
                                        "Error:"
                                        " failed to open template file '%s'\n",
                                        arg);
                                ret_err = 1;
                                /* will exit */
                            }
                            /* dummy to force termination */
                            arg = "x";
                            break;
                        case 'v':
                            opt_solve = 1;
                            break;
                        case 'w':
                            write_default_template(TEMPLATE_FALLBACK);
                            break;
                        default:
                            usage();
                            ret_err = 1;
                            /* will exit */
                            /* dummy to force termination */
                            arg = "x";
                            break;
                    }
                }
            }
            if (ret_err != 0) {
                /* will force the exit */
                break;
            }
        }
    }
    return ret_err;
}

static
void
play_the_game (void)
{
    int ch = ' ';
    while ('q' != ch) {
        wrefresh(stdscr);
        ch = getkey();

        if (have_status) {
            clear_status();
        }
 
       switch (ch) {
            case '.':
                ch = '0';
                /* Drop thru' to digit handler */
            case '0':
                /* clear location, or add digit to board */
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9': /* clear location, or add digit to board */
                if (!IS_FIXED(INDEX(cury, curx))) {
                    num_hints = -1;
                    fillx(INDEX(cury, curx), ch - '0');
                    addch('0' == ch ? '.' : ch);
                    move_to(curx, cury);
                    if (have_hint) {
                        clear_hints();
                    }
                    req_hints = 0;
                } else {
                    beep();
                }
                break;
            case ' ':  /* move to next */
                move_next();
                break;
            case 'h':  /* move left */
            case VKEY_LEFT:
                if (0 < curx) {
                    move_to(curx - 1, cury);
                } else {
                      move_to(8, cury);
                }
                break;
            case 'j': /* move down */
            case VKEY_DOWN:
                if (cury < 8) {
                    move_to(curx, cury + 1);
                } else {
                    move_to(curx, 0);
                }
                break;
            case 'k': /* move up */
            case VKEY_UP:
                if (0 < cury) {
                    move_to(curx, cury - 1);
                } else {
                    move_to(curx, 8);
                }
                break;
            case 'l': /* move right */
            case VKEY_RIGHT:
                if (curx < 8) {
                    move_to(curx + 1, cury);
                } else {
                    move_to(0, cury);
                }
                break;
            case 'u': /* undo last move */
                if (idx_history <= 0
                    || history[idx_history - 1] & FIXED) {
                    assert(idx_history >= 0);
                    beep();
                } else {
                    int i;
                    /* Don't ignore last reference (if exists) */
                    for (i = --idx_history - 1; 0 <= i; --i) {
                        if (GET_INDEX(history[i])
                            == GET_INDEX(history[idx_history])) {
                            history[i] &= ~IGNORED;
                            break;
                        }
                    }
                    num_hints = -1;
                    if (have_hint) {
                        clear_hints();
                    }
                    req_hints = 0;
                    reapply();
                    render();
                    if (history[idx_history - 1] & FIXED) {
                        curx = cury = 8;
                        move_next();
                    } else {
                        move_to (COLUMN(GET_INDEX(history[idx_history])),
                                 ROW(GET_INDEX(history[idx_history])));
                    }
                }
                break;
            case 'c': /* clear board */
                completed = 0;
                num_hints = -1;
                if (have_hint) {
                    clear_hints();
                }
                reset();
                render();
                write_title(0);
                move_to(0, 0);
                break;
            case 'd': /* refresh screen */
                wrefresh(stdscr);
                redrawwin(stdscr);
                break;
            case 'f': /* fix squares (if possible) */
                if (0 == idx_history || 0 != (history[0] & FIXED)) {
                    break;
                }
                if (0 != fix()) {
                    set_status("There is an error - no solution possible!");
                    beep();
                }
                break;
            case 'n': /* load new board */
                if (have_hint) {
                    clear_hints();
                }
                reset();
                render();
                write_title(0);
                wrefresh(stdscr);
                load_board();
                break;
            case 'v': /* show solution */
                clear_moves();
                if (have_hint) {
                    clear_hints();
                }
                num_hints = -1;
                if (0 == solve()) {
                    completed = 1;
                } else {
                    beep_status_message("This board has no solution!");
                }
                render();
                move_to(curx, cury);
                break;

            case 'r': /* restart the same board */
                clear_moves();
                render();
                curx = 8;
                cury = 8;
                /* move_next() takes care of this... */
                move_next();
                break;

            case 'w': /* write default template */
                if (0 == opt_restrict) {
                    write_template();
                }
                break;
            case 'o': /* open saved board */
                if (0 == opt_restrict) {
                    open_board();
                }
                break;

            /* The main difficulty with saving a board is managing the
             * user interface.
             */
            case 's':
                if (0 == opt_restrict) {
                    save_board();
                }
                break;

            case 't':
                if (0 == opt_restrict) {
                    rename_board_title();
                }
                break;

            case '?': /* Request hint */
                request_hint();
                break;
            case 'q': /* quit */
                break;
            default:
                beep();
        }

        /* Check for a solution */
        if (0 == completed) {
            if (is_complete() && 0 == fix()) {
                time_t end_time;
                time( &end_time );
                end_time -= start_time;
                sprintf(statusline, "Well done"
                                    " - you've completed the puzzle!"
                                    " (%02d:%02d:%02d)",
                                    (int) (end_time/60/60),
                                    (int) (end_time/60)%60,
                                    (int) (end_time%60));
                beep();
                set_status(statusline);
                completed = 1;
            }
        }
    }
}

int
main (int argc, char **argv)
{
    program = argv[0];

    /* Limited support for options */
    if (0 != evaluate_options(argc, argv)) {
        cleanup_curses_and_more();
        exit(1);
    }
    if (0 != opt_statistics && 0 != opt_generate) {
        fprintf(stderr, "Error: Cannot set both -g and -s options\n");
        cleanup_curses_and_more();
        return -1;
    }

    if (0 == precanned) {
        open_precanned(PRECANNED);

        /* Fallback - try current working directory */
        if (0 == precanned) {
            open_precanned(PRECANNED_FALLBACK);
        }
        /* Else, can continue happily without any precanned files... */
    }

    if (0 != opt_statistics || 0 != opt_solve) {
        int retval = gen_statistics();
        cleanup_curses_and_more();
        return retval;
    }

    srand((unsigned int)(time(0) ^ getpid()));

    if (0 == ftmplt) {
        open_template(TEMPLATE);

        /* Fallback - try current working directory */
        if (0 == ftmplt) {
            open_template(TEMPLATE_FALLBACK);
            if (0 == ftmplt) {
                fprintf(stderr, "Error: failed to open template file\n");
                exit(1);
            } else if (0 >= n_tmplt) {
                fprintf(stderr, "Error:"
                                " no valid template found"
                                " in the template file\n");
                cleanup_curses_and_more();
                exit(1);
            }
        }
    }

    if (0 != opt_generate) {
        /* -g0 generates many boards */
        if (0 == num_generate) {
            /* changed from --num_generate, to make it
             * independent from the actual size of int
             * 10000 seems to qualify for being 'many'
             */
            num_generate = 10000;
        }
        while (0 != num_generate--) {
            generate();
            print(stdout, title);
        }
        cleanup_curses_and_more();
        return 0;
    }

    if (0 == opt_random && 0 == precanned) {
        fprintf(stderr, "Error: option -n requires precanned boards\n");
        cleanup_curses_and_more();
        return 1;
    }

    if (!isatty(0) || !isatty(1)) {
        fprintf(stderr, "Error: stdin/out cannot be redirected\n");
        cleanup_curses_and_more();
        return 1;
    }

    /* Establish process environment */
    if (0 == initscr()) {
        fprintf(stderr,
                 "Error: failed to initialise curses screen library\n");
        cleanup_curses_and_more();
        return 1;
    }

    /* Any signal will now shutdown curses cleanly */
    signals();

    draw_screen();
    noecho();
    cbreak();
    load_board();
    play_the_game();
    cleanup_curses_and_more();
    return 0;
}