qqwing 1.3.5

QQwing is software for generating and solving Sudoku puzzles. This is a rust version.
Documentation
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
//! qqwing - Sudoku solver and generator
//!
//! Copyright (C) 2006-2014 Stephen Ostermiller <http://ostermiller.org/>
//!
//! Copyright (C) 2007 Jacques Bensimon (jacques@ipm.com)
//!
//! Copyright (C) 2007 Joel Yarde (joel.yarde - gmail.com)
//!
//!
//! 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.

use rand::{self, random, seq::SliceRandom, thread_rng};
use std::usize;
use strum::{EnumIter, EnumString};
use thiserror::Error;
use tracing::{debug, info};

use difficulty::Difficulty;
use logitem::LogItem;
use logtype::LogType;
use symmetry::Symmetry;

/// Module for puzzle difficulty.
pub mod difficulty;
/// Module for log item.
pub mod logitem;
/// Module for log type.
pub mod logtype;
/// Module for puzzle symmetry.
pub mod symmetry;
const UNSET_VALUE: usize = 4294967295;
const NL: &str = "\n";
const GRID_SIZE: usize = 3;
const ROW_COL_SEC_SIZE: usize = GRID_SIZE * GRID_SIZE;
const SEC_GROUP_SIZE: usize = ROW_COL_SEC_SIZE * GRID_SIZE;
pub const BOARD_SIZE: usize = ROW_COL_SEC_SIZE * ROW_COL_SEC_SIZE;
const POSSIBILITY_SIZE: usize = BOARD_SIZE * ROW_COL_SEC_SIZE;

#[derive(Error, Debug)]
pub enum QQWingError {
    #[error("Marking position that already has been marked.")]
    PositionAlreadyMarked,
    #[error("Marking position that was marked another round.")]
    PositionMarkedAnotherRound,
    #[error("Marking impossible position.")]
    PositionImpossible,
}

/// The board containing all the memory structures and methods for solving or
/// generating sudoku puzzles.
#[derive(Debug)]
pub struct QQWing {
    /**

     * The last round of solving
     */
    last_solve_round: u8,

    /**

     * 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".
     */
    puzzle: [u8; BOARD_SIZE],

