Esempio n. 1
0
        /**
         * Solves current puzzle.
         *
         * @see SudokuSolver#findAllSolutions()
         */
        private void solveFindAll()
        {
            solver = new SudokuSolver(puzzle);
            setSolverOptions();
            int solutionsNumber = solver.findAllSolutions();

            JanetConsole.println(">>>>>>>> Solutions found: " + solutionsNumber);
            if (solutionsNumber > 0)
            {
                List <SudokuBoard> solutions = solver.getAllSolutionsList();
                for (int i = 0; i < solutionsNumber; i++)
                {
                    SudokuBoard solution = solutions[i];
                    JanetConsole.println(">>>>>    Solution nr: " + i + "/" + solutionsNumber);
                    JanetConsole.println(">>>>>        Path nr: " + solution.pathNumber);
                    JanetConsole.println(">>>>> Computing time: " + solver.getComputingTime() + " s.");
                    SudokuStore.consolePrintBoard(solution.board);
                    JanetConsole.println(">>>>>");
                    JanetConsole.println(">>>>> Hit enter o to continue (non empty line will cancel).");
                    String line = JanetConsole.readLine();
                    if (line.Length > 0)
                    {
                        break;
                    }
                }
            }
            else
            {
                JanetConsole.println(solver.getMessages());
            }
        }
Esempio n. 2
0
        /**
         * Start the Janet-Sudoku Tutorial code.
         * @param args    No arguments are considered.
         */
        public static void Start()
        {
            String tmpDir = FileX.getTmpDir();
            {
                /*
                 * Simple sudoku generation.
                 */
                SudokuStore.consolePrintln("");
                SudokuStore.consolePrintln("Simple sudoku generation.");
                SudokuGenerator sg = new SudokuGenerator();
                int[,] puzzle = sg.generate();
                SudokuStore.consolePrintBoard(puzzle);
            }
            {
                /*
                 * Simple sudoku generation + parameters.
                 */
                SudokuStore.consolePrintln("");
                SudokuStore.consolePrintln("Simple sudoku generation + parameters.");
                SudokuGenerator sg = new SudokuGenerator(SudokuGenerator.PARAM_GEN_RND_BOARD);
                int[,] puzzle = sg.generate();
                SudokuStore.consolePrintBoard(puzzle);
            }
            {
                /*
                 * Simple sudoku generation + puzzle rating.
                 */
                SudokuStore.consolePrintln("");
                SudokuStore.consolePrintln("Simple sudoku generation + puzzle rating.");
                SudokuGenerator sg = new SudokuGenerator();
                int[,] puzzle = sg.generate();
                int rating = SudokuStore.calculatePuzzleRating(puzzle);
                SudokuStore.consolePrintBoard(puzzle);
                SudokuStore.consolePrintln("Puzzle rating: " + rating);
            }
            {
                /*
                 * Solving sudoku example.
                 */
                SudokuStore.consolePrintln("");
                SudokuStore.consolePrintln("Solving sudoku example.");
                SudokuSolver ss = new SudokuSolver(SudokuPuzzles.PUZZLE_EXAMPLE_001);
                SudokuStore.consolePrintBoard(ss.getBoard());
                ss.solve();
                SudokuStore.consolePrintBoard(ss.getSolvedBoard());
            }
            {
                /*
                 * Saving board examples
                 */
                SudokuStore.consolePrintln("");
                SudokuStore.consolePrintln("Saving board examples " + tmpDir);
                SudokuStore.saveBoard(SudokuPuzzles.PUZZLE_EXAMPLE_001, tmpDir + "sudoku-board-ex-1.txt");
                SudokuStore.saveBoard(SudokuPuzzles.PUZZLE_EXAMPLE_001, tmpDir + "sudoku-board-ex-2.txt", "This is a head comment");
                SudokuStore.saveBoard(SudokuPuzzles.PUZZLE_EXAMPLE_001, tmpDir + "sudoku-board-ex-3.txt", "This is a head comment", "And a tail comment");
                SudokuSolver ss = new SudokuSolver(1);
                ss.solve();
                ss.saveSolvedBoard(tmpDir + "sudoku-board-ex-sol.txt", "Solution for the PUZZLE_EXAMPLE_001");
            }

            /*
             * And many other staff provided by the library :-)
             */
        }
