Full source code is available on github.

There are several major source files for the C++ version of QQWing:

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
/*!
 * qqwing - Sudoku solver and generator
 * Copyright (C) 2014 Stephen Ostermiller
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
var qqwing = function(){

/**
 * Given the index of a cell (0-80) calculate
 * the column (0-8) in which that cell resides.
 */
var cellToColumn = function(cell){
    return cell%qqwing.ROW_COL_SEC_SIZE;
};

/**
 * Given the index of a cell (0-80) calculate
 * the row (0-8) in which it resides.
 */
var cellToRow = function(cell){
    return Math.floor(cell/qqwing.ROW_COL_SEC_SIZE);
};

/**
 * Given the index of a cell (0-80) calculate
 * the cell (0-80) that is the upper left start
 * cell of that section.
 */
var cellToSectionStartCell = function(cell){
    return Math.floor(cell/qqwing.SEC_GROUP_SIZE)*qqwing.SEC_GROUP_SIZE
            + Math.floor(cellToColumn(cell)/qqwing.GRID_SIZE)*qqwing.GRID_SIZE;
};

/**
 * Given the index of a cell (0-80) calculate
 * the section (0-8) in which it resides.
 */
var cellToSection = function(cell){
    return Math.floor(cell/qqwing.SEC_GROUP_SIZE)*qqwing.GRID_SIZE
            + Math.floor(cellToColumn(cell)/qqwing.GRID_SIZE);
};

/**
 * Given a row (0-8) calculate the first cell (0-80)
 * of that row.
 */
var rowToFirstCell = function(row){
    return qqwing.ROW_COL_SEC_SIZE*row;
};

/**
 * Given a column (0-8) calculate the first cell (0-80)
 * of that column.
 */
var columnToFirstCell = function(column){
    return column;
};

/**
 * Given a section (0-8) calculate the first cell (0-80)
 * of that section.
 */
var sectionToFirstCell = function(section){
    return (section%qqwing.GRID_SIZE*qqwing.GRID_SIZE) + Math.floor(section/qqwing.GRID_SIZE)*qqwing.SEC_GROUP_SIZE;
};

/**
 * Given a value for a cell (0-8) and a cell (0-80)
 * calculate the offset into the possibility array (0-728).
 */
var getPossibilityIndex = function(valueIndex, cell){
    return valueIndex+(qqwing.ROW_COL_SEC_SIZE*cell);
};

/**
 * Given a row (0-8) and a column (0-8) calculate the
 * cell (0-80).
 */
var rowColumnToCell = function(row, column){
    return (row*qqwing.ROW_COL_SEC_SIZE)+column;
};

/**
 * Given a section (0-8) and an offset into that section (0-8)
 * calculate the cell (0-80)
 */
var sectionToCell = function(section, offset){
    return sectionToFirstCell(section)
            + Math.floor(offset/qqwing.GRID_SIZE)*qqwing.ROW_COL_SEC_SIZE
            + (offset%qqwing.GRID_SIZE);
};

var println = function(s){
    if ((typeof console != 'undefined') && console.log) console.log(s);
};

var printnoln = function(s){
    if ((typeof process != 'undefined') && process.stdout && process.stdout.write) process.stdout.write(s);
    else println(s);
};

/**
 * The 81 integers that make up a sudoku puzzle.
 * Givens are 1-9, unknowns are 0.
 * Once initialized, this puzzle remains as is.
 * The answer is worked out in "solution".
 */
var puzzle = new Array(qqwing.BOARD_SIZE);

/**
 * The 81 integers that make up a sudoku puzzle.
 * The solution is built here, after completion
 * all will be 1-9.
 */
var solution = new Array(qqwing.BOARD_SIZE);

/**
 * Recursion depth at which each of the numbers
 * in the solution were placed.  Useful for backing
 * out solve branches that don't lead to a solution.
 */
var solutionRound = new Array(qqwing.BOARD_SIZE);

/**
 * The 729 integers that make up a the possible
 * values for a Sudoku puzzle. (9 possibilities
 * for each of 81 squares).  If possibilities[i]
 * is zero, then the possibility could still be
 * filled in according to the Sudoku rules.  When
 * a possibility is eliminated, possibilities[i]
 * is assigned the round (level) at
 * which it was determined that it could not be
 * a possibility.
 */
var possibilities = new Array(qqwing.POSSIBILITY_SIZE);

/**
 * An array the size of the board (81) containing each
 * of the numbers 0-n exactly once.  This array may
 * be shuffled so that operations that need to
 * look at each cell can do so in a random order.
 */
var randomBoardArray = new Array(qqwing.BOARD_SIZE);

for (var i=0; i<qqwing.BOARD_SIZE; i++){
    randomBoardArray[i] = i;
}

/**
 * An array with one element for each position (9), in
 * some random order to be used when trying each
 * position in turn during guesses.
 */
var randomPossibilityArray = new Array(qqwing.ROW_COL_SEC_SIZE);

for (var i=0; i<qqwing.ROW_COL_SEC_SIZE; i++){
    randomPossibilityArray[i] = i;
}

/**
 * Whether or not to record history
 */
var recordHistory = false;

/**
 * Whether or not to print history as it happens
 */
var logHistory = false;

/**
 * A list of moves used to solve the puzzle.
 * This list contains all moves, on solve
 * branches that did not lead to a solution.
 */
var solveHistory = [];

/**
 * A list of moves used to solve the puzzle.
 * This list contains only the moves needed
 * to solve the puzzle, doesn't contain
 * information about bad guesses.
 */
var solveInstructions = [];

/**
 * The style with which to print puzzles and solutions
 */
var printStyle = qqwing.PrintStyle.READABLE;

/**
 * The last round of solving
 */
var lastSolveRound = 0;

/**
 * Reset the board to its initial state with
 * only the givens.
 * This method clears any solution, resets statistics,
 * and clears any history messages.
 */
var reset = function(){
    for (var i=0; i<qqwing.BOARD_SIZE; i++){
        solution[i] = 0;
    }
    for (var i=0; i<qqwing.BOARD_SIZE; i++){
        solutionRound[i] = 0;
    }
    for (var i=0; i<qqwing.POSSIBILITY_SIZE; i++){
        possibilities[i] = 0;
    }
    solveHistory = [];
    solveInstructions = [];

    var round = 1;
    for (var position=0; position<qqwing.BOARD_SIZE; position++){
        if (puzzle[position] > 0){
            var valIndex = puzzle[position]-1;
            var valPos = getPossibilityIndex(valIndex,position);
            var value = puzzle[position];
            if (possibilities[valPos] != 0) return false;
            mark.call(this,position,round,value);
            if (logHistory || recordHistory) addHistoryItem.call(this, new this.LogItem(round, qqwing.LogType.GIVEN, value, position));
        }
    }
    return true;
};

var singleSolveMove = function(round){
    if (onlyPossibilityForCell.call(this, round)) return true;
    if (onlyValueInSection.call(this, round)) return true;
    if (onlyValueInRow.call(this, round)) return true;
    if (onlyValueInColumn.call(this, round)) return true;
    if (handleNakedPairs.call(this, round)) return true;
    if (pointingRowReduction.call(this, round)) return true;
    if (pointingColumnReduction.call(this, round)) return true;
    if (rowBoxReduction.call(this, round)) return true;
    if (colBoxReduction.call(this, round)) return true;
    if (hiddenPairInRow.call(this, round)) return true;
    if (hiddenPairInColumn.call(this, round)) return true;
    if (hiddenPairInSection.call(this, round)) return true;
    return false;
};

/**
 * Mark exactly one cell that has a single possibility, if such a cell exists.
 * This method will look for a cell that has only one possibility.  This type
 * of cell is often called a "single"
 */
var onlyPossibilityForCell = function(round){
    for (var position=0; position<qqwing.BOARD_SIZE; position++){
        if (solution[position] == 0){
            var count = 0;
            var lastValue = 0;
            for (var valIndex=0; valIndex<qqwing.ROW_COL_SEC_SIZE; valIndex++){
                var valPos = getPossibilityIndex(valIndex,position);
                if (possibilities[valPos] == 0){
                    count++;
                    lastValue=valIndex+1;
                }
            }
            if (count == 1){
                mark.call(this, position, round, lastValue);
                if (logHistory || recordHistory) addHistoryItem.call(this, new this.LogItem(round, qqwing.LogType.SINGLE, lastValue, position));
                return true;
            }
        }
    }
    return false;
};

/**
 * Mark exactly one cell which is the only possible value for some row, if
 * such a cell exists.
 * This method will look in a row for a possibility that is only listed
 * for one cell.  This type of cell is often called a "hidden single"
 */
var onlyValueInRow = function(round){
    for (var row=0; row<qqwing.ROW_COL_SEC_SIZE; row++){
        for (var valIndex=0; valIndex<qqwing.ROW_COL_SEC_SIZE; valIndex++){
            var count = 0;
            var lastPosition = 0;
            for (var col=0; col<qqwing.ROW_COL_SEC_SIZE; col++){
                var position = (row*qqwing.ROW_COL_SEC_SIZE)+col;
                var valPos = getPossibilityIndex(valIndex,position);
                if (possibilities[valPos] == 0){
                    count++;
                    lastPosition = position;
                }
            }
            if (count == 1){
                var value = valIndex+1;
                if (logHistory || recordHistory) addHistoryItem.call(this, new this.LogItem(round, qqwing.LogType.HIDDEN_SINGLE_ROW, value, lastPosition));
                mark.call(this, lastPosition, round, value);
                return true;
            }
        }
    }
    return false;
}