    /**

     * The 81 integers that make up a sudoku puzzle. The solution is built here,
     * after completion all will be 1-9.
     */
    solution: [u8; 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.
     */
    solution_round: [u8; 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 (recursion level) at which it was determined that it could not be a
     * possibility.
     */
    possibilities: [u8; 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.
     */
    random_board_array: [u8; BOARD_SIZE],

    /**

     * An array with one element for each position (9), in some random order to
     * be used when trying each position in turn during guesses.
     */
    random_possibility_array: [u8; ROW_COL_SEC_SIZE],

    /**

     * Whether or not to record history
     */
    record_history: bool,

    /**

     * Whether or not to print history as it happens
     */
    log_history: bool,

    /**

     * A list of moves used to solve the puzzle. This list contains all moves,
     * even on solve branches that did not lead to a solution.
     */
    solve_history: Vec<LogItem>,

    /**

     * A list of moves used to solve the puzzle. This list contains only the
     * moves needed to solve the puzzle, but doesn't contain information about
     * bad guesses.
     */
    solve_instructions: Vec<LogItem>,

    /**

     * The style with which to print puzzles and solutions
     */
    pub print_style: PrintStyle,
}

impl QQWing {
    pub fn new() -> Self {
        Self {
            last_solve_round: 0,
            puzzle: [0; BOARD_SIZE],
            solution: [0; BOARD_SIZE],
            solution_round: [0; BOARD_SIZE],
            possibilities: [0; POSSIBILITY_SIZE],
            random_possibility_array: core::array::from_fn::<u8, ROW_COL_SEC_SIZE, _>(|i| i as u8),
            random_board_array: core::array::from_fn::<u8, BOARD_SIZE, _>(|i| i as u8),
            record_history: false,
            log_history: false,
            solve_history: Vec::new(),
            solve_instructions: Vec::new(),
            print_style: PrintStyle::READABLE,
        }
    }

    /**

     * Get the number of cells that are set in the puzzle (as opposed to figured
     * out in the solution
     */
    fn get_given_count(&self) -> u32 {
        let mut count = 0;
        for i in 0..BOARD_SIZE {
            if self.puzzle[i] != 0 {
                count += 1;
            }
        }
        count
    }

    /**

     * Set the board to the given puzzle. The given puzzle must be an array of 81 integers.
     */
    pub fn set_puzzle(&mut self, init_puzzle: Vec<u8>) -> bool {
        for i in 0..BOARD_SIZE {
            self.puzzle[i] = init_puzzle[i];
        }
        self.reset()
    }

    /**

     * Reset the board to its initial state with only the givens. This method
     * clears any solution, resets statistics, and clears any history messages.
     */
    fn reset(&mut self) -> bool {
        self.solution.fill(0);
        self.solution_round.fill(0);
        self.possibilities.fill(0);
        self.solve_history.clear();
        self.solve_instructions.clear();

        let round = 1;
        for position in 0..BOARD_SIZE {
            if self.puzzle[position] > 0 {
                let val_index = self.puzzle[position] - 1;
                let val_pos = QQWing::get_possibility_index(val_index as usize, position);
                let value = self.puzzle[position];
                if self.possibilities[val_pos] != 0 {
                    return false;
                }
                let _ = self.mark(position, round, value).unwrap();
                if self.log_history || self.record_history {
                    self.add_history_item(LogItem::new(
                        round,
                        LogType::Given,
                        value as usize,
                        position,
                    ));
                }
            }
        }

        true
    }

    /**

     * Get the difficulty rating.
     *
     * This method will return Difficulty::UNKNOWN unless
     * a puzzle has been generated or set and then the following methods called:
     * set_record_history(true), and solve()
     */
    pub fn get_difficulty(&self) -> Difficulty {
        if self.get_guess_count() > 0 {
            return Difficulty::EXPERT;
        }
        if self.get_box_line_reduction_count() > 0 {
            return Difficulty::MEDIUM;
        }
        if self.get_pointing_pair_triple_count() > 0 {
            return Difficulty::MEDIUM;
        }
        if self.get_hidden_pair_count() > 0 {
            return Difficulty::MEDIUM;
        }
        if self.get_naked_pair_count() > 0 {
            return Difficulty::MEDIUM;
        }
        if self.get_hidden_single_count() > 0 {
            return Difficulty::EASY;
        }
        if self.get_single_count() > 0 {
            return Difficulty::SIMPLE;
        }
        return Difficulty::UNKNOWN;
    }

    /**

     * Get the number of cells for which the solution was determined because
     * there was only one possible value for that cell.
     */
    fn get_single_count(&self) -> usize {
        QQWing::get_log_count(&self.solve_instructions, 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.
     */
    fn get_hidden_single_count(&self) -> usize {
        QQWing::get_log_count(&self.solve_instructions, LogType::HiddenSingleRow)
            + QQWing::get_log_count(&self.solve_instructions, LogType::HiddenSingleColumn)
            + QQWing::get_log_count(&self.solve_instructions, LogType::HiddenSingleSection)
    }

    /**

     * Get the number of naked pair reductions that were performed in solving
     * this puzzle.
     */
    fn get_naked_pair_count(&self) -> usize {
        QQWing::get_log_count(&self.solve_instructions, LogType::NakedPairRow)
            + QQWing::get_log_count(&self.solve_instructions, LogType::NakedPairColumn)
            + QQWing::get_log_count(&self.solve_instructions, LogType::NakedPairSection)
    }

    /**

     * Get the number of hidden pair reductions that were performed in solving
     * this puzzle.
     */
    fn get_hidden_pair_count(&self) -> usize {
        QQWing::get_log_count(&self.solve_instructions, LogType::HiddenPairRow)
            + QQWing::get_log_count(&self.solve_instructions, LogType::HiddenPairColumn)
            + QQWing::get_log_count(&self.solve_instructions, LogType::HiddenPairSection)
    }

    /**

     * Get the number of pointing pair/triple reductions that were performed in
     * solving this puzzle.
     */
    fn get_pointing_pair_triple_count(&self) -> usize {
        QQWing::get_log_count(&self.solve_instructions, LogType::PointingPairTripleRow)
            + QQWing::get_log_count(&self.solve_instructions, LogType::PointingPairTripleColumn)
    }

    /**

     * Get the number of box/line reductions that were performed in solving this
     * puzzle.
     */
    fn get_box_line_reduction_count(&self) -> usize {
        QQWing::get_log_count(&self.solve_instructions, LogType::RowBox)
            + QQWing::get_log_count(&self.solve_instructions, LogType::ColumnBox)
    }

    /**

     * Get the number lucky guesses in solving this puzzle.
     */
    fn get_guess_count(&self) -> usize {
        QQWing::get_log_count(&self.solve_instructions, LogType::Guess)
    }

    /**

     * Get the number of backtracks (unlucky guesses) required when solving this
     * puzzle.
     */
    fn get_backtrack_count(&self) -> usize {
        QQWing::get_log_count(&self.solve_history, LogType::Rollback)
    }

    fn shuffle_random_arrays(&mut self) {
        let mut rng = thread_rng();
        self.random_board_array.shuffle(&mut rng);
        self.random_possibility_array.shuffle(&mut rng);
    }

    fn clear_puzzle(&mut self) {
        debug!("Clear any existing puzzle");
        for i in 0..BOARD_SIZE {
            self.puzzle[i] = 0;
        }
        self.reset();
    }

    /// Generate a new sudoku puzzle.
    pub fn generate_puzzle(&mut self) -> bool {
        self.generate_puzzle_symmetry(Symmetry::NONE)
    }

    fn generate_puzzle_symmetry(&mut self, symmetry: Symmetry) -> bool {
        let mut symmetry = symmetry;
        if symmetry == Symmetry::RANDOM {
            symmetry = QQWing::get_random_symmetry();
        }
        debug!("Symmetry: {:?}", symmetry);
        // Don't record history while generating.
        let rec_history = self.record_history;
        self.set_record_history(false);
        let l_history = self.record_history;
        self.set_log_history(false);

        self.clear_puzzle();

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

        // 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
        self.solve();

        if symmetry == 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)
            self.rollback_non_guesses();
        }

        // Record all marked squares as the puzzle so
        // that we can call countSolutions without losing it.
        for i in 0..BOARD_SIZE {
            self.puzzle[i] = self.solution[i];
        }

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

        // 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 i in 0..BOARD_SIZE {
            // check all the positions, but in shuffled order
            let position = self.random_board_array[i] as usize;
            if self.puzzle[position] > 0 {
                let mut positionsym1 = UNSET_VALUE;
                let mut positionsym2 = UNSET_VALUE;
                let mut positionsym3 = UNSET_VALUE;
                match symmetry {
                    Symmetry::ROTATE90 => {
                        positionsym2 = QQWing::row_column_to_cell(
                            ROW_COL_SEC_SIZE - 1 - QQWing::cell_to_column(position),
                            QQWing::cell_to_row(position),
                        );
                        positionsym3 = QQWing::row_column_to_cell(
                            QQWing::cell_to_column(position),
                            ROW_COL_SEC_SIZE - 1 - QQWing::cell_to_row(position),
                        );
                    }
                    Symmetry::ROTATE180 => {
                        positionsym1 = QQWing::row_column_to_cell(
                            ROW_COL_SEC_SIZE - 1 - QQWing::cell_to_row(position),
                            ROW_COL_SEC_SIZE - 1 - QQWing::cell_to_column(position),
                        )
                    }
                    Symmetry::MIRROR => {
                        positionsym1 = QQWing::row_column_to_cell(
                            QQWing::cell_to_row(position),
                            ROW_COL_SEC_SIZE - 1 - QQWing::cell_to_column(position),
                        )
                    }
                    Symmetry::FLIP => {
                        positionsym1 = QQWing::row_column_to_cell(
                            ROW_COL_SEC_SIZE - 1 - QQWing::cell_to_row(position),
                            QQWing::cell_to_column(position),
                        )
                    }
                    _ => {}
                }
                // try backing out the value and
                // counting solutions to the puzzle
                let saved_value = self.puzzle[position];
                self.puzzle[position] = 0;
                let mut saved_sym1 = 0;
                if positionsym1 != UNSET_VALUE {
                    saved_sym1 = self.puzzle[positionsym1];
                    self.puzzle[positionsym1] = 0;
                }
                let mut saved_sym2 = 0;
                if positionsym2 != UNSET_VALUE {
                    saved_sym2 = self.puzzle[positionsym2];
                    self.puzzle[positionsym2] = 0;
                }
                let mut saved_sym3 = 0;
                if positionsym3 != UNSET_VALUE {
                    saved_sym3 = self.puzzle[positionsym3];
                    self.puzzle[positionsym3] = 0;
                }
                self.reset();
                if self.count_solutions_round(2, true) > 1 {
                    // Put it back in, it is needed
                    self.puzzle[position] = saved_value;
                    if positionsym1 != UNSET_VALUE && saved_sym1 != 0 {
                        self.puzzle[positionsym1] = saved_sym1;
                    }
                    if positionsym2 != UNSET_VALUE && saved_sym2 != 0 {
                        self.puzzle[positionsym2] = saved_sym2;
                    }
                    if positionsym3 != UNSET_VALUE && saved_sym3 != 0 {
                        self.puzzle[positionsym3] = saved_sym3;
                    }
                }
            }
        }

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

        // Restore recording history.
        self.set_record_history(rec_history);
        self.set_log_history(l_history);

        true
    }

    fn rollback_non_guesses(&mut self) {
        // Guesses are odd rounds
        // Non-guesses are even rounds
        for i in 2..self.last_solve_round {
            if i % 2 == 1 {
                continue;
            }
            self.rollback_round(i);
        }
    }

    pub fn set_print_style(&mut self, ps: PrintStyle) {
        self.print_style = ps;
    }

    pub fn set_record_history(&mut self, rec_history: bool) {
        self.record_history = rec_history;
    }

    pub fn set_log_history(&mut self, log_hist: bool) {
        self.log_history = log_hist;
    }

    fn add_history_item(&mut self, l: LogItem) {
        if self.log_history {
            info!("{}", l);
        }
        if self.record_history {
            self.solve_history.push(l.clone()); // ->push_back(l);
            self.solve_instructions.push(l); // ->push_back(l);
        }
    }

    pub fn print_history(&self, v: Vec<LogItem>) {
        println!("{}", self.history_to_string(v));
    }

    fn history_to_string(&self, v: Vec<LogItem>) -> String {
        let mut sb = String::new();
        if !self.record_history {
            sb.push_str("History was not recorded.");
            if self.print_style == PrintStyle::CSV {
                sb.push_str(" -- ");
            } else {
                sb.push_str(NL);
            }
        }
        for i in 0..v.len() {
            sb.push_str(&(i + 1).to_string());
            sb.push_str(". ");
            sb.push_str(format!("{}", v[i]).as_str());
            if self.print_style == PrintStyle::CSV {
                sb.push_str(" -- ");
            } else {
                sb.push_str(NL);
            }
        }
        if self.print_style == PrintStyle::CSV {
            sb.push_str(",");
        } else {
            sb.push_str(NL);
        }
        sb
    }

    pub fn print_solve_instructions(&self) {
        println!("\nSolve instructions:");
        println!("{}", self.get_solve_instructions_string());
    }

    fn get_solve_instructions_string(&self) -> String {
        if self.is_solved() {
            return self.history_to_string(self.solve_instructions.clone());
        } else {
            return "No solve instructions - Puzzle is not possible to solve.".to_string();
        }
    }

    pub fn get_solve_instructions(&self) -> Vec<LogItem> {
        match self.is_solved() {
            true => self.solve_instructions.clone(),
            false => Vec::new(),
        }
    }

    pub fn print_solve_history(&self) {
        self.print_history(self.solve_history.clone());
    }

    pub fn get_solve_history_string(&self) -> String {
        self.history_to_string(self.solve_history.clone())
    }

    pub fn get_solve_history(&self) -> Vec<LogItem> {
        self.solve_history.clone()
    }

    /// Solve the puzzle.
    pub fn solve(&mut self) -> bool {
        self.reset();
        self.shuffle_random_arrays();
        debug!("Solve round 2");
        self.solve_round(2)
    }

    fn solve_round(&mut self, round: u8) -> bool {
        self.last_solve_round = round;

        while self.single_solve_move(round) {
            if self.is_solved() {
                return true;
            }
            if self.is_impossible() {
                return false;
            }
        }

        let next_guess_round = round + 1;
        let next_round = round + 2;
        let mut guess_number = 0;
        while self.guess(next_guess_round, guess_number) {
            if self.is_impossible() || !self.solve_round(next_round) {
                self.rollback_round(next_round);
                self.rollback_round(next_guess_round);
            } else {
                return true;
            }
            guess_number += 1;
        }
        false
    }

    /**

     * return true if the puzzle has no solutions at all
     */
    pub fn has_no_solution(&mut self) -> bool {
        self.count_solutions_limited() == 0
    }

    /**

     * return true if the puzzle has a solution
     * and only a single solution
     */
    pub fn has_unique_solution(&mut self) -> bool {
        self.count_solutions_limited() == 1
    }

    /**

     * return true if the puzzle has more than one solution
     */

    pub fn has_multiple_solutions(&mut self) -> bool {
        self.count_solutions_limited() > 1
    }

    /**

     * Count the number of solutions to the puzzle
     */
    pub fn count_total_solutions(&mut self) -> u32 {
        self.count_solutions(false)
    }

    /**

     * Count the number of solutions to the puzzle
     * but return two any time there are two or
     * more solutions.  This method will run much
     * faster than count_total_solutions() when there
     * are many possible solutions and can be used
     * when you are interested in knowing if the
     * puzzle has zero, one, or multiple solutions.
     */
    pub fn count_solutions_limited(&mut self) -> u32 {
        self.count_solutions(true)
    }

    fn count_solutions(&mut self, limit_to_two: bool) -> u32 {
        // Don't record history while generating.
        let rec_history = self.record_history;
        self.set_record_history(false);
        let l_history = self.log_history;
        self.set_log_history(false);

        self.reset();
        let solution_count = self.count_solutions_round(2, limit_to_two);

        // Restore recording history.
        self.set_record_history(rec_history);
        self.set_log_history(l_history);

        solution_count
    }

    fn count_solutions_round(&mut self, round: u8, limit_to_two: bool) -> u32 {
        while self.single_solve_move(round) {
            if self.is_solved() {
                self.rollback_round(round);
                return 1;
            }
            if self.is_impossible() {
                self.rollback_round(round);
                return 0;
            }
        }

        let mut solutions = 0;
        let next_round = round + 1;
        let mut guess_number = 0;
        while self.guess(next_round, guess_number) {
            solutions += self.count_solutions_round(next_round, limit_to_two);
            if limit_to_two && solutions >= 2 {
                self.rollback_round(round);
                return solutions;
            }
            guess_number += 1;
        }
        self.rollback_round(round);

        solutions
    }

    fn rollback_round(&mut self, round: u8) {
        if self.log_history || self.record_history {
            self.add_history_item(LogItem::new(
                round,
                LogType::Rollback,
                4294967295,
                4294967295,
            ));
        }

        for i in 0..BOARD_SIZE {
            if self.solution_round[i] == round {
                self.solution_round[i] = 0;
                self.solution[i] = 0;
            }
        }
        for i in 0..POSSIBILITY_SIZE {
            if self.possibilities[i] == round {
                self.possibilities[i] = 0;
            }
        }
        while self.solve_instructions.len() > 0
            && self.solve_instructions.last().unwrap().get_round() == round
        {
            let i = self.solve_instructions.len() - 1;
            self.solve_instructions.remove(i);
        }
    }

    /// Check if the puzzle is solved.
    pub fn is_solved(&self) -> bool {
        for i in 0..BOARD_SIZE {
            if self.solution[i] == 0 {
                return false;
            }
        }
        true
    }

    fn is_impossible(&self) -> bool {
        for position in 0..BOARD_SIZE {
            if self.solution[position] == 0 {
                let mut count = 0;
                for val_index in 0..ROW_COL_SEC_SIZE {
                    let val_pos = QQWing::get_possibility_index(val_index, position);
                    if self.possibilities[val_pos] == 0 {
                        count += 1;
                    }
                }
                if count == 0 {
                    return true;
                }
            }
        }
        false
    }

    fn find_position_with_fewest_possibilities(&self) -> usize {
        let mut min_possibilities = 10;
        let mut best_position = 0;
        for i in 0..BOARD_SIZE {
            let position = self.random_board_array[i];
            if self.solution[position as usize] == 0 {
                let mut count = 0;
                for val_index in 0..ROW_COL_SEC_SIZE {
                    let val_pos = QQWing::get_possibility_index(val_index, position as usize);
                    if self.possibilities[val_pos] == 0 {
                        count += 1;
                    }
                }
                if count < min_possibilities {
                    min_possibilities = count;
                    best_position = position;
                }
            }
        }
        return best_position as usize;
    }

    fn guess(&mut self, round: u8, guess_number: u32) -> bool {
        debug!("Guess round: {}, number: {}", round, guess_number);
        let mut local_guess_count = 0;
        let position = self.find_position_with_fewest_possibilities();
        for i in 0..ROW_COL_SEC_SIZE {
            let val_index = self.random_possibility_array[i];
            let val_pos = QQWing::get_possibility_index(val_index as usize, position);
            if self.possibilities[val_pos] == 0 {
                if local_guess_count == guess_number {
                    let value = val_index + 1;
                    if self.log_history || self.record_history {
                        self.add_history_item(LogItem::new(
                            round,
                            LogType::Guess,
                            value as usize,
                            position,
                        ));
                    }
                    let _ = self.mark(position, round, value).unwrap();
                    return true;
                }
                local_guess_count += 1;
            }
        }
        false
    }

    fn single_solve_move(&mut self, round: u8) -> bool {
        debug!("Single Solve Move, round: {}", round);
        if self.only_possibility_for_cell(round) {
            debug!("only_possibility_for_cell round {} is ture", round);
            return true;
        }
        if self.only_value_in_section(round) {
            debug!("only_value_in_section round {} is ture", round);
            return true;
        }
        if self.only_value_in_row(round) {
            debug!("only_value_in_row round {} is ture", round);
            return true;
        }
        if self.only_value_in_column(round) {
            debug!("only_value_in_column round {} is ture", round);
            return true;
        }
        if self.handle_naked_pairs(round) {
            debug!("handle_naked_pairs round {} is ture", round);
            return true;
        }
        if self.pointing_row_reduction(round) {
            debug!("pointing_row_reduction round {} is ture", round);
            return true;
        }
        if self.pointing_column_reduction(round) {
            debug!("pointing_column_reduction round {} is ture", round);
            return true;
        }
        if self.row_box_reduction(round) {
            debug!("row_box_reduction round {} is ture", round);
            return true;
        }
        if self.col_box_reduction(round) {
            debug!("col_box_reduction round {} is ture", round);
            return true;
        }
        if self.hidden_pair_in_row(round) {
            debug!("hidden_pair_in_row round {} is ture", round);
            return true;
        }
        if self.hidden_pair_in_column(round) {
            debug!("hidden_pair_in_column round {} is ture", round);
            return true;
        }
        if self.hidden_pair_in_section(round) {
            debug!("hidden_pair_in_section round {} is ture", round);
            return true;
        }
        debug!("single_solve_move round {} is false", round);
        false
    }

    fn col_box_reduction(&mut self, round: u8) -> bool {
        debug!("col_box_reduction round: {}", round);
        for val_index in 0..ROW_COL_SEC_SIZE {
            for col in 0..ROW_COL_SEC_SIZE {
                let col_start = col;
                let mut in_one_box = true;
                let mut col_box = UNSET_VALUE;
                for i in 0..GRID_SIZE {
                    for j in 0..GRID_SIZE {
                        let row = i * GRID_SIZE + j;
                        let position = QQWing::row_column_to_cell(row, col);
                        let val_pos = QQWing::get_possibility_index(val_index, position);
                        if self.possibilities[val_pos] == 0 {
                            if col_box == UNSET_VALUE || col_box == i {
                                col_box = i;
                            } else {
                                in_one_box = false;
                            }
                        }
                    }
                }
                if in_one_box && col_box != UNSET_VALUE {
                    let mut done_something = false;
                    let row = GRID_SIZE * col_box;
                    let sec_start =
                        QQWing::cell_to_section_start_cell(QQWing::row_column_to_cell(row, col));
                    let sec_start_row = QQWing::cell_to_row(sec_start);
                    let sec_start_col = QQWing::cell_to_column(sec_start);
                    for i in 0..GRID_SIZE {
                        for j in 0..GRID_SIZE {
                            let row2 = sec_start_row + i;
                            let col2 = sec_start_col + j;
                            let position = QQWing::row_column_to_cell(row2, col2);
                            let val_pos = QQWing::get_possibility_index(val_index, position);
                            if col != col2 && self.possibilities[val_pos] == 0 {
                                self.possibilities[val_pos] = round;
                                done_something = true;
                            }
                        }
                    }
                    if done_something {
                        if self.log_history || self.record_history {
                            self.add_history_item(LogItem::new(
                                round,
                                LogType::ColumnBox,
                                val_index + 1,
                                col_start,
                            ));
                        }
                        return true;
                    }
                }
            }
        }
        false
    }

    fn row_box_reduction(&mut self, round: u8) -> bool {
        debug!("row_box_reduction round: {}", round);
        for val_index in 0..ROW_COL_SEC_SIZE {
            for row in 0..ROW_COL_SEC_SIZE {
                let row_start = row * 9;
                let mut in_one_box = true;
                let mut row_box = UNSET_VALUE;
                for i in 0..GRID_SIZE {
                    for j in 0..GRID_SIZE {
                        let column = i * GRID_SIZE + j;
                        let position = QQWing::row_column_to_cell(row, column);
                        let val_pos = QQWing::get_possibility_index(val_index, position);
                        if self.possibilities[val_pos] == 0 {
                            if row_box == UNSET_VALUE || row_box == i {
                                row_box = i;
                            } else {
                                in_one_box = false;
                            }
                        }
                    }
                }
                if in_one_box && row_box != UNSET_VALUE {
                    let mut done_something = false;
                    let column = GRID_SIZE * row_box;
                    let sec_start =
                        QQWing::cell_to_section_start_cell(QQWing::row_column_to_cell(row, column));
                    let sec_start_row = QQWing::cell_to_row(sec_start);
                    let sec_start_col = QQWing::cell_to_column(sec_start);
                    for i in 0..GRID_SIZE {
                        for j in 0..GRID_SIZE {
                            let row2 = sec_start_row + i;
                            let col2 = sec_start_col + j;
                            let position = QQWing::row_column_to_cell(row2, col2);
                            let val_pos = QQWing::get_possibility_index(val_index, position);
                            if row != row2 && self.possibilities[val_pos] == 0 {
                                self.possibilities[val_pos] = round;
                                done_something = true;
                            }
                        }
                    }
                    if done_something {
                        if self.log_history || self.record_history {
                            self.add_history_item(LogItem::new(
                                round,
                                LogType::RowBox,
                                val_index + 1,
                                row_start,
                            ));
                        }
                        return true;
                    }
                }
            }
        }
        false
    }

    fn pointing_row_reduction(&mut self, round: u8) -> bool {
        debug!("pointing_row_reduction round: {}", round);
        for val_index in 0..ROW_COL_SEC_SIZE {
            for section in 0..ROW_COL_SEC_SIZE {
                let sec_start = QQWing::section_to_first_cell(section);
                let mut in_one_row = true;
                let mut box_row = UNSET_VALUE;
                for j in 0..GRID_SIZE {
                    for i in 0..GRID_SIZE {
                        let sec_val = sec_start + i + (ROW_COL_SEC_SIZE * j);
                        let val_pos = QQWing::get_possibility_index(val_index, sec_val);
                        if self.possibilities[val_pos] == 0 {
                            if box_row == UNSET_VALUE || box_row == j {
                                box_row = j;
                            } else {
                                in_one_row = false;
                            }
                        }
                    }
                }
                if in_one_row && box_row != UNSET_VALUE {
                    let mut done_something = false;
                    let row = QQWing::cell_to_row(sec_start) + box_row;
                    let row_start = row * 9;

                    for i in 0..ROW_COL_SEC_SIZE {
                        let position = row_start + i;
                        let section2 = QQWing::cell_to_section(position);
                        let val_pos = QQWing::get_possibility_index(val_index, position);
                        if section != section2 && self.possibilities[val_pos] == 0 {
                            self.possibilities[val_pos] = round;
                            done_something = true;
                        }
                    }
                    if done_something {
                        if self.log_history || self.record_history {
                            self.add_history_item(LogItem::new(
                                round,
                                LogType::PointingPairTripleRow,
                                val_index + 1,
                                row_start,
                            ));
                        }
                        return true;
                    }
                }
            }
        }
        false
    }

    fn pointing_column_reduction(&mut self, round: u8) -> bool {
        debug!("pointing_column_reduction round: {}", round);
        for val_index in 0..ROW_COL_SEC_SIZE {
            for section in 0..ROW_COL_SEC_SIZE {
                let sec_start = QQWing::section_to_first_cell(section);
                let mut in_one_col = true;
                let mut box_col = UNSET_VALUE;
                for i in 0..GRID_SIZE {
                    for j in 0..GRID_SIZE {
                        let sec_val = sec_start + i + (ROW_COL_SEC_SIZE * j);
                        let val_pos = QQWing::get_possibility_index(val_index, sec_val);
                        if self.possibilities[val_pos] == 0 {
                            if box_col == UNSET_VALUE || box_col == i {
                                box_col = i;
                            } else {
                                in_one_col = false;
                            }
                        }
                    }
                }
                if in_one_col && box_col != UNSET_VALUE {
                    let mut done_something = false;
                    let col = QQWing::cell_to_column(sec_start) + box_col;
                    let col_start = col;

                    for i in 0..ROW_COL_SEC_SIZE {
                        let position = col_start + (ROW_COL_SEC_SIZE * i);
                        let section2 = QQWing::cell_to_section(position);
                        let val_pos = QQWing::get_possibility_index(val_index, position);
                        if section != section2 && self.possibilities[val_pos] == 0 {
                            self.possibilities[val_pos] = round;
                            done_something = true;
                        }
                    }
                    if done_something {
                        if self.log_history || self.record_history {
                            self.add_history_item(LogItem::new(
                                round,
                                LogType::PointingPairTripleColumn,
                                val_index + 1,
                                col_start,
                            ));
                        }
                        return true;
                    }
                }
            }
        }
        false
    }

    fn count_possibilities(&self, position: usize) -> u32 {
        let mut count = 0;
        for val_index in 0..ROW_COL_SEC_SIZE {
            let val_pos = QQWing::get_possibility_index(val_index, position);
            if self.possibilities[val_pos] == 0 {
                count += 1;
            }
        }
        count
    }

    fn are_possibilities_same(&self, position1: usize, position2: usize) -> bool {
        for val_index in 0..ROW_COL_SEC_SIZE {
            let val_pos1 = QQWing::get_possibility_index(val_index, position1);
            let val_pos2 = QQWing::get_possibility_index(val_index, position2);
            if (self.possibilities[val_pos1] == 0 || self.possibilities[val_pos2] == 0)
                && (self.possibilities[val_pos1] != 0 || self.possibilities[val_pos2] != 0)
            {
                return false;
            }
        }
        true
    }

    fn remove_possibilities_in_one_from_two(
        &mut self,
        position1: usize,
        position2: usize,
        round: u8,
    ) -> bool {
        let mut done_something = false;
        for val_index in 0..ROW_COL_SEC_SIZE {
            let val_pos1 = QQWing::get_possibility_index(val_index, position1);
            let val_pos2 = QQWing::get_possibility_index(val_index, position2);

            if self.possibilities[val_pos1] == 0 && self.possibilities[val_pos2] == 0 {
                self.possibilities[val_pos2] = round;
                done_something = true;
            }
        }
        done_something
    }

    fn hidden_pair_in_column(&mut self, round: u8) -> bool {
        debug!("hidden_pair_in_column round: {}", round);
        for column in 0..ROW_COL_SEC_SIZE {
            for val_index in 0..ROW_COL_SEC_SIZE {
                let mut r1 = UNSET_VALUE;
                let mut r2 = UNSET_VALUE;
                let mut val_count = 0;
                for row in 0..ROW_COL_SEC_SIZE {
                    let position = QQWing::row_column_to_cell(row, column);
                    let val_pos = QQWing::get_possibility_index(val_index, position);
                    if self.possibilities[val_pos] == 0 {
                        if r1 == UNSET_VALUE || r1 == row {
                            r1 = row;
                        } else if r2 == UNSET_VALUE || r2 == row {
                            r2 = row;
                        }
                        val_count += 1;
                    }
                }
                if val_count == 2 {
                    for val_index2 in (val_index + 1)..ROW_COL_SEC_SIZE {
                        let mut r3 = UNSET_VALUE;
                        let mut r4 = UNSET_VALUE;
                        let mut val_count2 = 0;
                        for row in 0..ROW_COL_SEC_SIZE {
                            let position = QQWing::row_column_to_cell(row, column);
                            let val_pos = QQWing::get_possibility_index(val_index2, position);
                            if self.possibilities[val_pos] == 0 {
                                if r3 == UNSET_VALUE || r3 == row {
                                    r3 = row;
                                } else if r4 == UNSET_VALUE || r4 == row {
                                    r4 = row;
                                }
                                val_count2 += 1;
                            }
                        }
                        if val_count2 == 2 && r1 == r3 && r2 == r4 {
                            let mut done_something = false;
                            for val_index3 in 0..ROW_COL_SEC_SIZE {
                                if val_index3 != val_index && val_index3 != val_index2 {
                                    let position1 = QQWing::row_column_to_cell(r1, column);
                                    let position2 = QQWing::row_column_to_cell(r2, column);
                                    let val_pos1 =
                                        QQWing::get_possibility_index(val_index3, position1);
                                    let val_pos2 =
                                        QQWing::get_possibility_index(val_index3, position2);
                                    if self.possibilities[val_pos1] == 0 {
                                        self.possibilities[val_pos1] = round;
                                        done_something = true;
                                    }
                                    if self.possibilities[val_pos2] == 0 {
                                        self.possibilities[val_pos2] = round;
                                        done_something = true;
                                    }
                                }
                            }
                            if done_something {
                                if self.log_history || self.record_history {
                                    self.add_history_item(LogItem::new(
                                        round,
                                        LogType::HiddenPairColumn,
                                        val_index + 1,
                                        QQWing::row_column_to_cell(r1, column),
                                    ));
                                }
                                return true;
                            }
                        }
                    }
                }
            }
        }
        false
    }

    fn hidden_pair_in_section(&mut self, round: u8) -> bool {
        debug!("hidden_pair_in_section round: {}", round);
        for section in 0..ROW_COL_SEC_SIZE {
            for val_index in 0..ROW_COL_SEC_SIZE {
                let mut si1 = UNSET_VALUE;
                let mut si2 = UNSET_VALUE;
                let mut val_count = 0;
                for sec_ind in 0..ROW_COL_SEC_SIZE {
                    let position = QQWing::section_to_cell(section, sec_ind);
                    let val_pos = QQWing::get_possibility_index(val_index, position);
                    if self.possibilities[val_pos] == 0 {
                        if si1 == UNSET_VALUE || si1 == sec_ind {
                            si1 = sec_ind;
                        } else if si2 == UNSET_VALUE || si2 == sec_ind {
                            si2 = sec_ind;
                        }
                        val_count += 1;
                    }
                }
                if val_count == 2 {
                    for val_index2 in (val_index + 1)..ROW_COL_SEC_SIZE {
                        let mut si3 = UNSET_VALUE;
                        let mut si4 = UNSET_VALUE;
                        let mut val_count2 = 0;
                        for sec_ind in 0..ROW_COL_SEC_SIZE {
                            let position = QQWing::section_to_cell(section, sec_ind);
                            let val_pos = QQWing::get_possibility_index(val_index2, position);
                            if self.possibilities[val_pos] == 0 {
                                if si3 == UNSET_VALUE || si3 == sec_ind {
                                    si3 = sec_ind;
                                } else if si4 == UNSET_VALUE || si4 == sec_ind {
                                    si4 = sec_ind;
                                }
                                val_count2 += 1;
                            }
                        }
                        if val_count2 == 2 && si1 == si3 && si2 == si4 {
                            let mut done_something = false;
                            for val_index3 in 0..ROW_COL_SEC_SIZE {
                                if val_index3 != val_index && val_index3 != val_index2 {
                                    let position1 = QQWing::section_to_cell(section, si1);
                                    let position2 = QQWing::section_to_cell(section, si2);
                                    let val_pos1 =
                                        QQWing::get_possibility_index(val_index3, position1);
                                    let val_pos2 =
                                        QQWing::get_possibility_index(val_index3, position2);
                                    if self.possibilities[val_pos1] == 0 {
                                        self.possibilities[val_pos1] = round;
                                        done_something = true;
                                    }
                                    if self.possibilities[val_pos2] == 0 {
                                        self.possibilities[val_pos2] = round;
                                        done_something = true;
                                    }
                                }
                            }
                            if done_something {
                                if self.log_history || self.record_history {
                                    self.add_history_item(LogItem::new(
                                        round,
                                        LogType::HiddenPairSection,
                                        val_index + 1,
                                        QQWing::section_to_cell(section, si1),
                                    ));
                                }
                                return true;
                            }
                        }
                    }
                }
            }
        }
        false
    }

    fn hidden_pair_in_row(&mut self, round: u8) -> bool {
        debug!("hidden_pair_in_row round: {}", round);
        for row in 0..ROW_COL_SEC_SIZE {
            for val_index in 0..ROW_COL_SEC_SIZE {
                let mut c1 = UNSET_VALUE;
                let mut c2 = UNSET_VALUE;
                let mut val_count = 0;
                for column in 0..ROW_COL_SEC_SIZE {
                    let position = QQWing::row_column_to_cell(row, column);
                    let val_pos = QQWing::get_possibility_index(val_index, position);
                    if self.possibilities[val_pos] == 0 {
                        if c1 == UNSET_VALUE || c1 == column {
                            c1 = column;
                        } else if c2 == UNSET_VALUE || c2 == column {
                            c2 = column;
                        }
                        val_count += 1;
                    }
                }
                if val_count == 2 {
                    for val_index2 in (val_index + 1)..ROW_COL_SEC_SIZE {
                        let mut c3 = UNSET_VALUE;
                        let mut c4 = UNSET_VALUE;
                        let mut val_count2 = 0;
                        for column in 0..ROW_COL_SEC_SIZE {
                            let position = QQWing::row_column_to_cell(row, column);
                            let val_pos = QQWing::get_possibility_index(val_index2, position);
                            if self.possibilities[val_pos] == 0 {
                                if c3 == UNSET_VALUE || c3 == column {
                                    c3 = column;
                                } else if c4 == UNSET_VALUE || c4 == column {
                                    c4 = column;
                                }
                                val_count2 += 1;
                            }
                        }
                        if val_count2 == 2 && c1 == c3 && c2 == c4 {
                            let mut done_something = false;
                            for val_index3 in 0..ROW_COL_SEC_SIZE {
                                if val_index3 != val_index && val_index3 != val_index2 {
                                    let position1 = QQWing::row_column_to_cell(row, c1);
                                    let position2 = QQWing::row_column_to_cell(row, c2);
                                    let val_pos1 =
                                        QQWing::get_possibility_index(val_index3, position1);
                                    let val_pos2 =
                                        QQWing::get_possibility_index(val_index3, position2);
                                    if self.possibilities[val_pos1] == 0 {
                                        self.possibilities[val_pos1] = round;
                                        done_something = true;
                                    }
                                    if self.possibilities[val_pos2] == 0 {
                                        self.possibilities[val_pos2] = round;
                                        done_something = true;
                                    }
                                }
                            }
                            if done_something {
                                if self.log_history || self.record_history {
                                    self.add_history_item(LogItem::new(
                                        round,
                                        LogType::HiddenPairRow,
                                        val_index + 1,
                                        QQWing::row_column_to_cell(row, c1),
                                    ));
                                }
                                return true;
                            }
                        }
                    }
                }
            }
        }
        false
    }

    fn handle_naked_pairs(&mut self, round: u8) -> bool {
        debug!("handle_naked_pairs round: {}", round);
        for position in 0..BOARD_SIZE {
            let possibilities = self.count_possibilities(position);
            if possibilities == 2 {
                let row = QQWing::cell_to_row(position);
                let column = QQWing::cell_to_column(position);
                let section = QQWing::cell_to_section_start_cell(position);
                for position2 in position..BOARD_SIZE {
                    if position != position2 {
                        let possibilities2 = self.count_possibilities(position2);
                        if possibilities2 == 2 && self.are_possibilities_same(position, position2) {
                            if row == QQWing::cell_to_row(position2) {
                                let mut done_something = false;
                                for column2 in 0..ROW_COL_SEC_SIZE {
                                    let position3 = QQWing::row_column_to_cell(row, column2);
                                    if position3 != position
                                        && position3 != position2
                                        && self.remove_possibilities_in_one_from_two(
                                            position, position3, round,
                                        )
                                    {
                                        done_something = true;
                                    }
                                }
                                if done_something {
                                    if self.log_history || self.record_history {
                                        self.add_history_item(LogItem::new(
                                            round,
                                            LogType::NakedPairRow,
                                            0,
                                            position,
                                        ));
                                    }
                                    return true;
                                }
                            }
                            if column == QQWing::cell_to_column(position2) {
                                let mut done_something = false;
                                for row2 in 0..ROW_COL_SEC_SIZE {
                                    let position3 = QQWing::row_column_to_cell(row2, column);
                                    if position3 != position
                                        && position3 != position2
                                        && self.remove_possibilities_in_one_from_two(
                                            position, position3, round,
                                        )
                                    {
                                        done_something = true;
                                    }
                                }
                                if done_something {
                                    if self.log_history || self.record_history {
                                        self.add_history_item(LogItem::new(
                                            round,
                                            LogType::NakedPairColumn,
                                            0,
                                            position,
                                        ));
                                    }
                                    return true;
                                }
                            }
                            if section == QQWing::cell_to_section_start_cell(position2) {
                                let mut done_something = false;
                                let sec_start = QQWing::cell_to_section_start_cell(position);
                                for i in 0..GRID_SIZE {
                                    for j in 0..GRID_SIZE {
                                        let position3 = sec_start + i + (ROW_COL_SEC_SIZE * j);
                                        if position3 != position
                                            && position3 != position2
                                            && self.remove_possibilities_in_one_from_two(
                                                position, position3, round,
                                            )
                                        {
                                            done_something = true;
                                        }
                                    }
                                }
                                if done_something {
                                    if self.log_history || self.record_history {
                                        self.add_history_item(LogItem::new(
                                            round,
                                            LogType::NakedPairSection,
                                            0,
                                            position,
                                        ));
                                    }
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
        }
        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"
     */
    fn only_value_in_row(&mut self, round: u8) -> bool {
        debug!("only_value_in_row round: {}", round);
        for row in 0..ROW_COL_SEC_SIZE {
            for val_index in 0..ROW_COL_SEC_SIZE {
                let mut count = 0;
                let mut last_position = 0;
                for col in 0..ROW_COL_SEC_SIZE {
                    let position = (row * ROW_COL_SEC_SIZE) + col;
                    let val_pos = QQWing::get_possibility_index(val_index, position);
                    if self.possibilities[val_pos] == 0 {
                        count += 1;
                        last_position = position;
                    }
                }
                if count == 1 {
                    let value = val_index + 1;
                    if self.log_history || self.record_history {
                        self.add_history_item(LogItem::new(
                            round,
                            LogType::HiddenSingleRow,
                            value,
                            last_position,
                        ));
                    }
                    let _ = self.mark(last_position, round, value as u8).unwrap();
                    return true;
                }
            }
        }
        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"
     */
    fn only_value_in_column(&mut self, round: u8) -> bool {
        debug!("only_value_in_column round: {}", round);
        for col in 0..ROW_COL_SEC_SIZE {
            for val_index in 0..ROW_COL_SEC_SIZE {
                let mut count = 0;
                let mut last_position = 0;
                for row in 0..ROW_COL_SEC_SIZE {
                    let position = QQWing::row_column_to_cell(row, col);
                    let val_pos = QQWing::get_possibility_index(val_index, position);
                    if self.possibilities[val_pos] == 0 {
                        count += 1;
                        last_position = position;
                    }
                }
                if count == 1 {
                    let value = val_index + 1;
                    if self.log_history || self.record_history {
                        self.add_history_item(LogItem::new(
                            round,
                            LogType::HiddenSingleColumn,
                            value,
                            last_position,
                        ));
                    }
                    let _ = self.mark(last_position, round, value as u8).unwrap();
                    return true;
                }
            }
        }
        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"
     */
    fn only_value_in_section(&mut self, round: u8) -> bool {
        debug!("only_value_in_section round: {}", round);
        for sec in 0..ROW_COL_SEC_SIZE {
            let sec_pos = QQWing::section_to_first_cell(sec);
            for val_index in 0..ROW_COL_SEC_SIZE {
                let mut count = 0;
                let mut last_position = 0;
                for i in 0..GRID_SIZE {
                    for j in 0..GRID_SIZE {
                        let position = sec_pos + i + ROW_COL_SEC_SIZE * j;
                        let val_pos = QQWing::get_possibility_index(val_index, position);
                        if self.possibilities[val_pos] == 0 {
                            count += 1;
                            last_position = position;
                        }
                    }
                }
                if count == 1 {
                    let value = val_index + 1;
                    if self.log_history || self.record_history {
                        self.add_history_item(LogItem::new(
                            round,
                            LogType::HiddenSingleSection,
                            value,
                            last_position,
                        ));
                    }
                    let _ = self.mark(last_position, round, value as u8).unwrap();
                    return true;
                }
            }
        }
        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"
     */
    fn only_possibility_for_cell(&mut self, round: u8) -> bool {
        debug!("only_possibility_for_cell round: {}", round);
        for position in 0..BOARD_SIZE {
            if self.solution[position] == 0 {
                let mut count = 0;
                let mut last_value = 0;
                for val_index in 0..ROW_COL_SEC_SIZE {
                    let val_pos = QQWing::get_possibility_index(val_index, position);
                    if self.possibilities[val_pos] == 0 {
                        count += 1;
                        last_value = val_index + 1;
                    }
                }
                if count == 1 {
                    let _ = self.mark(position, round, last_value as u8).unwrap();
                    if self.log_history || self.record_history {
                        self.add_history_item(LogItem::new(
                            round,
                            LogType::Single,
                            last_value,
                            position,
                        ));
                    }
                    return true;
                }
            }
        }
        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
     */
    fn mark(&mut self, position: usize, round: u8, value: u8) -> Result<bool, QQWingError> {
        debug!(
            "Mark position: {}, round: {}, value: {}",
            position, round, value
        );
        if self.solution[position] != 0 {
            return Err(QQWingError::PositionAlreadyMarked);
        }
        if self.solution_round[position] != 0 {
            return Err(QQWingError::PositionAlreadyMarked);
        }

        let val_index = value - 1;
        self.solution[position] = value;

        let poss_ind = QQWing::get_possibility_index(val_index as usize, position);
        if self.possibilities[poss_ind] != 0 {
            return Err(QQWingError::PositionAlreadyMarked);
        }

        // Take this value out of the possibilities for everything in the row
        self.solution_round[position] = round;
        let row_start = QQWing::cell_to_row(position) * ROW_COL_SEC_SIZE;
        for col in 0..ROW_COL_SEC_SIZE {
            let row_val = row_start + col;
            let val_pos = QQWing::get_possibility_index(val_index as usize, row_val);
            // System.out.println("Row Start: "+row_start+" Row Value: "+rowVal+" Value Position: "+val_pos);
            if self.possibilities[val_pos] == 0 {
                self.possibilities[val_pos] = round;
            }
        }

        // Take this value out of the possibilities for everything in the column
        let col_start = QQWing::cell_to_column(position);
        for i in 0..ROW_COL_SEC_SIZE {
            let col_val = col_start + (ROW_COL_SEC_SIZE * i);
            let val_pos = QQWing::get_possibility_index(val_index as usize, col_val);
            // System.out.println("Col Start: "+col_start+" Col Value: "+colVal+" Value Position: "+val_pos);
            if self.possibilities[val_pos] == 0 {
                self.possibilities[val_pos] = round;
            }
        }

        // Take this value out of the possibilities for everything in section
        let sec_start = QQWing::cell_to_section_start_cell(position);
        for i in 0..GRID_SIZE {
            for j in 0..GRID_SIZE {
                let sec_val = sec_start + i + (ROW_COL_SEC_SIZE * j);
                let val_pos = QQWing::get_possibility_index(val_index as usize, sec_val);
                // System.out.println("Sec Start: "+sec_start+" Sec Value: "+sec_val+" Value Position: "+val_pos);
                if self.possibilities[val_pos] == 0 {
                    self.possibilities[val_pos] = round;
                }
            }
        }

        // This position itself is determined, it should have possibilities.
        for val_index in 0..ROW_COL_SEC_SIZE {
            let val_pos = QQWing::get_possibility_index(val_index as usize, position);
            if self.possibilities[val_pos] == 0 {
                self.possibilities[val_pos] = round;
            }
        }
        Ok(true)
    }

    /**

     * print the given BOARD_SIZEd array of ints as a sudoku puzzle. Use print
     * options from member variables.
     */
    fn print(&self, sudoku: [u8; 81]) {
        println!("{}", self.puzzle_to_string(sudoku));
    }

    fn puzzle_to_string(&self, sudoku: [u8; 81]) -> String {
        let mut sb = String::new();
        for i in 0..BOARD_SIZE {
            if self.print_style == PrintStyle::READABLE {
                sb.push_str(" ");
            }
            if sudoku[i] == 0 {
                sb.push_str(".");
            } else {
                sb.push_str(sudoku[i].to_string().as_str());
            }
            if i == BOARD_SIZE - 1 {
                if self.print_style == PrintStyle::CSV {
                    sb.push_str(",");
                } else {
                    sb.push_str(NL);
                }
                if self.print_style == PrintStyle::READABLE
                    || self.print_style == PrintStyle::COMPACT
                {
                    sb.push_str(NL);
                }
            } else if i % ROW_COL_SEC_SIZE == ROW_COL_SEC_SIZE - 1 {
                if self.print_style == PrintStyle::READABLE
                    || self.print_style == PrintStyle::COMPACT
                {
                    sb.push_str(NL);
                }
                if i % SEC_GROUP_SIZE == SEC_GROUP_SIZE - 1 {
                    if self.print_style == PrintStyle::READABLE {
                        sb.push_str("-------|-------|-------");
                        sb.push_str(NL);
                    }
                }
            } else if i % GRID_SIZE == GRID_SIZE - 1 {
                if self.print_style == PrintStyle::READABLE {
                    sb.push_str(" |");
                }
            }
        }
        sb
    }

    /// Print any stats we were able to gather while solving the puzzle.
    pub fn get_stats(&self) -> String {
        let mut sb = String::new();
        let given_count = self.get_given_count();
        let single_count = self.get_single_count();
        let hidden_single_count = self.get_hidden_single_count();
        let naked_pair_count = self.get_naked_pair_count();
        let hidden_pair_count = self.get_hidden_pair_count();
        let pointing_pair_triple_count = self.get_pointing_pair_triple_count();
        let box_reduction_count = self.get_box_line_reduction_count();
        let guess_count = self.get_guess_count();
        let backtrack_count = self.get_backtrack_count();
        let difficulty_string = self.get_difficulty();
        if self.print_style == PrintStyle::CSV {
            sb.push_str(format!("{:?}", difficulty_string).as_str());
            sb.push_str(",");
            sb.push_str(given_count.to_string().as_str());
            sb.push_str(",");
            sb.push_str(single_count.to_string().as_str());
            sb.push_str(",");
            sb.push_str(hidden_single_count.to_string().as_str());
            sb.push_str(",");
            sb.push_str(naked_pair_count.to_string().as_str());
            sb.push_str(",");
            sb.push_str(hidden_pair_count.to_string().as_str());
            sb.push_str(",");
            sb.push_str(pointing_pair_triple_count.to_string().as_str());
            sb.push_str(",");
            sb.push_str(box_reduction_count.to_string().as_str());
            sb.push_str(",");
            sb.push_str(guess_count.to_string().as_str());
            sb.push_str(",");
            sb.push_str(backtrack_count.to_string().as_str());
            sb.push_str(",");
        } else {
            sb.push_str("Difficulty: ");
            sb.push_str(format!("{:?}", difficulty_string).as_str());
            sb.push_str(NL);
            sb.push_str("Number of Givens: ");
            sb.push_str(given_count.to_string().as_str());
            sb.push_str(NL);
            sb.push_str("Number of Singles: ");
            sb.push_str(single_count.to_string().as_str());
            sb.push_str(NL);
            sb.push_str("Number of Hidden Singles: ");
            sb.push_str(hidden_single_count.to_string().as_str());
            sb.push_str(NL);
            sb.push_str("Number of Naked Pairs: ");
            sb.push_str(naked_pair_count.to_string().as_str());
            sb.push_str(NL);
            sb.push_str("Number of Hidden Pairs: ");
            sb.push_str(hidden_pair_count.to_string().as_str());
            sb.push_str(NL);
            sb.push_str("Number of Pointing Pairs/Triples: ");
            sb.push_str(pointing_pair_triple_count.to_string().as_str());
            sb.push_str(NL);
            sb.push_str("Number of Box/Line Intersections: ");
            sb.push_str(box_reduction_count.to_string().as_str());
            sb.push_str(NL);
            sb.push_str("Number of Guesses: ");
            sb.push_str(guess_count.to_string().as_str());
            sb.push_str(NL);
            sb.push_str("Number of Backtracks: ");
            sb.push_str(backtrack_count.to_string().as_str());
            sb.push_str(NL);
        }
        sb
    }

    /**

     * Print the sudoku puzzle.
     */
    pub fn print_puzzle(&self) {
        self.print(self.puzzle);
    }

    /**

     * Given a vector of LogItems, determine how many log items in the vector
     * are of the specified type.
     */
    fn get_log_count(v: &Vec<LogItem>, logtype: LogType) -> usize {
        let mut count = 0;
        for i in 0..v.len() {
            if v[i].log_type == logtype {
                count += 1;
            }
        }
        return count;
    }

    fn get_random_symmetry() -> Symmetry {
        let values = [
            Symmetry::NONE,
            Symmetry::ROTATE90,
            Symmetry::ROTATE180,
            Symmetry::MIRROR,
            Symmetry::FLIP,
            Symmetry::RANDOM,
        ];
        // not the first and last value which are NONE and RANDOM
        values[(random::<usize>() % (values.len() - 1)) + 1].clone()
    }

    /**

     * Given a value for a cell (0-8) and a cell number (0-80) calculate the
     * offset into the possibility array (0-728).
     */
    pub(crate) fn get_possibility_index(value_index: usize, cell: usize) -> usize {
        value_index + (ROW_COL_SEC_SIZE * cell)
    }

    /**

     * Given the index of a cell (0-80) calculate the row (0-8) in which it
     * resides.
     */
    pub(crate) fn cell_to_row(cell: usize) -> usize {
        cell / ROW_COL_SEC_SIZE
    }

    /**

     * Given the index of a cell (0-80) calculate the column (0-8) in which that
     * cell resides.
     */
    pub(crate) fn cell_to_column(cell: usize) -> usize {
        cell % ROW_COL_SEC_SIZE
    }

    /**

     * Given the index of a cell (0-80) calculate the section (0-8) in which it
     * resides.
     */
    pub(crate) fn cell_to_section(cell: usize) -> usize {
        (cell / SEC_GROUP_SIZE * GRID_SIZE) + (QQWing::cell_to_column(cell) / GRID_SIZE)
    }

    /**

     * Given the index of a cell (0-80) calculate the cell (0-80) that is the
     * upper left start cell of that section.
     */
    pub(crate) fn cell_to_section_start_cell(cell: usize) -> usize {
        (cell / SEC_GROUP_SIZE * SEC_GROUP_SIZE)
            + (QQWing::cell_to_column(cell) / GRID_SIZE * GRID_SIZE)
    }

    /**

     * Given a row (0-8) and a column (0-8) calculate the cell (0-80).
     */
    pub(crate) fn row_column_to_cell(row: usize, column: usize) -> usize {
        row * ROW_COL_SEC_SIZE + column
    }

    /**

     * Given a section (0-8) calculate the first cell (0-80) of that section.
     */
    pub(crate) fn section_to_first_cell(section: usize) -> usize {
        (section % GRID_SIZE * GRID_SIZE) + (section / GRID_SIZE * SEC_GROUP_SIZE)
    }

    /**

     * Given a section (0-8) and an offset into that section (0-8) calculate the
     * cell (0-80)
     */
    pub(crate) fn section_to_cell(section: usize, offset: usize) -> usize {
        QQWing::section_to_first_cell(section)
            + ((offset / GRID_SIZE) * ROW_COL_SEC_SIZE)
            + (offset % GRID_SIZE)
    }
}

#[derive(Debug, PartialEq, Clone, EnumString, EnumIter)]
pub enum PrintStyle {
    ONELINE,
    COMPACT,
    READABLE,
    CSV,
}