Esempio n. 3
0
        /**
         * Test scenario implementation.
         * @param testId        Test id.
         * @param threadId      Thread id.
         * @return              True is test successful, otherwise false.
         */
        static bool runTest(int testId, int threadId)
        {
            SudokuSolver    solverGen, solverInit;
            SudokuGenerator g;
            int             solUnq;

            int[,] genPuzzle;
            int[,] solvedGen, solvedInit, initBoard;
            bool testResult = true;

            switch (testId)
            {
            case 0:
                for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++)
                {
                    g         = new SudokuGenerator(example, SudokuGenerator.PARAM_DO_NOT_TRANSFORM);
                    genPuzzle = g.generate();
                    solverGen = new SudokuSolver(genPuzzle);
                    ErrorCodes.consolePrintlnIfError(solverGen.solve());
                    solverInit = new SudokuSolver(example);
                    ErrorCodes.consolePrintlnIfError(solverInit.solve());
                    solUnq = solverGen.checkIfUniqueSolution();
                    ErrorCodes.consolePrintlnIfError(solUnq);
                    solvedGen  = solverGen.getSolvedBoard();
                    solvedInit = solverInit.getSolvedBoard();
                    if ((solUnq == SudokuSolver.SOLUTION_UNIQUE) && (SudokuStore.boardsAreEqual(solvedGen, solvedInit) == true))
                    {
                        //SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by example number, example: " + example + ", solution unique and correct: YES, time (g, s1, s2): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., " + solverInit.getComputingTime() + " s.");
                    }
                    else
                    {
                        testResult = false;
                        SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by example number, example: " + example + ", solution unique and correct: NO, time (g, s1, s2): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., " + solverInit.getComputingTime() + " s.");
                        Console.WriteLine("Initial board");
                        SudokuStore.consolePrintBoard(SudokuStore.getPuzzleExample(example));
                        Console.WriteLine("Generated puzzle");
                        SudokuStore.consolePrintBoard(genPuzzle);
                        Console.WriteLine("Solved initial board");
                        SudokuStore.consolePrintBoard(solvedInit);
                        Console.WriteLine("Solved generated puzzle");
                        SudokuStore.consolePrintBoard(solvedGen);
                    }
                }
                break;

            case 1:
                for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++)
                {
                    initBoard = SudokuStore.getPuzzleExample(example);
                    g         = new SudokuGenerator(initBoard, SudokuGenerator.PARAM_DO_NOT_TRANSFORM, SudokuGenerator.PARAM_DO_NOT_SOLVE);
                    genPuzzle = g.generate();
                    solverGen = new SudokuSolver(genPuzzle);
                    ErrorCodes.consolePrintlnIfError(solverGen.solve());
                    solverInit = new SudokuSolver(initBoard);
                    ErrorCodes.consolePrintlnIfError(solverInit.solve());
                    solUnq = solverGen.checkIfUniqueSolution();
                    ErrorCodes.consolePrintlnIfError(solUnq);
                    solvedGen  = solverGen.getSolvedBoard();
                    solvedInit = solverInit.getSolvedBoard();
                    if ((solUnq == SudokuSolver.SOLUTION_UNIQUE) && (SudokuStore.boardsAreEqual(solvedGen, solvedInit) == true))
                    {
                        //SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by example board, example: " + example + ", solution unique and correct: YES, time (g, s1, s2): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., " + solverInit.getComputingTime() + " s.");
                    }
                    else
                    {
                        testResult = false;
                        SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by example board, example: " + example + ", solution unique and correct: NO, time (g, s1, s2): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., " + solverInit.getComputingTime() + " s.");
                        Console.WriteLine("Initial board");
                        SudokuStore.consolePrintBoard(initBoard);
                        Console.WriteLine("Generated puzzle");
                        SudokuStore.consolePrintBoard(genPuzzle);
                        Console.WriteLine("Solved initial board");
                        SudokuStore.consolePrintBoard(solvedInit);
                        Console.WriteLine("Solved generated puzzle");
                        SudokuStore.consolePrintBoard(solvedGen);
                    }
                }
                break;

            case 2:
                for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++)
                {
                    g         = new SudokuGenerator(example);
                    genPuzzle = g.generate();
                    solverGen = new SudokuSolver(genPuzzle);
                    ErrorCodes.consolePrintlnIfError(solverGen.solve());
                    solUnq = solverGen.checkIfUniqueSolution();
                    ErrorCodes.consolePrintlnIfError(solUnq);
                    solvedGen = solverGen.getSolvedBoard();
                    if ((solUnq == SudokuSolver.SOLUTION_UNIQUE) && (SudokuStore.checkPuzzle(genPuzzle) == true))
                    {
                        //SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by example number, example: " + example + ", solution unique and correct: YES, time (g, s): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., ");
                    }
                    else
                    {
                        testResult = false;
                        SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by example number, example: " + example + ", solution unique and correct: NO, time (g, s): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., ");
                        Console.WriteLine("Generated puzzle");
                        SudokuStore.consolePrintBoard(genPuzzle);
                        Console.WriteLine("Solved generated puzzle");
                        SudokuStore.consolePrintBoard(solvedGen);
                    }
                }
                break;

            case 3:
                for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++)
                {
                    initBoard = SudokuStore.getPuzzleExample(example);
                    g         = new SudokuGenerator(initBoard, SudokuGenerator.PARAM_DO_NOT_SOLVE);
                    genPuzzle = g.generate();
                    solverGen = new SudokuSolver(genPuzzle);
                    ErrorCodes.consolePrintlnIfError(solverGen.solve());
                    solUnq = solverGen.checkIfUniqueSolution();
                    ErrorCodes.consolePrintlnIfError(solUnq);
                    solvedGen = solverGen.getSolvedBoard();
                    if ((solUnq == SudokuSolver.SOLUTION_UNIQUE) && (SudokuStore.checkPuzzle(genPuzzle) == true))
                    {
                        //SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by example board, example: " + example + ", solution unique and correct: YES, time (g, s): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., ");
                    }
                    else
                    {
                        testResult = false;
                        SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by example board, example: " + example + ", solution unique and correct: NO, time (g, s): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., ");
                        Console.WriteLine("Generated puzzle");
                        SudokuStore.consolePrintBoard(genPuzzle);
                        Console.WriteLine("Solved initial board");
                        SudokuStore.consolePrintBoard(solvedGen);
                    }
                }
                break;

            case 5:
                initBoard = SudokuPuzzles.PUZZLE_EMPTY;
                g         = new SudokuGenerator(initBoard);
                genPuzzle = g.generate();
                solverGen = new SudokuSolver(genPuzzle);
                ErrorCodes.consolePrintlnIfError(solverGen.solve());
                solUnq = solverGen.checkIfUniqueSolution();
                ErrorCodes.consolePrintlnIfError(solUnq);
                solvedGen = solverGen.getSolvedBoard();
                if ((solUnq == SudokuSolver.SOLUTION_UNIQUE) && (SudokuStore.checkPuzzle(genPuzzle) == true))
                {
                    //SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by empty board, solution unique and correct: YES, time (g, s): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., ");
                }
                else
                {
                    testResult = false;
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by empty board, solution unique and correct: NO, time (g, s): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., ");
                    Console.WriteLine("Generated puzzle");
                    SudokuStore.consolePrintBoard(genPuzzle);
                    Console.WriteLine("Solved initial board");
                    SudokuStore.consolePrintBoard(solvedGen);
                }
                break;

            case 6:
                initBoard = SudokuPuzzles.PUZZLE_EMPTY;
                g         = new SudokuGenerator(initBoard, SudokuGenerator.PARAM_DO_NOT_SOLVE);
                if (g.getGeneratorState() == SudokuGenerator.GENERATOR_INIT_FAILED)
                {
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by empty board, solution unique and correct: YES, time (g, s): ");
                }
                else
                {
                    testResult = false;
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by empty board, solution unique and correct: NO, time (g, s): ");
                    SudokuStore.consolePrintln("Generator state: " + g.getGeneratorState());
                    Console.WriteLine(g.getMessages());
                }
                break;

            case 7:
                initBoard = SudokuPuzzles.PUZZLE_ERROR;
                g         = new SudokuGenerator(initBoard, SudokuGenerator.PARAM_DO_NOT_SOLVE);
                if (g.getGeneratorState() == SudokuGenerator.GENERATOR_INIT_FAILED)
                {
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by PUZZLE_ERROR + PARAM_DO_NOT_SOLVE, init failed: YES.");
                }
                else
                {
                    testResult = false;
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by PUZZLE_ERROR + PARAM_DO_NOT_SOLVE, init failed: NO.");
                    SudokuStore.consolePrintln("Generator state: " + g.getGeneratorState());
                    Console.WriteLine(g.getMessages());
                }
                break;

            case 8:
                initBoard = SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION;
                g         = new SudokuGenerator(initBoard);
                genPuzzle = g.generate();
                solverGen = new SudokuSolver(genPuzzle);
                ErrorCodes.consolePrintlnIfError(solverGen.solve());
                solUnq = solverGen.checkIfUniqueSolution();
                ErrorCodes.consolePrintlnIfError(solUnq);
                solvedGen = solverGen.getSolvedBoard();
                if ((solUnq == SudokuSolver.SOLUTION_UNIQUE) && (SudokuStore.checkPuzzle(genPuzzle) == true))
                {
                    //SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by empty board, solution unique and correct: YES, time (g, s): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., ");
                }
                else
                {
                    testResult = false;
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by empty board, solution unique and correct: NO, time (g, s): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., ");
                    Console.WriteLine("Generated puzzle");
                    SudokuStore.consolePrintBoard(genPuzzle);
                    Console.WriteLine("Solved initial board");
                    SudokuStore.consolePrintBoard(solvedGen);
                }
                break;

            case 9:
                initBoard = SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION;
                g         = new SudokuGenerator(initBoard, SudokuGenerator.PARAM_DO_NOT_SOLVE);
                if (g.getGeneratorState() == SudokuGenerator.GENERATOR_INIT_FAILED)
                {
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by PUZZLE_NON_UNIQUE_SOLUTION + PARAM_DO_NOT_SOLVE, init failed: YES.");
                }
                else
                {
                    testResult = false;
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by PUZZLE_NON_UNIQUE_SOLUTION + PARAM_DO_NOT_SOLVE, init failed: NO.");
                    SudokuStore.consolePrintln("Generator state: " + g.getGeneratorState());
                    Console.WriteLine(g.getMessages());
                }
                break;
            }
            if (testResult == true)
            {
                SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + " >>>>>>>>>>>>>>>>>>>>>> SudokuGenerator, result: OK");
            }
            else
            {
                SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + " >>>>>>>>>>>>>>>>>>>>>> SudokuGenerator, result: ERROR");
            }
            return(testResult);
        }
Esempio n. 4
0
 /**
  * Print current puzzle to the console
  */
 internal void consolePrintPuzzle()
 {
     JanetConsole.println();
     JanetConsole.print(">>> Random seed option - empty cells = " + rndSeedOnCells + ", free digits = " + rndSeedOnDigits);
     SudokuStore.consolePrintBoard(puzzle);
 }