/**
 * Mark exactly one cell which is the only possible value for some column, if
 * such a cell exists.
 * This method will look in a column for a possibility that is only listed
 * for one cell.  This type of cell is often called a "hidden single"
 */
var onlyValueInColumn = function(round){
    for (var col=0; col<qqwing.ROW_COL_SEC_SIZE; col++){
        for (var valIndex=0; valIndex<qqwing.ROW_COL_SEC_SIZE; valIndex++){
            var count = 0;
            var lastPosition = 0;
            for (var row=0; row<qqwing.ROW_COL_SEC_SIZE; row++){
                var position = rowColumnToCell(row,col);
                var valPos = getPossibilityIndex(valIndex,position);
                if (possibilities[valPos] == 0){
                    count++;
                    lastPosition = position;
                }
            }
            if (count == 1){
                var value = valIndex+1;
                if (logHistory || recordHistory) addHistoryItem.call(this, new this.LogItem(round, qqwing.LogType.HIDDEN_SINGLE_COLUMN, value, lastPosition));
                mark.call(this, lastPosition, round, value);
                return true;
            }
        }
    }
    return false;
}


/**
 * Mark exactly one cell which is the only possible value for some section, if
 * such a cell exists.
 * This method will look in a section for a possibility that is only listed
 * for one cell.  This type of cell is often called a "hidden single"
 */
var onlyValueInSection = function(round){
    for (var sec=0; sec<qqwing.ROW_COL_SEC_SIZE; sec++){
        var secPos = sectionToFirstCell(sec);
        for (var valIndex=0; valIndex<qqwing.ROW_COL_SEC_SIZE; valIndex++){
            var count = 0;
            var lastPosition = 0;
            for (var i=0; i<qqwing.GRID_SIZE; i++){
                for (var j=0; j<qqwing.GRID_SIZE; j++){
                    var position = secPos + i + qqwing.ROW_COL_SEC_SIZE*j;
                    var valPos = getPossibilityIndex(valIndex,position);
                    if (possibilities[valPos] == 0){
                        count++;
                        lastPosition = position;
                    }
                }
            }
            if (count == 1){
                var value = valIndex+1;
                if (logHistory || recordHistory) addHistoryItem.call(this, new this.LogItem(round, qqwing.LogType.HIDDEN_SINGLE_SECTION, value, lastPosition));
                mark.call(this, lastPosition, round, value);
                return true;
            }
        }
    }
    return false;
}

var guess = function(round, guessNumber){
    var localGuessCount = 0;
    var position = findPositionWithFewestPossibilities.call(this);
    for (var i=0; i<qqwing.ROW_COL_SEC_SIZE; i++){
        var valIndex = randomPossibilityArray[i];
        var valPos = getPossibilityIndex(valIndex,position);
        if (possibilities[valPos] == 0){
            if (localGuessCount == guessNumber){
                var value = valIndex+1;
                if (logHistory || recordHistory) addHistoryItem.call(this, new this.LogItem(round, qqwing.LogType.GUESS, value, position));
                mark.call(this, position, round, value);
                return true;
            }
            localGuessCount++;
        }
    }
    return false;
};

var isImpossible = function(){
    for (var position=0; position<qqwing.BOARD_SIZE; position++){
        if (solution[position] == 0){
            var count = 0;
            for (var valIndex=0; valIndex<qqwing.ROW_COL_SEC_SIZE; valIndex++){
                var valPos = getPossibilityIndex(valIndex,position);
                if (possibilities[valPos] == 0) count++;
            }
            if (count == 0) {
                return true;
            }
        }
    }
    return false;
};

var rollbackRound = function(round){
    if (logHistory || recordHistory) addHistoryItem.call(this, new this.LogItem(round, qqwing.LogType.ROLLBACK));
    for (var i=0; i<qqwing.BOARD_SIZE; i++){
        if (solutionRound[i] == round){
            solutionRound[i] = 0;
            solution[i] = 0;
        }
    }
    {for (var i=0; i<qqwing.POSSIBILITY_SIZE; i++){
        if (possibilities[i] == round){
            possibilities[i] = 0;
        }
    }}

    while(solveInstructions.length > 0 && solveInstructions[solveInstructions.length-1] == round){
        solveInstructions.pop();
    }
};

var pointingRowReduction = function(round){
    for (var valIndex=0; valIndex<qqwing.ROW_COL_SEC_SIZE; valIndex++){
        for (var section=0; section<qqwing.ROW_COL_SEC_SIZE; section++){
            var secStart = sectionToFirstCell(section);
            var inOneRow = true;
            var boxRow = -1;
            for (var j=0; j<qqwing.GRID_SIZE; j++){
                for (var i=0; i<qqwing.GRID_SIZE; i++){
                    var secVal=secStart+i+(qqwing.ROW_COL_SEC_SIZE*j);
                    var valPos = getPossibilityIndex(valIndex,secVal);
                    if(possibilities[valPos] == 0){
                        if (boxRow == -1 || boxRow == j){
                            boxRow = j;
                        } else {
                            inOneRow = false;
                        }
                    }
                }
            }
            if (inOneRow && boxRow != -1){
                var doneSomething = false;
                var row = cellToRow(secStart) + boxRow;
                var rowStart = rowToFirstCell(row);

                for (var i=0; i<qqwing.ROW_COL_SEC_SIZE; i++){
                    var position = rowStart+i;
                    var section2 = cellToSection(position);
                    var valPos = getPossibilityIndex(valIndex,position);
                    if (section != section2 && possibilities[valPos] == 0){
                        possibilities[valPos] = round;
                        doneSomething = true;
                    }
                }
                if (doneSomething){
                    if (logHistory || recordHistory) addHistoryItem.call(this, new this.LogItem(round, qqwing.LogType.POINTING_PAIR_TRIPLE_ROW, valIndex+1, rowStart));
                    return true;
                }
            }
        }
    }
    return false;
};

var rowBoxReduction = function(round){
    for (var valIndex=0; valIndex<qqwing.ROW_COL_SEC_SIZE; valIndex++){
        for (var row=0; row<qqwing.ROW_COL_SEC_SIZE; row++){
            var rowStart = rowToFirstCell(row);
            var inOneBox = true;
            var rowBox = -1;
            for (var i=0; i<qqwing.GRID_SIZE; i++){
                for (var j=0; j<qqwing.GRID_SIZE; j++){
                    var column = i*qqwing.GRID_SIZE+j;
                    var position = rowColumnToCell(row, column);
                    var valPos = getPossibilityIndex(valIndex,position);
                    if(possibilities[valPos] == 0){
                        if (rowBox == -1 || rowBox == i){
                            rowBox = i;
                        } else {
                            inOneBox = false;
                        }
                    }
                }
            }
            if (inOneBox && rowBox != -1){
                var doneSomething = false;
                var column = qqwing.GRID_SIZE*rowBox;
                var secStart = cellToSectionStartCell(rowColumnToCell(row, column));
                var secStartRow = cellToRow(secStart);
                var secStartCol = cellToColumn(secStart);
                for (var i=0; i<qqwing.GRID_SIZE; i++){
                    for (var j=0; j<qqwing.GRID_SIZE; j++){
                        var row2 = secStartRow+i;
                        var col2 = secStartCol+j;
                        var position = rowColumnToCell(row2, col2);
                        var valPos = getPossibilityIndex(valIndex,position);
                        if (row != row2 && possibilities[valPos] == 0){
                            possibilities[valPos] = round;
                            doneSomething = true;
                        }
                    }
                }
                if (doneSomething){
                    if (logHistory || recordHistory) addHistoryItem.call(this, new this.LogItem(round, qqwing.LogType.ROW_BOX, valIndex+1, rowStart));
                    return true;
                }
            }
        }
    }
    return false;
};

var colBoxReduction = function(round){
    for (var valIndex=0; valIndex<qqwing.ROW_COL_SEC_SIZE; valIndex++){
        for (var row=0; row<qqwing.ROW_COL_SEC_SIZE; row++){
            var rowStart = rowToFirstCell(row);
            var inOneBox = true;
            var rowBox = -1;
            for (var i=0; i<qqwing.GRID_SIZE; i++){
                for (var j=0; j<qqwing.GRID_SIZE; j++){
                    var column = i*qqwing.GRID_SIZE+j;
                    var position = rowColumnToCell(row, column);
                    var valPos = getPossibilityIndex(valIndex,position);
                    if(possibilities[valPos] == 0){
                        if (rowBox == -1 || rowBox == i){
                            rowBox = i;
                        } else {
                            inOneBox = false;
                        }
                    }
                }
            }
            if (inOneBox && rowBox != -1){
                var doneSomething = false;
                var column = qqwing.GRID_SIZE*rowBox;
                var secStart = cellToSectionStartCell(rowColumnToCell(row, column));
                var secStartRow = cellToRow(secStart);
                var secStartCol = cellToColumn(secStart);
                for (var i=0; i<qqwing.GRID_SIZE; i++){
                    for (var j=0; j<qqwing.GRID_SIZE; j++){
                        var row2 = secStartRow+i;
                        var col2 = secStartCol+j;
                        var position = rowColumnToCell(row2, col2);
                        var valPos = getPossibilityIndex(valIndex,position);
                        if (row != row2 && possibilities[valPos] == 0){
                            possibilities[valPos] = round;
                            doneSomething = true;
                        }
                    }
                }
                if (doneSomething){
                    if (logHistory || recordHistory) addHistoryItem.call(this, new this.LogItem(round, qqwing.LogType.ROW_BOX, valIndex+1, rowStart));
                    return true;
                }
            }
        }
    }
    return false;
};

