Пример #1
0
        public void FindSinglesTest()
        {
            Board board = new Board();

            board.ReplaceCell(new Cell(0, 0, new List <int> {
                3
            }));
            board.ReplaceCell(new Cell(0, 1, new List <int> {
                3, 4
            }));
            board.ReplaceCell(new Cell(0, 2, new List <int> {
                5, 3
            }));
            board.ReplaceCell(new Cell(0, 3, new List <int> {
                5
            }));
            board.ReplaceCell(new Cell(0, 4, new List <int> {
                6, 3
            }));
            board.ReplaceCell(new Cell(0, 5, new List <int> {
                7, 3
            }));
            board.ReplaceCell(new Cell(0, 6, new List <int> {
                5, 6
            }));
            board.ReplaceCell(new Cell(0, 7, new List <int> {
                5, 7
            }));
            board.ReplaceCell(new Cell(0, 8, new List <int> {
                5, 3
            }));
            OnlyOptionInColumnSolver solver = new OnlyOptionInColumnSolver(board);

            List <Cell> result = solver.Find();

            Assert.Single(result);
            Assert.Equal(4, result.First().Number);
        }
Пример #2
0
        private bool SolveHelper(Board board)
        {
            // Create Reducers
            OtherRowsAreBlockedReducer    otherRowsAreBlockedReducer    = new OtherRowsAreBlockedReducer(board);
            OtherColumnsAreBlockedReducer otherColumnsAreBlockedReducer = new OtherColumnsAreBlockedReducer(board);
            EqualOptionsInRowReducer      equalOptionsInRowReducer      = new EqualOptionsInRowReducer(board);
            EqualOptionsInColumnReducer   equalOptionsInColumnReducer   = new EqualOptionsInColumnReducer(board);
            EqualOptionsInQuadReducer     equalOptionsInQuadReducer     = new EqualOptionsInQuadReducer(board);

            // Create Solvers
            OnlyCandidateInCellSolver onlyCandidateInCellSolver = new OnlyCandidateInCellSolver(board);
            OnlyOptionInRowSolver     onlyOptionInRowSolver     = new OnlyOptionInRowSolver(board);
            OnlyOptionInColumnSolver  onlyOptionInColumnSolver  = new OnlyOptionInColumnSolver(board);
            OnlyOptionInQuadSolver    onlyOptionInQuadSolver    = new OnlyOptionInQuadSolver(board);

            while (true)
            {
                iterations++;

                bool repeatLoop = false;

                // Limit insertion candidates
                repeatLoop = repeatLoop || otherRowsAreBlockedReducer.Run();
                repeatLoop = repeatLoop || otherColumnsAreBlockedReducer.Run();
                repeatLoop = repeatLoop || equalOptionsInRowReducer.Run();
                repeatLoop = repeatLoop || equalOptionsInColumnReducer.Run();
                repeatLoop = repeatLoop || equalOptionsInQuadReducer.Run();

                // Fill cells
                repeatLoop = repeatLoop || onlyCandidateInCellSolver.Run();
                repeatLoop = repeatLoop || onlyOptionInRowSolver.Run();
                repeatLoop = repeatLoop || onlyOptionInColumnSolver.Run();
                repeatLoop = repeatLoop || onlyOptionInQuadSolver.Run();

                if (repeatLoop)
                {
                    continue;
                }

                if (board.UnknownValues() == 0)
                {
                    return(true);
                }
                else if (board.BacktrackLevel < maxBacktrackLevel)
                {
                    foreach (Cell cell in board.AllCells())
                    {
                        foreach (int candidate in cell.Candidates)
                        {
                            Board newBoard    = new Board(board);
                            Cell  assumedCell = new Cell(cell);
                            assumedCell.Number = candidate;
                            newBoard.ReplaceCell(assumedCell);

                            if (SolveHelper(newBoard))
                            {
                                assumedCells.Add(assumedCell);

                                foreach (Cell solvedCell in newBoard.AllCells())
                                {
                                    board.ReplaceCell(solvedCell);
                                }

                                return(true);
                            }
                        }
                    }
                }

                return(false);
            }
        }
Пример #3
0
 public void ConstructorTest()
 {
     Board board = new Board();
     OnlyOptionInColumnSolver solver = new OnlyOptionInColumnSolver(board);
 }