Ejemplo n.º 1
0
        public void BruteForce_Recursive_FullSolve()
        {
            string TestType = "Recursive Brute force ";
            bool   MultipleSolutionsFound = false;

            //all right, now the real test
            List <Cell> SolvedCells   = BoardTest.SolvedSudokuPuzzle1();
            Board       UnSolvedBoard = new Board(BoardTest.UnsolvedSudokuPuzzle1());

            BruteForceSolver Solver = new BruteForceSolver();

            Solver.RecursiveBruteForceSolve(ref UnSolvedBoard, ref MultipleSolutionsFound);



            Assert.IsTrue(BoardTest.CellListsAreEqual(UnSolvedBoard.Cells, SolvedCells),
                          TestType + "failed to solve the first test board, its result: " + UnSolvedBoard.PrintBoard());
            Assert.IsFalse(MultipleSolutionsFound,
                           TestType + "said there were multiple solutions on a board with just one");



            SolvedCells   = BoardTest.SolvedSudokuPuzzle2();
            UnSolvedBoard = new Board(BoardTest.UnsolvedSudokuPuzzle2());

            Solver.RecursiveBruteForceSolve(ref UnSolvedBoard, ref MultipleSolutionsFound);


            Assert.IsTrue(BoardTest.CellListsAreEqual(UnSolvedBoard.Cells, SolvedCells),
                          TestType + "failed to solve the second test board, its result: " + UnSolvedBoard.PrintBoard());
            Assert.IsFalse(MultipleSolutionsFound,
                           TestType + "said there were multiple solutions on a board with just one");
        }
Ejemplo n.º 2
0
        public void BruteForce_SingleState_FullSolve()
        {
            //all right, now the real test
            List <Cell> SolvedCells   = BoardTest.SolvedSudokuPuzzle1();
            Board       UnSolvedBoard = new Board(BoardTest.UnsolvedSudokuPuzzle1());

            BruteForceSolver Solver = new BruteForceSolver();

            Solver.SingleStateBruteForceSolve(ref UnSolvedBoard);



            Assert.IsTrue(BoardTest.CellListsAreEqual(UnSolvedBoard.Cells, SolvedCells),
                          "Brute force failed to solve the first test board, its result: " + UnSolvedBoard.PrintBoard());



            SolvedCells   = BoardTest.SolvedSudokuPuzzle2();
            UnSolvedBoard = new Board(BoardTest.UnsolvedSudokuPuzzle2());

            Solver.SingleStateBruteForceSolve(ref UnSolvedBoard);


            Assert.IsTrue(BoardTest.CellListsAreEqual(UnSolvedBoard.Cells, SolvedCells),
                          "Brute force failed to solve the second test board, its result: " + UnSolvedBoard.PrintBoard());
        }
Ejemplo n.º 3
0
        public void BruteForce_Recursive_SimpleTest()
        {
            //For testing purposes, if we need to zero out a cell, we reassign the value in the list to a new "zero cell"
            //When lists of cells are initialized, if they have values filled in, those values are considered non-changeable
            //which is good, we want that, just for the purposes of fiddling with data for tests, that's less convenient

            bool MultipleSolutionsFound = false;

            //make sure test does nothing on an already-solved board
            List <Cell>      SolvedCells = BoardTest.SolvedSudokuPuzzle1();
            Board            SolvedBoard = new Board(SolvedCells);
            BruteForceSolver Solver      = new BruteForceSolver();

            Solver.RecursiveBruteForceSolve(ref SolvedBoard, ref MultipleSolutionsFound);

            Assert.IsTrue(BoardTest.CellListsAreEqual(SolvedBoard.Cells, SolvedCells),
                          "Brute force solver changed values on an already-solved board");
            Assert.IsFalse(MultipleSolutionsFound,
                           "Recursive Brute force solver said there were multiple solutions on an already-solved board");

            //now let's blank out one value on our board, and see the brute force solver fill it in
            Board MissingOneBoard = new Board(SolvedCells);
            int   OriginalValue   = MissingOneBoard.Cells[8].Value;

            MissingOneBoard.Cells[8] = new Cell(0);
            Solver.RecursiveBruteForceSolve(ref MissingOneBoard, ref MultipleSolutionsFound);

            Assert.IsTrue(MissingOneBoard.Cells[8].Value == OriginalValue,
                          "Brute force solver failed to fill in 1 value missing from completed board");
            Assert.IsFalse(MultipleSolutionsFound,
                           "Recursive Brute force solver said there were multiple solutions on a board missing 1 value");

            //now let's blank out three values in a row on our board, and see the brute force solver fill them in
            //this is an incredibly easy sudoku

            Board MissingThreeBoard = new Board(SolvedCells);
            int   OriginalValue1    = MissingOneBoard.Cells[6].Value;

            MissingOneBoard.Cells[6] = new Cell(0);
            int OriginalValue2 = MissingOneBoard.Cells[7].Value;

            MissingOneBoard.Cells[7] = new Cell(0);
            int OriginalValue3 = MissingOneBoard.Cells[8].Value;

            MissingOneBoard.Cells[8] = new Cell(0);
            Solver.RecursiveBruteForceSolve(ref MissingOneBoard, ref MultipleSolutionsFound);

            Assert.IsTrue(MissingOneBoard.Cells[6].Value == OriginalValue1 &&
                          MissingOneBoard.Cells[7].Value == OriginalValue2 &&
                          MissingOneBoard.Cells[8].Value == OriginalValue3,
                          "Brute force solver failed to fill in 3 values in a row missing from completed board: " +
                          OriginalValue1 + " is now " + MissingOneBoard.Cells[6].Value + ", " +
                          OriginalValue2 + " is now " + MissingOneBoard.Cells[7].Value + ", " +
                          OriginalValue3 + " is now " + MissingOneBoard.Cells[8].Value);
            Assert.IsFalse(MultipleSolutionsFound,
                           "Recursive Brute force solver said there were multiple solutions on a board missing 3 values");


            //Let's blank out three more random cells
            OriginalValue1            = MissingOneBoard.Cells[18].Value;
            MissingOneBoard.Cells[18] = new Cell(0);
            OriginalValue2            = MissingOneBoard.Cells[40].Value;
            MissingOneBoard.Cells[40] = new Cell(0);
            OriginalValue3            = MissingOneBoard.Cells[78].Value;
            MissingOneBoard.Cells[78] = new Cell(0);
            Solver.RecursiveBruteForceSolve(ref MissingOneBoard, ref MultipleSolutionsFound);

            Assert.IsTrue(MissingOneBoard.Cells[18].Value == OriginalValue1 &&
                          MissingOneBoard.Cells[40].Value == OriginalValue2 &&
                          MissingOneBoard.Cells[78].Value == OriginalValue3,
                          "Brute force solver failed to fill in 3 scattered values missing from completed board: " +
                          OriginalValue1 + " is now " + MissingOneBoard.Cells[18].Value + ", " +
                          OriginalValue2 + " is now " + MissingOneBoard.Cells[40].Value + ", " +
                          OriginalValue3 + " is now " + MissingOneBoard.Cells[78].Value);
            Assert.IsFalse(MultipleSolutionsFound,
                           "Recursive Brute force solver said there were multiple solutions on a board missing 3 scattered values");
        }