var pointingColumnReduction = function(round){
    for (var valIndex=0; valIndex<qqwing.ROW_COL_SEC_SIZE; valIndex++){
        for (var section=0; section<qqwing.ROW_COL_SEC_SIZE; section++){
            var secStart = sectionToFirstCell(section);
            var inOneCol = true;
            var boxCol = -1;
            for (var i=0; i<qqwing.GRID_SIZE; i++){
                for (var j=0; j<qqwing.GRID_SIZE; j++){
                    var secVal=secStart+i+(qqwing.ROW_COL_SEC_SIZE*j);
                    var valPos = getPossibilityIndex(valIndex,secVal);
                    if(possibilities[valPos] == 0){
                        if (boxCol == -1 || boxCol == i){
                            boxCol = i;
                        } else {
                            inOneCol = false;
                        }
                    }
                }
            }
            if (inOneCol && boxCol != -1){
                var doneSomething = false;
                var col = cellToColumn(secStart) + boxCol;
                var colStart = columnToFirstCell(col);

                for (var i=0; i<qqwing.ROW_COL_SEC_SIZE; i++){
                    var position = colStart+(qqwing.ROW_COL_SEC_SIZE*i);
                    var section2 = cellToSection(position);
                    var valPos = getPossibilityIndex(valIndex,position);
                    if (section != section2 && possibilities[valPos] == 0){
                        possibilities[valPos] = round;
                        doneSomething = true;
                    }
                }
                if (doneSomething){
                    if (logHistory || recordHistory) addHistoryItem.call(this, new this.LogItem(round, qqwing.LogType.POINTING_PAIR_TRIPLE_COLUMN, valIndex+1, colStart));
                    return true;
                }
            }
        }
    }
    return false;
}

var hiddenPairInRow = function(round){
    for (var row=0; row<qqwing.ROW_COL_SEC_SIZE; row++){
        for (var valIndex=0; valIndex<qqwing.ROW_COL_SEC_SIZE; valIndex++){
            var c1 = -1;
            var c2 = -1;
            var valCount = 0;
            for (var column=0; column<qqwing.ROW_COL_SEC_SIZE; column++){
                var position = rowColumnToCell(row,column);
                var valPos = getPossibilityIndex(valIndex,position);
                if (possibilities[valPos] == 0){
                    if (c1 == -1 || c1 == column){
                        c1 = column;
                    } else if (c2 == -1 || c2 == column){
                        c2 = column;
                    }
                    valCount++;
                }
            }
            if (valCount==2){
                for (var valIndex2=valIndex+1; valIndex2<qqwing.ROW_COL_SEC_SIZE; valIndex2++){
                    var c3 = -1;
                    var c4 = -1;
                    var valCount2 = 0;
                    for (var column=0; column<qqwing.ROW_COL_SEC_SIZE; column++){
                        var position = rowColumnToCell(row,column);
                        var valPos = getPossibilityIndex(valIndex2,position);
                        if (possibilities[valPos] == 0){
                            if (c3 == -1 || c3 == column){
                                c3 = column;
                            } else if (c4 == -1 || c4 == column){
                                c4 = column;
                            }
                            valCount2++;
                        }
                    }
                    if (valCount2==2 && c1==c3 && c2==c4){
                        var doneSomething = false;
                        for (var valIndex3=0; valIndex3<qqwing.ROW_COL_SEC_SIZE; valIndex3++){
                            if (valIndex3 != valIndex && valIndex3 != valIndex2){
                                var position1 = rowColumnToCell(row,c1);
                                var position2 = rowColumnToCell(row,c2);
                                var valPos1 = getPossibilityIndex(valIndex3,position1);
                                var valPos2 = getPossibilityIndex(valIndex3,position2);
                                if (possibilities[valPos1] == 0){
                                    possibilities[valPos1] = round;
                                    doneSomething = true;
                                }
                                if (possibilities[valPos2] == 0){
                                    possibilities[valPos2] = round;
                                    doneSomething = true;
                                }
                            }
                        }
                        if (doneSomething){
                            if (logHistory || recordHistory) addHistoryItem.call(this, new this.LogItem(round, qqwing.LogType.HIDDEN_PAIR_ROW, valIndex+1, rowColumnToCell(row,c1)));
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
};

var hiddenPairInColumn = function(round){
    for (var column=0; column<qqwing.ROW_COL_SEC_SIZE; column++){
        for (var valIndex=0; valIndex<qqwing.ROW_COL_SEC_SIZE; valIndex++){
            var r1 = -1;
            var r2 = -1;
            var valCount = 0;
            for (var row=0; row<qqwing.ROW_COL_SEC_SIZE; row++){
                var position = rowColumnToCell(row,column);
                var valPos = getPossibilityIndex(valIndex,position);
                if (possibilities[valPos] == 0){
                    if (r1 == -1 || r1 == row){
                        r1 = row;
                    } else if (r2 == -1 || r2 == row){
                        r2 = row;
                    }
                    valCount++;
                }
            }
            if (valCount==2){
                for (var valIndex2=valIndex+1; valIndex2<qqwing.ROW_COL_SEC_SIZE; valIndex2++){
                    var r3 = -1;
                    var r4 = -1;
                    var valCount2 = 0;
                    for (var row=0; row<qqwing.ROW_COL_SEC_SIZE; row++){
                        var position = rowColumnToCell(row,column);
                        var valPos = getPossibilityIndex(valIndex2,position);
                        if (possibilities[valPos] == 0){
                            if (r3 == -1 || r3 == row){
                                r3 = row;
                            } else if (r4 == -1 || r4 == row){
                                r4 = row;
                            }
                            valCount2++;
                        }
                    }
                    if (valCount2==2 && r1==r3 && r2==r4){
                        var doneSomething = false;
                        for (var valIndex3=0; valIndex3<qqwing.ROW_COL_SEC_SIZE; valIndex3++){
                            if (valIndex3 != valIndex && valIndex3 != valIndex2){
                                var position1 = rowColumnToCell(r1,column);
                                var position2 = rowColumnToCell(r2,column);
                                var valPos1 = getPossibilityIndex(valIndex3,position1);
                                var valPos2 = getPossibilityIndex(valIndex3,position2);
                                if (possibilities[valPos1] == 0){
                                    possibilities[valPos1] = round;
                                    doneSomething = true;
                                }
                                if (possibilities[valPos2] == 0){
                                    possibilities[valPos2] = round;
                                    doneSomething = true;
                                }
                            }
                        }
                        if (doneSomething){
                            if (logHistory || recordHistory) addHistoryItem.call(this, new this.LogItem(round, qqwing.LogType.HIDDEN_PAIR_COLUMN, valIndex+1, rowColumnToCell(r1,column)));
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
};

var hiddenPairInSection = function(round){
    for (var section=0; section<qqwing.ROW_COL_SEC_SIZE; section++){
        for (var valIndex=0; valIndex<qqwing.ROW_COL_SEC_SIZE; valIndex++){
            var si1 = -1;
            var si2 = -1;
            var valCount = 0;
            for (var secInd=0; secInd<qqwing.ROW_COL_SEC_SIZE; secInd++){
                var position = sectionToCell(section,secInd);
                var valPos = getPossibilityIndex(valIndex,position);
                if (possibilities[valPos] == 0){
                    if (si1 == -1 || si1 == secInd){
                        si1 = secInd;
                    } else if (si2 == -1 || si2 == secInd){
                        si2 = secInd;
                    }
                    valCount++;
                }
            }
            if (valCount==2){
                for (var valIndex2=valIndex+1; valIndex2<qqwing.ROW_COL_SEC_SIZE; valIndex2++){
                    var si3 = -1;
                    var si4 = -1;
                    var valCount2 = 0;
                    for (var secInd=0; secInd<qqwing.ROW_COL_SEC_SIZE; secInd++){
                        var position = sectionToCell(section,secInd);
                        var valPos = getPossibilityIndex(valIndex2,position);
                        if (possibilities[valPos] == 0){
                            if (si3 == -1 || si3 == secInd){
                                si3 = secInd;
                            } else if (si4 == -1 || si4 == secInd){
                                si4 = secInd;
                            }
                            valCount2++;
                        }
                    }
                    if (valCount2==2 && si1==si3 && si2==si4){
                        var doneSomething = false;
                        for (var valIndex3=0; valIndex3<qqwing.ROW_COL_SEC_SIZE; valIndex3++){
                            if (valIndex3 != valIndex && valIndex3 != valIndex2){
                                var position1 = sectionToCell(section,si1);
                                var position2 = sectionToCell(section,si2);
                                var valPos1 = getPossibilityIndex(valIndex3,position1);
                                var valPos2 = getPossibilityIndex(valIndex3,position2);
                                if (possibilities[valPos1] == 0){
                                    possibilities[valPos1] = round;
                                    doneSomething = true;
                                }
                                if (possibilities[valPos2] == 0){
                                    possibilities[valPos2] = round;
                                    doneSomething = true;
                                }
                            }
                        }
                        if (doneSomething){
                            if (logHistory || recordHistory) addHistoryItem.call(this, new this.LogItem(round, qqwing.LogType.HIDDEN_PAIR_SECTION, valIndex+1, sectionToCell(section,si1)));
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
};

/**
 * Mark the given value at the given position.  Go through
 * the row, column, and section for the position and remove
 * the value from the possibilities.
 *
 * @param position Position into the board (0-80)
 * @param round Round to mark for rollback purposes
 * @param value The value to go in the square at the given position
 */
var mark = function(position, round, value){
    if (solution[position] != 0) throw ("Marking position that already has been marked.");
    if (solutionRound[position] !=0) throw ("Marking position that was marked another round.");
    var valIndex = value-1;
    solution[position] = value;

    var possInd = getPossibilityIndex(valIndex,position);
    if (possibilities[possInd] != 0) throw ("Marking impossible position.");

    // Take this value out of the possibilities for everything in the row
    solutionRound[position] = round;
    var rowStart = cellToRow(position)*qqwing.ROW_COL_SEC_SIZE;
    for (var col=0; col<qqwing.ROW_COL_SEC_SIZE; col++){
        var rowVal=rowStart+col;
        var valPos = getPossibilityIndex(valIndex,rowVal);
        if (possibilities[valPos] == 0){
            possibilities[valPos] = round;
        }
    }

    // Take this value out of the possibilities for everything in the column
    var colStart = cellToColumn(position);
    for (var i=0; i<qqwing.ROW_COL_SEC_SIZE; i++){
        var colVal=colStart+(qqwing.ROW_COL_SEC_SIZE*i);
        var valPos = getPossibilityIndex(valIndex,colVal);
        if (possibilities[valPos] == 0){
            possibilities[valPos] = round;
        }
    }

    // Take this value out of the possibilities for everything in section
    var secStart = cellToSectionStartCell(position);
    for (var i=0; i<qqwing.GRID_SIZE; i++){
        for (var j=0; j<qqwing.GRID_SIZE; j++){
            var secVal=secStart+i+(qqwing.ROW_COL_SEC_SIZE*j);
            var valPos = getPossibilityIndex(valIndex,secVal);
            if (possibilities[valPos] == 0){
                possibilities[valPos] = round;
            }
        }
    }

    //This position itself is determined, it should have possibilities.
    for (var valIndex=0; valIndex<qqwing.ROW_COL_SEC_SIZE; valIndex++){
        var valPos = getPossibilityIndex(valIndex,position);
        if (possibilities[valPos] == 0){
            possibilities[valPos] = round;
        }
    }
};

var findPositionWithFewestPossibilities = function(){
    var minPossibilities = 10;
    var bestPosition = 0;
    for (var i=0; i<qqwing.BOARD_SIZE; i++){
        var position = randomBoardArray[i];
        if (solution[position] == 0){
            var count = 0;
            for (var valIndex=0; valIndex<qqwing.ROW_COL_SEC_SIZE; valIndex++){
                var valPos = getPossibilityIndex(valIndex,position);
                if (possibilities[valPos] == 0) count++;
            }
            if (count < minPossibilities){
                minPossibilities = count;
                bestPosition = position;
            }
        }
    }
    return bestPosition;
};

var handleNakedPairs = function(round){
    for (var position=0; position<qqwing.BOARD_SIZE; position++){
        var possibilities = countPossibilities(position);
        if (possibilities == 2){
            var row = cellToRow(position);
            var column = cellToColumn(position);
            var section = cellToSectionStartCell(position);
            for (var position2=position; position2<qqwing.BOARD_SIZE; position2++){
                if (position != position2){
                    var possibilities2 = countPossibilities(position2);
                    if (possibilities2 == 2 && arePossibilitiesSame(position, position2)){
                        if (row == cellToRow(position2)){
                            var doneSomething = false;
                            for (var column2=0; column2<qqwing.ROW_COL_SEC_SIZE; column2++){
                                var position3 = rowColumnToCell(row,column2);
                                if (position3 != position && position3 != position2 && removePossibilitiesInOneFromTwo(position, position3, round)){
                                    doneSomething = true;
                                }
                            }
                            if (doneSomething){
                                if (logHistory || recordHistory) addHistoryItem.call(this, new this.LogItem(round, qqwing.LogType.NAKED_PAIR_ROW, 0, position));
                                return true;
                            }
                        }
                        if (column == cellToColumn(position2)){
                            var doneSomething = false;
                            for (var row2=0; row2<qqwing.ROW_COL_SEC_SIZE; row2++){
                                var position3 = rowColumnToCell(row2,column);
                                if (position3 != position && position3 != position2 && removePossibilitiesInOneFromTwo(position, position3, round)){
                                    doneSomething = true;
                                }
                            }
                            if (doneSomething){
                                if (logHistory || recordHistory) addHistoryItem.call(this, new this.LogItem(round, qqwing.LogType.NAKED_PAIR_COLUMN, 0, position));
                                return true;
                            }
                        }
                        if (section == cellToSectionStartCell(position2)){
                            var doneSomething = false;
                            var secStart = cellToSectionStartCell(position);
                            for (var i=0; i<qqwing.GRID_SIZE; i++){
                                for (var j=0; j<qqwing.GRID_SIZE; j++){
                                    var position3=secStart+i+(qqwing.ROW_COL_SEC_SIZE*j);
                                    if (position3 != position && position3 != position2 && removePossibilitiesInOneFromTwo(position, position3, round)){
                                        doneSomething = true;
                                    }
                                }
                            }
                            if (doneSomething){
                                if (logHistory || recordHistory) addHistoryItem.call(this, new this.LogItem(round, qqwing.LogType.NAKED_PAIR_SECTION, 0, position));
                                return true;
                            }
                        }
                    }
                }
            }
        }
    }
    return false;
};

var countPossibilities = function(position){
    var count = 0;
    for (var valIndex=0; valIndex<qqwing.ROW_COL_SEC_SIZE; valIndex++){
        var valPos = getPossibilityIndex(valIndex,position);
        if (possibilities[valPos] == 0) count++;
    }
    return count;
};

var arePossibilitiesSame = function(position1, position2){
    for (var valIndex=0; valIndex<qqwing.ROW_COL_SEC_SIZE; valIndex++){
        var valPos1 = getPossibilityIndex(valIndex,position1);
        var valPos2 = getPossibilityIndex(valIndex,position2);
        if ((possibilities[valPos1] == 0 || possibilities[valPos2] == 0) && (possibilities[valPos1] != 0 || possibilities[valPos2] != 0)){
                return false;
        }
    }
    return true;
};

var addHistoryItem = function(l){
    if (logHistory) l.print();
    if (recordHistory){
        solveHistory.push(l);
        solveInstructions.push(l);
    }
};

var shuffleRandomArrays = function(){
    shuffleArray(randomBoardArray, qqwing.BOARD_SIZE);
    shuffleArray(randomPossibilityArray, qqwing.ROW_COL_SEC_SIZE);
};

/**
 * print the given BOARD_SIZEd array of ints
 * as a sudoku puzzle.  Use print options from
 * member variables.
 */
var print = function(puz){
    printnoln(sudokuToString.call(this, puz));
};

var sudokuToString = function(puz){
    var s = "";
    for(var i=0; i<qqwing.BOARD_SIZE; i++){
        if (printStyle == qqwing.PrintStyle.READABLE){
            s += " ";
        }
        if (puz[i]==0){
            s += '.';
        } else {
            s += puz[i];
        }
        if (i == qqwing.BOARD_SIZE-1){
            if (printStyle == qqwing.PrintStyle.CSV){
                s += ",";
            } else {
                s += "\n";
            }
            if (printStyle == qqwing.PrintStyle.READABLE || printStyle == qqwing.PrintStyle.COMPACT){
                s += "\n";
            }
        } else if (i%qqwing.ROW_COL_SEC_SIZE==qqwing.ROW_COL_SEC_SIZE-1){
            if (printStyle == qqwing.PrintStyle.READABLE || printStyle == qqwing.PrintStyle.COMPACT){
                s += "\n";
            }
            if (i%qqwing.SEC_GROUP_SIZE==qqwing.SEC_GROUP_SIZE-1){
                if (printStyle == qqwing.PrintStyle.READABLE){
                    s += "-------|-------|-------\n";
                }
            }
        } else if (i%qqwing.GRID_SIZE==qqwing.GRID_SIZE-1){
            if (printStyle == qqwing.PrintStyle.READABLE){
                s += " |";
            }
        }
    }
    return s;
};

var rollbackNonGuesses = function(){
    // Guesses are odd rounds
    // Non-guesses are even rounds
    for (var i=2; i<=lastSolveRound; i+=2){
        rollbackRound.call(this, i);
    }
};

var clearPuzzle = function(){
    // Clear any existing puzzle
    for (var i=0; i<qqwing.BOARD_SIZE; i++){
        puzzle[i] = 0;
    }
    reset.call(this);
};

var printHistory = function(v){
    printnoln(getHistoryString(v));
};

var getHistoryString = function(v){
    var s = "";
    if (!recordHistory){
        s += "History was not recorded.";
        if (printStyle == qqwing.PrintStyle.CSV){
            s += " -- ";
        } else {
            s += "\n";
        }
    }
    for (var i=0;i<v.length;i++){
        s += i+1 + ". " + v[i].toString();
        if (printStyle == qqwing.PrintStyle.CSV){
            s += " -- ";
        } else {
            s += "\n";
        }
    }
    if (printStyle == qqwing.PrintStyle.CSV){
        s += ",";
    } else {
        s += "\n";
    }
    return s;
};

var removePossibilitiesInOneFromTwo = function(position1, position2, round){
    var doneSomething = false;
    for (var valIndex=0; valIndex<qqwing.ROW_COL_SEC_SIZE; valIndex++){
        var valPos1 = getPossibilityIndex(valIndex,position1);
        var valPos2 = getPossibilityIndex(valIndex,position2);
        if (possibilities[valPos1] == 0 && possibilities[valPos2] == 0){
            possibilities[valPos2] = round;
            doneSomething = true;
        }
    }
    return doneSomething;
};

/**
 * Shuffle the values in an array of integers.
 */
var shuffleArray = function(array, size){
    for (var i=0; i<size; i++){
        var tailSize = size-i;
        var randTailPos = Math.floor(Math.random() * tailSize) + i;
        var temp = array[i];
        array[i] = array[randTailPos];
        array[randTailPos] = temp;
    }
};

var getRandomSymmetry = function(){
    var rand = Math.floor(Math.random() * 4)
    switch (rand){
        case 0: return qqwing.Symmetry.ROTATE90;
        case 1: return qqwing.Symmetry.ROTATE180;
        case 2: return qqwing.Symmetry.MIRROR;
        case 3: return qqwing.Symmetry.FLIP;
    }
    throw ("Unexpected random value: " + rand);
};

var getLogCount = function(v, type){
    var count = 0;
    for (var i=0; i<v.length; i++){
        if((v[i]).getType() == type) count++;
    }
    return count;
};
this.LogItem = function(r, t, v, p){
    /**
     * The recursion level at which this item was gathered.
     * Used for backing out log items solve branches that
     * don't lead to a solution.
     */
    var round = r;

    /**
     * The type of log message that will determine the
     * message printed.
     */
    var type = t;

    /**
     * Value that was set by the operation (or zero for no value)
     */
    var value = v;

    /**
     * position on the board at which the value (if any) was set.
     */
    var position = p;

    this.getRound = function (){
        return round;
    };

    this.print = function(){
        println(this.toString());
    };

    this.getType = function(){
        return type;
    };

    this.getColumn = function(){
        if (position == -1) return -1;
        return cellToColumn(position);
    };

    this.getRow = function(){
        if (position == -1) return -1;
        return cellToRow(position);
    }

    this.getPosition = function(){
        return position;
    }

    this.getValue = function(){
        return value;
    }

    this.getDescription = function(){
        switch(this.getType()){
            case qqwing.LogType.GIVEN: return "Mark given";
            case qqwing.LogType.ROLLBACK: return "Roll back round";
            case qqwing.LogType.GUESS: return "Mark guess (start round)";
            case qqwing.LogType.HIDDEN_SINGLE_ROW: return "Mark single possibility for value in row";
            case qqwing.LogType.HIDDEN_SINGLE_COLUMN: return "Mark single possibility for value in column";
            case qqwing.LogType.HIDDEN_SINGLE_SECTION: return "Mark single possibility for value in section";
            case qqwing.LogType.SINGLE: return "Mark only possibility for cell";
            case qqwing.LogType.NAKED_PAIR_ROW: return "Remove possibilities for naked pair in row";
            case qqwing.LogType.NAKED_PAIR_COLUMN: return "Remove possibilities for naked pair in column";
            case qqwing.LogType.NAKED_PAIR_SECTION: return "Remove possibilities for naked pair in section";
            case qqwing.LogType.POINTING_PAIR_TRIPLE_ROW: return "Remove possibilities for row because all values are in one section";
            case qqwing.LogType.POINTING_PAIR_TRIPLE_COLUMN: return "Remove possibilities for column because all values are in one section";
            case qqwing.LogType.ROW_BOX: return "Remove possibilities for section because all values are in one row";
            case qqwing.LogType.COLUMN_BOX: return "Remove possibilities for section because all values are in one column";
            case qqwing.LogType.HIDDEN_PAIR_ROW: return "Remove possibilities from hidden pair in row";
            case qqwing.LogType.HIDDEN_PAIR_COLUMN: return "Remove possibilities from hidden pair in column";
            case qqwing.LogType.HIDDEN_PAIR_SECTION: return "Remove possibilities from hidden pair in section";
            default: return "!!! Performed unknown optimization !!!";
        }
    }

    this.toString = function(){
        var s = "Round: " + this.getRound() + " - ";
        s += this.getDescription();
        if (value > 0 || position > -1){
            s += " (";
            var printed = false;
            if (position > -1){
                if (printed) s += " - ";
                s += "Row: " + (cellToRow(position)+1) + " - Column: " + (cellToColumn(position)+1);
                printed = true;
            }
            if (value > 0){
                if (printed) s += " - ";
                s += "Value: " + value;
                printed = true;
            }
            s += ")";
        }
        return s;
    }
};

/**
 * Set the board to the given puzzle.
 * The given puzzle must be an array of 81 integers.
 */
this.setPuzzle = function(initPuzzle){
    for (var i=0; i<qqwing.BOARD_SIZE; i++){
        puzzle[i] = initPuzzle[i];
    }
    return reset.call(this);
}

/**
 * Print the sudoku puzzle.
 */
this.printPuzzle = function(){
    return print.call(this, puzzle);
}

/**
 * Get the sudoku puzzle as a String.
 */
this.getPuzzleString = function(){
    return sudokuToString.call(this, puzzle);
}

/**
 * Print the sudoku solution.
 */
this.printSolution = function(){
    return print.call(this, solution);
}

/**
 * Get the sudoku puzzle as a String.
 */
this.getSolutionString = function(){
    return sudokuToString.call(this, solution);
}

this.solve = function(round){
    if (!round || round <= 1){
        reset.call(this);
        shuffleRandomArrays();
        return this.solve(2);
    }

    lastSolveRound = round;

    while (singleSolveMove.call(this, round)){
        if (this.isSolved()) return true;
        if (isImpossible.call(this)) return false;
    }

    var nextGuessRound = round+1;
    var nextRound = round+2;
    for (var guessNumber=0; guess.call(this, nextGuessRound, guessNumber); guessNumber++){
        if (isImpossible.call(this) || !this.solve(nextRound)){
            rollbackRound.call(this, nextRound);
            rollbackRound.call(this, nextGuessRound);
        } else {
            return true;
        }
    }
    return false;
};


this.countSolutions = function(round, limitToTwo){
    if (!round || round <= 1){
        // Don't record history while generating.
        var recHistory = recordHistory;
        this.setRecordHistory(false);
        var lHistory = logHistory;
        this.setLogHistory(false);

        reset.call(this);
        var solutionCount = this.countSolutions(2, false);

        // Restore recording history.
        this.setRecordHistory(recHistory);
        this.setLogHistory(lHistory);

        return solutionCount;
    } else {
        while (singleSolveMove.call(this, round)){
            if (this.isSolved()){
                rollbackRound.call(this, round);
                return 1;
            }
            if (isImpossible.call(this)){
                rollbackRound.call(this, round);
                return 0;
            }
        }

        var solutions = 0;
        var nextRound = round+1;
        for (var guessNumber=0; guess.call(this, nextRound, guessNumber); guessNumber++){
            solutions += this.countSolutions(nextRound, limitToTwo);
            if (limitToTwo && solutions >=2){
                rollbackRound.call(this, round);
                return solutions;
            }
        }
        rollbackRound.call(this, round);
        return solutions;
    }
};

this.isSolved = function(){
    for (var i=0; i<qqwing.BOARD_SIZE; i++){
        if (solution[i] == 0){
            return false;
        }
    }
    return true;
};

this.getSolveHistory = function(){
    if (this.isSolved()){
        return solveHistory;
    } else {
        return "No solve history - Puzzle is not possible to solve.";
    }
};

this.getSolveHistoryString = function(){
    if (this.isSolved()){
        return getHistoryString.call(this, solveHistory);
    } else {
        return "No solve history - Puzzle is not possible to solve.";
    }
};

this.printSolveHistory = function(){
    if (this.isSolved()){
        printHistory(solveHistory);
    } else {
        println("No solve history - Puzzle is not possible to solve.");
    }
};

this.setRecordHistory = function(recHistory){
    recordHistory = recHistory;
};

this.setLogHistory = function(logHist){
    logHistory = logHist;
};

this.setPrintStyle = function(ps){
    printStyle = ps;
};

this.generatePuzzle = function(){
    return this.generatePuzzleSymmetry(qqwing.Symmetry.NONE);
};

this.generatePuzzleSymmetry = function(symmetry){
        if (symmetry == qqwing.Symmetry.RANDOM) symmetry = getRandomSymmetry.call(this);

        // Don't record history while generating.
        var recHistory = recordHistory;
        this.setRecordHistory(false);
        var lHistory = logHistory;
        this.setLogHistory(false);

        clearPuzzle.call(this);

        // Start by getting the randomness in order so that
        // each puzzle will be different from the last.
        shuffleRandomArrays.call(this);

        // Now solve the puzzle the whole way.  The solve
        // uses random algorithms, so we should have a
        // really randomly totally filled sudoku
        // Even when starting from an empty grid
        this.solve();

        if (symmetry == qqwing.Symmetry.NONE){
            // Rollback any square for which it is obvious that
            // the square doesn't contribute to a unique solution
            // (ie, squares that were filled by logic rather
            // than by guess)
            rollbackNonGuesses.call(this);
        }

        // Record all marked squares as the puzzle so
        // that we can call countSolutions without losing it.
        for (var i=0; i<qqwing.BOARD_SIZE; i++){
            puzzle[i] = solution[i];
        }

        // Rerandomize everything so that we test squares
        // in a different order than they were added.
        shuffleRandomArrays.call(this);

        // Remove one value at a time and see if
        // the puzzle still has only one solution.
        // If it does, leave it out the point because
        // it is not needed.
        for (var i=0; i<qqwing.BOARD_SIZE; i++){
            // check all the positions, but in shuffled order
            var position = randomBoardArray[i];
            if (puzzle[position] > 0){
                var positionsym1 = -1;
                var positionsym2 = -1;
                var positionsym3 = -1;
                switch (symmetry){
                    case qqwing.Symmetry.ROTATE90:
                        positionsym2 = rowColumnToCell(qqwing.ROW_COL_SEC_SIZE-1-cellToColumn(position),cellToRow(position));
                        positionsym3 = rowColumnToCell(cellToColumn(position),qqwing.ROW_COL_SEC_SIZE-1-cellToRow(position));
                    case qqwing.Symmetry.ROTATE180:
                        positionsym1 = rowColumnToCell(qqwing.ROW_COL_SEC_SIZE-1-cellToRow(position),qqwing.ROW_COL_SEC_SIZE-1-cellToColumn(position));
                    break;
                    case qqwing.Symmetry.MIRROR:
                        positionsym1 = rowColumnToCell(cellToRow(position),qqwing.ROW_COL_SEC_SIZE-1-cellToColumn(position));
                    break;
                    case qqwing.Symmetry.FLIP:
                        positionsym1 = rowColumnToCell(qqwing.ROW_COL_SEC_SIZE-1-cellToRow(position),cellToColumn(position));
                    break;
                }
                // try backing out the value and
                // counting solutions to the puzzle
                var savedValue = puzzle[position];
                puzzle[position] = 0;
                var savedSym1 = 0;
                if (positionsym1 >= 0){
                    savedSym1 = puzzle[positionsym1];
                    puzzle[positionsym1] = 0;
                }
                var savedSym2 = 0;
                if (positionsym2 >= 0){
                    savedSym2 = puzzle[positionsym2];
                    puzzle[positionsym2] = 0;
                }
                var savedSym3 = 0;
                if (positionsym3 >= 0){
                    savedSym3 = puzzle[positionsym3];
                    puzzle[positionsym3] = 0;
                }
                reset.call(this);
                if (this.countSolutions(2, true) > 1){
                    // Put it back in, it is needed
                    puzzle[position] = savedValue;
                    if (positionsym1 >= 0 && savedSym1 != 0) puzzle[positionsym1] = savedSym1;
                    if (positionsym2 >= 0 && savedSym2 != 0) puzzle[positionsym2] = savedSym2;
                    if (positionsym3 >= 0 && savedSym3 != 0) puzzle[positionsym3] = savedSym3;
                }
            }
        }

        // Clear all solution info, leaving just the puzzle.
        reset.call(this);

        // Restore recording history.
        this.setRecordHistory(recHistory);
        this.setLogHistory(lHistory);

        return true;
};

/**
 * Get the number of cells that are
 * set in the puzzle (as opposed to
 * figured out in the solution
 */
this.getGivenCount = function(){
    var count = 0;
    for (var i=0; i<qqwing.BOARD_SIZE; i++){
        if (puzzle[i] != 0) count++;
    }
    return count;
};

/**
 * Get the number of cells for which the solution was determined
 * because there was only one possible value for that cell.
 */
this.getSingleCount = function(){
    return getLogCount.call(this, solveInstructions, qqwing.LogType.SINGLE);
}

/**
 * Get the number of cells for which the solution was determined
 * because that cell had the only possibility for some value in
 * the row, column, or section.
 */
this.getHiddenSingleCount = function(){
    return (
        getLogCount.call(this, solveInstructions, qqwing.LogType.HIDDEN_SINGLE_ROW) +
        getLogCount.call(this, solveInstructions, qqwing.LogType.HIDDEN_SINGLE_COLUMN) +
        getLogCount.call(this, solveInstructions, qqwing.LogType.HIDDEN_SINGLE_SECTION)
    );
};

/**
 * Get the number of naked pair reductions that were performed
 * in solving this puzzle.
 */

this.getNakedPairCount = function(){
    return (
        getLogCount.call(this, solveInstructions, qqwing.LogType.NAKED_PAIR_ROW) +
        getLogCount.call(this, solveInstructions, qqwing.LogType.NAKED_PAIR_COLUMN) +
        getLogCount.call(this, solveInstructions, qqwing.LogType.NAKED_PAIR_SECTION)
    );
};

/**
 * Get the number of hidden pair reductions that were performed
 * in solving this puzzle.
 */
this.getHiddenPairCount = function(){
    return (
        getLogCount.call(this, solveInstructions, qqwing.LogType.HIDDEN_PAIR_ROW) +
        getLogCount.call(this, solveInstructions, qqwing.LogType.HIDDEN_PAIR_COLUMN) +
        getLogCount.call(this, solveInstructions, qqwing.LogType.HIDDEN_PAIR_SECTION)
    );
};

/**
 * Get the number of box/line reductions that were performed
 * in solving this puzzle.
 */
this.getBoxLineReductionCount = function(){
    return (
        getLogCount.call(this, solveInstructions, qqwing.LogType.ROW_BOX)+
        getLogCount.call(this, solveInstructions, qqwing.LogType.COLUMN_BOX)
    );
};

/**
 * Get the number of pointing pair/triple reductions that were performed
 * in solving this puzzle.
 */

this.getPointingPairTripleCount = function(){
    return (
        getLogCount.call(this, solveInstructions, qqwing.LogType.POINTING_PAIR_TRIPLE_ROW)+
        getLogCount.call(this, solveInstructions, qqwing.LogType.POINTING_PAIR_TRIPLE_COLUMN)
    );
};

/**
 * Get the number lucky guesses in solving this puzzle.
 */
this.getGuessCount = function(){
    return getLogCount.call(this, solveInstructions, qqwing.LogType.GUESS);
};

/**
 * Get the number of backtracks (unlucky guesses) required
 * when solving this puzzle.
 */
this.getBacktrackCount = function(){
    return getLogCount.call(this, solveHistory, qqwing.LogType.ROLLBACK);
};

this.getSolveInstructions = function(){
    if (this.isSolved()){
        return solveInstructions;
    } else {
        return "No solve instructions - Puzzle is not possible to solve.";
    }
};

this.getSolveInstructionsString = function(){
    if (this.isSolved()){
        return getHistoryString.call(this, solveInstructions);
    } else {
        return "No solve instructions - Puzzle is not possible to solve.";
    }
};

this.printSolveInstructions = function(){
    if (this.isSolved()){
        printHistory(solveInstructions);
    } else {
        println("No solve instructions - Puzzle is not possible to solve.");
    }
};

this.getDifficulty = function(){
    if (this.getGuessCount() > 0) return qqwing.Difficulty.EXPERT;
    if (this.getBoxLineReductionCount() > 0) return qqwing.Difficulty.INTERMEDIATE;
    if (this.getPointingPairTripleCount() > 0) return qqwing.Difficulty.INTERMEDIATE;
    if (this.getHiddenPairCount() > 0) return qqwing.Difficulty.INTERMEDIATE;
    if (this.getNakedPairCount() > 0) return qqwing.Difficulty.INTERMEDIATE;
    if (this.getHiddenSingleCount() > 0) return qqwing.Difficulty.EASY;
    if (this.getSingleCount() > 0) return qqwing.Difficulty.SIMPLE;
    return qqwing.Difficulty.UNKNOWN;
};

this.getDifficultyAsString = function(){
    var difficulty = this.getDifficulty();
    switch (difficulty){
        case qqwing.Difficulty.EXPERT: return "Expert";
        case qqwing.Difficulty.INTERMEDIATE: return "Intermediate";
        case qqwing.Difficulty.EASY: return "Easy";
        case qqwing.Difficulty.SIMPLE: return "Simple";
        default: return "Unknown";
    }
};
};
qqwing.PrintStyle = {
    ONE_LINE: 0,
    COMPACT: 1,
    READABLE: 2,
    CSV: 3
};

qqwing.Difficulty = {
    UNKNOWN: 0,
    SIMPLE: 1,
    EASY: 2,
    INTERMEDIATE: 3,
    EXPERT: 4
};

qqwing.Symmetry = {
    NONE: 0,
    ROTATE90: 1,
    ROTATE180: 2,
    MIRROR: 3,
    FLIP: 4,
    RANDOM: 5
};

qqwing.LogType = {
    GIVEN: 0,
    SINGLE: 1,
    HIDDEN_SINGLE_ROW: 2,
    HIDDEN_SINGLE_COLUMN: 3,
    HIDDEN_SINGLE_SECTION: 4,
    GUESS: 5,
    ROLLBACK: 6,
    NAKED_PAIR_ROW: 7,
    NAKED_PAIR_COLUMN: 8,
    NAKED_PAIR_SECTION: 9,
    POINTING_PAIR_TRIPLE_ROW: 10,
    POINTING_PAIR_TRIPLE_COLUMN: 11,
    ROW_BOX: 12,
    COLUMN_BOX: 13,
    HIDDEN_PAIR_ROW: 14,
    HIDDEN_PAIR_COLUMN: 15,
    HIDDEN_PAIR_SECTION: 16
};

qqwing.GRID_SIZE = 3;
qqwing.ROW_COL_SEC_SIZE = qqwing.GRID_SIZE*qqwing.GRID_SIZE;
qqwing.SEC_GROUP_SIZE = qqwing.ROW_COL_SEC_SIZE*qqwing.GRID_SIZE;
qqwing.BOARD_SIZE = qqwing.ROW_COL_SEC_SIZE*qqwing.ROW_COL_SEC_SIZE;
qqwing.POSSIBILITY_SIZE = qqwing.BOARD_SIZE*qqwing.ROW_COL_SEC_SIZE;
try {
    // Start time for the application for timing
    var applicationStartTime = getMicroseconds();

    // The number of puzzles solved or generated.
    var puzzleCount = 0;

    // defaults for options
    var printPuzzle = false;
    var printSolution = false;
    var printHistory = false;
    var printInstructions = false;
    var timer = false;
    var countSolutions = false;
    var action = "NONE";
    var logHistory = false;
    var printStyle = qqwing.PrintStyle.READABLE;
    var numberToGenerate = 1;
    var printStats = false;
    var difficulty = qqwing.Difficulty.UNKNOWN;
    var symmetry = qqwing.Symmetry.NONE;
    var argv = process.argv;

    // Read the arguments and set the options
    for (var i=2; i<argv.length; i++){
        if (argv[i] == "--puzzle"){
            printPuzzle = true;
        } else if (argv[i] == "--nopuzzle"){
            printPuzzle = false;
        } else if (argv[i] == "--solution"){
            printSolution = true;
        } else if (argv[i] == "--nosolution"){
            printSolution = false;
        } else if (argv[i] == "--history"){
            printHistory = true;
        } else if (argv[i] == "--nohistory"){
            printHistory = false;
        } else if (argv[i] == "--instructions"){
            printInstructions = true;
        } else if (argv[i] == "--noinstructions"){
            printInstructions = false;
        } else if (argv[i] == "--stats"){
            printStats = true;
        } else if (argv[i] == "--nostats"){
            printStats = false;
        } else if (argv[i] == "--timer"){
            timer = true;
        } else if (argv[i] == "--notimer"){
            timer = false;
        } else if (argv[i] == "--count-solutions"){
            countSolutions = true;
        } else if (argv[i] == "--nocount-solutions"){
            countSolutions = false;
        } else if (argv[i] == "--generate"){
            action = "GENERATE";
            printPuzzle = true;
            if (i+1 < argv.length && argv[i+1].charAt(0) != "-"){
                numberToGenerate = parseInt(argv[i+1]);
                if (isNaN(numberToGenerate) || numberToGenerate <= 0){
                    console.log("Bad number of puzzles to generate: "+argv[i+1]);
                    process.exit(1);
                }
                i++;
            }
        } else if (argv[i] == "--difficulty") {
            if (argv.length <= i+1){
                console.log("Please specify a difficulty.");
                process.exit(1);
            } else if (argv[i+1].toLowerCase() == "simple"){
                difficulty = qqwing.Difficulty.SIMPLE;
            } else if (argv[i+1].toLowerCase() == "easy"){
                difficulty = qqwing.Difficulty.EASY;
            } else if (argv[i+1].toLowerCase() == "intermediate"){
                difficulty = qqwing.Difficulty.INTERMEDIATE;
            } else if (argv[i+1].toLowerCase() == "expert"){
                difficulty = qqwing.Difficulty.EXPERT;
            } else if (argv[i+1].toLowerCase() == "any"){
                difficulty = qqwing.Difficulty.UNKNOWN;
            } else {
                console.log("Difficulty expected to be simple, easy, intermediate, expert, or any, not "+argv[i+1]);
                process.exit(1);
            }
            i++;
        } else if (argv[i] == "--symmetry") {
            if (argv.length <= i+1){
                console.log("Please specify a symmetry.");
                process.exit(1);
            } else if (argv[i+1] == "none") {
                symmetry = qqwing.Symmetry.NONE;
            } else if (argv[i+1] == "rotate90") {
                symmetry = qqwing.Symmetry.ROTATE90;
            } else if (argv[i+1] == "rotate180") {
                symmetry = qqwing.Symmetry.ROTATE180;
            } else if (argv[i+1] == "mirror") {
                symmetry = qqwing.Symmetry.MIRROR;
            } else if (argv[i+1] == "flip") {
                symmetry = qqwing.Symmetry.FLIP;
            } else if (argv[i+1] == "random") {
                symmetry = qqwing.Symmetry.RANDOM;
            } else {
                console.log("Symmetry expected to be none, rotate90, rotate180, mirror, flip, or random, not " + argv[i+1]);
                process.exit(1);
            }
            i++;
        } else if (argv[i] == "--solve") {
            action = "SOLVE";
            printSolution = true;
        } else if (argv[i] == "--log-history") {
            logHistory = true;
        } else if (argv[i] == "--nolog-history") {
            logHistory = false;
        } else if (argv[i] == "--one-line") {
            printStyle=qqwing.PrintStyle.ONE_LINE;
        } else if (argv[i] == "--compact") {
            printStyle=qqwing.PrintStyle.COMPACT;
        } else if (argv[i] == "--readable") {
            printStyle=qqwing.PrintStyle.READABLE;
        } else if (argv[i] == "--csv") {
            printStyle=qqwing.PrintStyle.CSV;
        } else if (argv[i] == "-n" || argv[i] == "--number") {
            if (i+1 < argv.length){
                numberToGenerate = parseInt(argv[i+1]);
                i++;
            } else {
                console.log("Please specify a number.");
                process.exit(1);
            }
        } else if (argv[i] == "-h" || argv[i] == "--help" || argv[i] == "help" || argv[i] == "?") {
            printHelp();
            process.exit(0);
        } else if (argv[i] == "--version") {
            printVersion();
            process.exit(0);
        } else if (argv[i] == "--about") {
            printAbout();
            process.exit(0);
        } else {
            console.log("Unknown argument: '"+argv[i]+"'");
            printHelp();
            process.exit(0);
        }
    }

    if (action == "NONE"){
        console.log("Either --solve or --generate must be specified.");
        printHelp();
        process.exit(1);
    }

    // If printing out CSV, print a header
    if (printStyle == qqwing.PrintStyle.CSV){
        if (printPuzzle) process.stdout.write("Puzzle,");
        if (printSolution) process.stdout.write("Solution,");
        if (printHistory) process.stdout.write("Solve History,");
        if (printInstructions) process.stdout.write("Solve Instructions,");
        if (countSolutions) process.stdout.write("Solution Count,");
        if (timer) process.stdout.write("Time (milliseconds),");
        if (printStats) process.stdout.write("Givens,Singles,Hidden Singles,Naked Pairs,Hidden Pairs,Pointing Pairs/Triples,Box/Line Intersections,Guesses,Backtracks,Difficulty");
        console.log("");
    }

    // Create a new puzzle board
    // and set the options
    var ss = new qqwing();
    ss.setRecordHistory(printHistory || printInstructions || printStats || difficulty!=qqwing.Difficulty.UNKNOWN);
    ss.setLogHistory(logHistory);
    ss.setPrintStyle(printStyle);

    // Solve puzzle or generate puzzles
    // until end of input for solving, or
    // until we have generated the specified number.
    var done = false;
    var numberGenerated = 0;
    while (!done){
        // record the start time for the timer.
        var puzzleStartTime = getMicroseconds();

        // iff something has been printed for this particular puzzle
        var printedSomething = false;

        // Record whether the puzzle was possible or not,
        // so that we don't try to solve impossible givens.
        var havePuzzle = false;
        if (action == "GENERATE"){
            // Generate a puzzle
            havePuzzle = ss.generatePuzzleSymmetry(symmetry);
            if (!havePuzzle && printPuzzle){
                process.stdout.write("Could not generate puzzle.");
                if (printStyle==qqwing.PrintStyle.CSV){
                    console.log(",");
                } else {
                    console.log("");
                }
                printedSomething = true;
                    process.exit(1);
            }
        } else {
            // Read the next puzzle on STDIN
            var puzzle = [];
            if (readPuzzleFromStdIn(puzzle)){
                havePuzzle = ss.setPuzzle(puzzle);
                if (!havePuzzle){
                    if (printPuzzle){
                        ss.printPuzzle();
                        printedSomething = true;
                    }
                    if (printSolution) {
                        process.stdout.write("Puzzle is not possible.");
                        if (printStyle==qqwing.PrintStyle.CSV){
                            process.stdout.write(",");
                        } else {
                            console.log("");
                        }
                        printedSomething = true;
                    }
                }
            } else {
                // Set loop to terminate when nothing is left on STDIN
                havePuzzle = false;
                done = true;
            }
            puzzle = null;
        }

        var solutions = 0;

        if (havePuzzle){

            // Count the solutions if requested.
            // (Must be done before solving, as it would
            // mess up the stats.)
            if (countSolutions){
                solutions = ss.countSolutions();
            }

            // Solve the puzzle
            if (printSolution || printHistory || printStats || printInstructions || difficulty!=qqwing.Difficulty.UNKNOWN){
                ss.solve();
            }

            // Bail out if it didn't meet the difficulty standards for generation
            if (action == "GENERATE"){
                if (difficulty!=qqwing.Difficulty.UNKNOWN && difficulty!=ss.getDifficulty()){
                    havePuzzle = false;
                } else {
                    numberGenerated++;
                    // Set loop to terminate if enough have been generated.
                    if (numberGenerated >= numberToGenerate) done = true;
                }
            }
        }

        // Check havePuzzle again, it may have changed based on difficulty
        if (havePuzzle){

            // With a puzzle now in hand and possibly solved
            // print out the solution, stats, etc.
            printedSomething = true;

            // Record the end time for the timer.
            var puzzleDoneTime = getMicroseconds();

            // Print the puzzle itself.
            if (printPuzzle) ss.printPuzzle();

            // Print the solution if there is one
            if (printSolution){
                if (ss.isSolved()){
                    ss.printSolution();
                } else {
                    process.stdout.write("Puzzle has no solution.");
                    if (printStyle==qqwing.PrintStyle.CSV){
                        process.stdout.write(",");
                    } else {
                        console.log("");
                    }
                }
            }

            // Print the steps taken to solve or attempt to solve the puzzle.
            if (printHistory) ss.printSolveHistory();
            // Print the instructions for solving the puzzle
            if (printInstructions) ss.printSolveInstructions();

            // Print the number of solutions to the puzzle.
            if (countSolutions){
                if (printStyle == qqwing.PrintStyle.CSV){
                    process.stdout.write(solutions+",");
                } else {
                    if (solutions == 0){
                        console.log("There are no solutions to the puzzle.");
                    } else if (solutions == 1){
                        console.log("The solution to the puzzle is unique.");
                    } else {
                        console.log("There are "+solutions+" solutions to the puzzle.");
                    }
                }
            }

            // Print out the time it took to solve the puzzle.
            if (timer){
                var t = (puzzleDoneTime - puzzleStartTime)/1000.0;
                if (printStyle == qqwing.PrintStyle.CSV){
                    process.stdout.write(t+",");
                } else {
                    console.log("Time: "+t +" milliseconds");
                }
            }

            // Print any stats we were able to gather while solving the puzzle.
            if (printStats){
                var givenCount = ss.getGivenCount();
                var singleCount = ss.getSingleCount();
                var hiddenSingleCount = ss.getHiddenSingleCount();
                var nakedPairCount = ss.getNakedPairCount();
                var hiddenPairCount = ss.getHiddenPairCount();
                var pointingPairTripleCount = ss.getPointingPairTripleCount();
                var boxReductionCount = ss.getBoxLineReductionCount();
                var guessCount = ss.getGuessCount();
                var backtrackCount = ss.getBacktrackCount();
                var difficultyString = ss.getDifficultyAsString();
                if (printStyle == qqwing.PrintStyle.CSV){
                    console.log(givenCount+"," +singleCount+","+hiddenSingleCount
                            +","+nakedPairCount+","+hiddenPairCount
                            +"," +pointingPairTripleCount +"," +boxReductionCount
                            +","+guessCount+","+backtrackCount
                            +","+difficultyString+",");
                } else {
                    console.log("Number of Givens: "+givenCount );
                    console.log("Number of Singles: "+singleCount);
                    console.log("Number of Hidden Singles: "+hiddenSingleCount );
                    console.log("Number of Naked Pairs: "+nakedPairCount );
                    console.log("Number of Hidden Pairs: "+hiddenPairCount );
                    console.log("Number of Pointing Pairs/Triples: "+pointingPairTripleCount );
                    console.log("Number of Box/Line Intersections: "+boxReductionCount );
                    console.log("Number of Guesses: "+guessCount );
                    console.log("Number of Backtracks: "+backtrackCount );
                    console.log("Difficulty: "+difficultyString );
                }
            }
            puzzleCount++;
        }
        if (printedSomething && printStyle == qqwing.PrintStyle.CSV){
            console.log("");
        }
    }

    var applicationDoneTime = getMicroseconds();
    // Print out the time it took to do everything
    if (timer){
        var t = (applicationDoneTime - applicationStartTime)/1000000.0;
        console.log(puzzleCount+" puzzle"+((puzzleCount==1)?"":"s")+" "+(action=="GENERATE"?"generated":"solved")+" in "+t+" seconds.");
    }
} catch (e){
    console.log(e.stack);
    process.exit(1);
}
process.exit(0);

function printHelp(){
    console.log("qqwing <options>");
    console.log("Sudoku solver and generator.");
    console.log("  --generate <num>     Generate new puzzles");
    console.log("  --solve              Solve all the puzzles from standard input");
    console.log("  --difficulty <diff>  Generate only simple, easy, intermediate, expert, or any");
    console.log("  --symmetry <sym>     Symmetry: none, rotate90, rotate180, mirror, flip, or random");
    console.log("  --puzzle             Print the puzzle (default when generating)");
    console.log("  --nopuzzle           Do not print the puzzle (default when solving)");
    console.log("  --solution           Print the solution (default when solving)");
    console.log("  --nosolution         Do not print the solution (default when generating)");
    console.log("  --stats              Print statistics about moves used to solve the puzzle");
    console.log("  --nostats            Do not print statistics (default)");
    console.log("  --timer              Print time to generate or solve each puzzle");
    console.log("  --notimer            Do not print solve or generation times (default)");
    console.log("  --count-solutions    Count the number of solutions to puzzles");
    console.log("  --nocount-solutions  Do not count the number of solutions (default)");
    console.log("  --history            Print trial and error used when solving");
    console.log("  --nohistory          Do not print trial and error to solve (default)");
    console.log("  --instructions       Print the steps (at least 81) needed to solve the puzzle");
    console.log("  --noinstructions     Do not print steps to solve (default)");
    console.log("  --log-history        Print trial and error to solve as it happens");
    console.log("  --nolog-history      Do not print trial and error  to solve as it happens");
    console.log("  --one-line           Print puzzles on one line of 81 characters");
    console.log("  --compact            Print puzzles on 9 lines of 9 characters");
    console.log("  --readable           Print puzzles in human readable form (default)");
    console.log("  --csv                Output CSV format with one line puzzles");
    console.log("  --help               Print this message");
    console.log("  --about              Author and license information");
    console.log("  --version            Display current version number");
}

function printVersion(){
    console.log("qqwing 1.3.4");
}

function printAbout(){
    console.log("qqwing - Sudoku solver and generator");
    console.log("Copyright (C) 2014 Stephen Ostermiller");
    console.log("");
    console.log("This program is free software; you can redistribute it and/or modify");
    console.log("it under the terms of the GNU General Public License as published by");
    console.log("the Free Software Foundation; either version 2 of the License, or");
    console.log("(at your option) any later version.");
    console.log("");
    console.log("This program is distributed in the hope that it will be useful,");
    console.log("but WITHOUT ANY WARRANTY; without even the implied warranty of");
    console.log("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the");
    console.log("GNU General Public License for more details.");
    console.log("");
    console.log("You should have received a copy of the GNU General Public License along");
    console.log("with this program; if not, write to the Free Software Foundation, Inc.,");
    console.log("51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.");
}

function getMicroseconds(){
    return new Date().getTime() * 1000;
}


/**
 * Read a sudoku puzzle from standard input.
 * STDIN is processed one character at a time
 * until the sudoku is filled in.  Any digit
 * or period is used to fill the sudoku, any
 * other character is ignored.
 */
function readPuzzleFromStdIn(puzzle){
    var fs = require('fs');
    var read = 0;
    while (read < qqwing.BOARD_SIZE){
        var c = fs.readSync(process.stdin.fd, 1);
        if (c[1] == 0) return false;
        if (c[0] >= '1' && c[0] <='9'){
            puzzle[read] = c[0]-'0';
            read++;
        }
        if (c[0] == '.' || c[0] == '0'){
            puzzle[read] = 0;
            read++;
        }
    }
    return true;
}