예제 #1
0
 /**
  * Constructor based on the sudoku board
  * provided list of strings.
  *
  * @param boardDefinition  Board definition as list of strings
  *                         (each list entry as separate row).
  * @param parameters       Optional parameters
  *
  * @see #PARAM_DO_NOT_SOLVE
  * @see #PARAM_DO_NOT_TRANSFORM
  * @see #PARAM_GEN_RND_BOARD
  */
 public SudokuGenerator(List <String> boardDefinition, params char[] parameters)
 {
     setParameters(parameters);
     initInternalVars();
     if (boardDefinition == null)
     {
         generatorState = GENERATOR_INIT_FAILED;
         addMessage("(SudokuGenerator) Generator not initialized - null board definition.", MSG_ERROR);
     }
     else if (boardDefinition.Count == 0)
     {
         generatorState = GENERATOR_INIT_FAILED;
         addMessage("(SudokuGenerator) Generator not initialized - blank board definition.", MSG_ERROR);
     }
     else
     {
         int[,] board = SudokuStore.loadBoard(boardDefinition);
         if (transformBeforeGeneration == true)
         {
             boardInit(SudokuStore.seqOfRandomBoardTransf(board), "transformed board provided by the user");
         }
         else
         {
             boardInit(board, "board provided by the user");
         }
     }
 }
예제 #2
0
 /**
  * Default constructor based on puzzle example.
  *
  * @param example         Example number between 0 and {@link SudokuPuzzles#NUMBER_OF_PUZZLE_EXAMPLES}.
  * @param parameters      Optional parameters.
  *
  * @see #PARAM_DO_NOT_SOLVE
  * @see #PARAM_DO_NOT_TRANSFORM
  * @see #PARAM_GEN_RND_BOARD
  */
 public SudokuGenerator(int example, params char[] parameters)
 {
     setParameters(parameters);
     initInternalVars();
     if ((example >= 0) && (example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES))
     {
         int[,] board = SudokuStore.boardCopy(SudokuStore.getPuzzleExample(example));
         if (transformBeforeGeneration == true)
         {
             boardInit(SudokuStore.seqOfRandomBoardTransf(board), "transformed example: " + example);
         }
         else
         {
             boardInit(board, "example: " + example);
         }
     }
     else
     {
         generatorState = GENERATOR_INIT_FAILED;
         addMessage("(SudokuGenerator) Generator not initialized incorrect example number: " + example + " - should be between 1 and " + SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES + ".", MSG_ERROR);
     }
 }
예제 #3
0
 /**
  * Default constructor based on random Sudoku puzzle example.
  *
  * @param parameters      Optional parameters.
  *
  * @see #PARAM_DO_NOT_SOLVE
  * @see #PARAM_DO_NOT_TRANSFORM
  * @see #PARAM_GEN_RND_BOARD
  */
 public SudokuGenerator(params char[] parameters)
 {
     setParameters(parameters);
     initInternalVars();
     if (generateRandomBoard == true)
     {
         boardInit(null, "random board");
     }
     else
     {
         int example = SudokuStore.randomIndex(SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES);
         int[,] board = SudokuStore.boardCopy(SudokuStore.getPuzzleExample(example));
         if (transformBeforeGeneration == true)
         {
             boardInit(SudokuStore.seqOfRandomBoardTransf(board), "transformed example: " + example);
         }
         else
         {
             boardInit(board, "example: " + example);
         }
     }
 }
예제 #4
0
 /**
  * Default constructor based on provided initial board.
  *
  * @param initialBoard    Array with the board definition.
  * @param parameters      Optional parameters
  *
  * @see #PARAM_DO_NOT_SOLVE
  * @see #PARAM_DO_NOT_TRANSFORM
  * @see #PARAM_GEN_RND_BOARD
  */
 public SudokuGenerator(int[,] initialBoard, params char[] parameters)
 {
     setParameters(parameters);
     initInternalVars();
     if (initialBoard == null)
     {
         generatorState = GENERATOR_INIT_FAILED;
         addMessage("(SudokuGenerator) Generator not initialized - null initial board.", MSG_ERROR);
     }
     else if (initialBoard.GetLength(0) != BOARD_SIZE)
     {
         generatorState = GENERATOR_INIT_FAILED;
         addMessage("(SudokuGenerator) Generator not initialized - incorrect number of rows in initial board, is: " + initialBoard.GetLength(0) + ",  expected: " + BOARD_SIZE + ".", MSG_ERROR);
     }
     else if (initialBoard.GetLength(1) != BOARD_SIZE)
     {
         generatorState = GENERATOR_INIT_FAILED;
         addMessage("(SudokuGenerator) Generator not initialized - incorrect number of columns in initial board, is: " + initialBoard.GetLength(1) + ", expected: " + BOARD_SIZE + ".", MSG_ERROR);
     }
     else if (SudokuStore.checkPuzzle(initialBoard) == false)
     {
         generatorState = GENERATOR_INIT_FAILED;
         addMessage("(SudokuGenerator) Generator not initialized - initial board contains an error.", MSG_ERROR);
     }
     else
     {
         int[,] board = SudokuStore.boardCopy(initialBoard);
         if (transformBeforeGeneration == true)
         {
             boardInit(SudokuStore.seqOfRandomBoardTransf(board), "transformed board provided by the user");
         }
         else
         {
             boardInit(board, "board provided by the user");
         }
     }
 }
예제 #5
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 s;
            int          sol;
            int          solNum;
            int          solUnq;
            int          solvingState;
            int          boardState;

            int[,] solution;
            int[,] puzzle;
            bool solCorr;
            bool testResult = true;

            switch (testId)
            {
            case 0:
                for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++)
                {
                    s      = new SudokuSolver(example);
                    solNum = s.findAllSolutions();
                    ErrorCodes.consolePrintlnIfError(solNum);
                    if (solNum != 1)
                    {
                        testResult = false;
                    }
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", findAllSolutions, example: " + example + ", solutions: " + solNum + ", time: " + s.getComputingTime() + " s.");
                }
                break;

            case 1:
                for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++)
                {
                    s      = new SudokuSolver(example);
                    solUnq = s.checkIfUniqueSolution();
                    ErrorCodes.consolePrintlnIfError(solUnq);
                    if (solUnq != SudokuSolver.SOLUTION_UNIQUE)
                    {
                        testResult = false;
                        SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", checkIfUniqueSolution, example: " + example + ", is solution unique: NO, time: " + s.getComputingTime() + " s.");
                    }
                    else
                    {
                        SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", checkIfUniqueSolution, example: " + example + ", is solution unique: YES, time: " + s.getComputingTime() + " s.");
                    }
                }
                break;

            case 2:
                for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++)
                {
                    s   = new SudokuSolver(example);
                    sol = s.solve();
                    ErrorCodes.consolePrintlnIfError(sol);
                    solution = s.getSolvedBoard();
                    solCorr  = SudokuStore.checkSolvedBoard(solution);
                    if (solCorr != true)
                    {
                        testResult = false;
                        SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: " + example + ", is solution correct: NO, time: " + s.getComputingTime() + " s.");
                    }
                    else
                    {
                        SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: " + example + ", is solution correct: YES, time: " + s.getComputingTime() + " s.");
                    }
                }
                break;

            case 3:
                for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++)
                {
                    puzzle = SudokuStore.seqOfRandomBoardTransf(SudokuStore.getPuzzleExample(example));
                    s      = new SudokuSolver(puzzle);
                    solNum = s.findAllSolutions();
                    ErrorCodes.consolePrintlnIfError(solNum);
                    if (solNum != 1)
                    {
                        testResult = false;
                    }
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + findAllSolutions, example: " + example + ", solutions: " + solNum + ", time: " + s.getComputingTime() + " s.");
                }
                break;

            case 4:
                for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++)
                {
                    puzzle = SudokuStore.seqOfRandomBoardTransf(SudokuStore.getPuzzleExample(example));
                    s      = new SudokuSolver(puzzle);
                    solUnq = s.checkIfUniqueSolution();
                    ErrorCodes.consolePrintlnIfError(solUnq);
                    if (solUnq != SudokuSolver.SOLUTION_UNIQUE)
                    {
                        testResult = false;
                        SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + checkIfUniqueSolution, example: " + example + ", is solution unique: NO, time: " + s.getComputingTime() + " s.");
                    }
                    else
                    {
                        SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + checkIfUniqueSolution, example: " + example + ", is solution unique: YES, time: " + s.getComputingTime() + " s.");
                    }
                }
                break;

            case 5:
                for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++)
                {
                    puzzle = SudokuStore.seqOfRandomBoardTransf(SudokuStore.getPuzzleExample(example));
                    s      = new SudokuSolver(puzzle);
                    sol    = s.solve();
                    ErrorCodes.consolePrintlnIfError(sol);
                    solution = s.getSolvedBoard();
                    solCorr  = SudokuStore.checkSolvedBoard(solution);
                    if (solCorr != true)
                    {
                        testResult = false;
                        SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + solve, example: " + example + ", is solution correct: NO, time: " + s.getComputingTime() + " s.");
                    }
                    else
                    {
                        SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + solve, example: " + example + ", is solution correct: YES, time: " + s.getComputingTime() + " s.");
                    }
                }
                break;

            case 6:
                s      = new SudokuSolver(SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION);
                solNum = s.findAllSolutions();
                ErrorCodes.consolePrintlnIfError(solNum);
                if (solNum <= 1)
                {
                    testResult = false;
                }
                SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", findAllSolutions, example: non unique, solutions: " + solNum + ", time: " + s.getComputingTime() + " s.");
                break;

            case 7:
                s      = new SudokuSolver(SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION);
                solUnq = s.checkIfUniqueSolution();
                ErrorCodes.consolePrintlnIfError(solUnq);
                if (solUnq != SudokuSolver.SOLUTION_NON_UNIQUE)
                {
                    testResult = false;
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", checkIfUniqueSolution, example: non unique, is solution unique: YES, time: " + s.getComputingTime() + " s.");
                }
                else
                {
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", checkIfUniqueSolution, example: non unique, is solution unique: NO, time: " + s.getComputingTime() + " s.");
                }
                break;

            case 8:
                s   = new SudokuSolver(SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION);
                sol = s.solve();
                ErrorCodes.consolePrintlnIfError(sol);
                solution = s.getSolvedBoard();
                solCorr  = SudokuStore.checkSolvedBoard(solution);
                if (solCorr != true)
                {
                    testResult = false;
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: non unique, is solution correct: NO, time: " + s.getComputingTime() + " s.");
                }
                else
                {
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: non unique, is solution correct: YES, time: " + s.getComputingTime() + " s.");
                }
                break;

            case 9:
                puzzle = SudokuStore.seqOfRandomBoardTransf(SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION);
                s      = new SudokuSolver(puzzle);
                solNum = s.findAllSolutions();
                ErrorCodes.consolePrintlnIfError(solNum);
                if (solNum <= 1)
                {
                    testResult = false;
                }
                SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + findAllSolutions, example: non unique, solutions: " + solNum + ", time: " + s.getComputingTime() + " s.");
                break;

            case 10:
                puzzle = SudokuStore.seqOfRandomBoardTransf(SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION);
                s      = new SudokuSolver(puzzle);
                solUnq = s.checkIfUniqueSolution();
                ErrorCodes.consolePrintlnIfError(solUnq);
                if (solUnq != SudokuSolver.SOLUTION_NON_UNIQUE)
                {
                    testResult = false;
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + checkIfUniqueSolution, example: non unique, is solution unique: YES, time: " + s.getComputingTime() + " s.");
                }
                else
                {
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + checkIfUniqueSolution, example: non unique, is solution unique: NO, time: " + s.getComputingTime() + " s.");
                }
                break;

            case 11:
                puzzle = SudokuStore.seqOfRandomBoardTransf(SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION);
                s      = new SudokuSolver(puzzle);
                sol    = s.solve();
                ErrorCodes.consolePrintlnIfError(sol);
                solution = s.getSolvedBoard();
                solCorr  = SudokuStore.checkSolvedBoard(solution);
                if (solCorr != true)
                {
                    testResult = false;
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + solve, example: non unique, is solution correct: NO, time: " + s.getComputingTime() + " s.");
                }
                else
                {
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + solve, example: non unique, is solution correct: YES, time: " + s.getComputingTime() + " s.");
                }
                break;

            case 12:
                s      = new SudokuSolver(SudokuPuzzles.PUZZLE_NO_SOLUTION);
                solNum = s.findAllSolutions();
                ErrorCodes.consolePrintlnIfError(solNum);
                if (solNum != 0)
                {
                    testResult = false;
                }
                SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", findAllSolutions, example: no solution, solutions: " + solNum + ", time: " + s.getComputingTime() + " s.");
                break;

            case 13:
                s      = new SudokuSolver(SudokuPuzzles.PUZZLE_NO_SOLUTION);
                solUnq = s.checkIfUniqueSolution();
                ErrorCodes.consolePrintlnIfError(solUnq);
                if (solUnq == SudokuSolver.SOLUTION_NOT_EXISTS)
                {
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", checkIfUniqueSolution, example: no solution, no solutions found: YES, time: " + s.getComputingTime() + " s.");
                }
                else
                {
                    testResult = false;
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", checkIfUniqueSolution, example: no solution, no solutions found: NO, time: " + s.getComputingTime() + " s.");
                }
                break;

            case 14:
                s        = new SudokuSolver(SudokuPuzzles.PUZZLE_NO_SOLUTION);
                sol      = s.solve();
                solution = s.getSolvedBoard();
                if (s.getSolvingState() == ErrorCodes.SUDOKUSOLVER_SOLVE_SOLVING_FAILED)
                {
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: no solution, solving failed: YES, time: " + s.getComputingTime() + " s.");
                }
                else
                {
                    testResult = false;
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: no solution, solving failed: NO, time: " + s.getComputingTime() + " s.");
                }
                break;

            case 15:
                s            = new SudokuSolver(SudokuPuzzles.PUZZLE_ERROR);
                solvingState = s.solve();
                boardState   = s.getBoardState();
                if ((boardState == SudokuBoard.BOARD_STATE_ERROR) && (solvingState == ErrorCodes.SUDOKUSOLVER_SOLVE_SOLVING_NOT_STARTED))
                {
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: board with error, board state error and solving not started: YES, time: " + s.getComputingTime() + " s.");
                }
                else
                {
                    testResult = false;
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: board with error, board state error and solving not started: NO, time: " + s.getComputingTime() + " s.");
                }
                break;

            case 16:
                s   = new SudokuSolver(SudokuPuzzles.PUZZLE_REGTESTS);
                sol = s.solve();
                ErrorCodes.consolePrintlnIfError(sol);
                if (SudokuStore.boardsAreEqual(s.getSolvedBoard(), SudokuPuzzles.PUZZLE_REGTESTS_SOLUTION))
                {
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: with solution, solutions are equal: YES, time: " + s.getComputingTime() + " s.");
                }
                else
                {
                    testResult = false;
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: with solution, solutions are equal: NO, time: " + s.getComputingTime() + " s.");
                }
                break;

            case 17:
                s      = new SudokuSolver(SudokuPuzzles.PUZZLE_EMPTY);
                sol    = s.solve();
                solUnq = s.checkIfUniqueSolution();
                ErrorCodes.consolePrintlnIfError(sol);
                ErrorCodes.consolePrintlnIfError(solUnq);
                if ((SudokuStore.checkSolvedBoard(s.getSolvedBoard()) == true) & (solUnq == SudokuSolver.SOLUTION_NON_UNIQUE))
                {
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: empty puzzle, found solution and solution non unique: YES, time: " + s.getComputingTime() + " s.");
                }
                else
                {
                    testResult = false;
                    SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: empty puzzle, found solution and solution non unique: NO, time: " + s.getComputingTime() + " s.");
                }
                break;
            }
            if (testResult == true)
            {
                SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + " >>>>>>>>>>>>>>>>>>>>>> SudokuSolver, result: OK");
            }
            else
            {
                SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + " >>>>>>>>>>>>>>>>>>>>>> SudokuSolver, result: ERROR");
            }
            return(testResult);
        }
예제 #6
0
        /*
         * ========================================
         *              Modify Menu
         * ========================================
         */
        /**
         * Modify menu loop
         */
        private void loopMenuModify()
        {
            int  selItem;
            Menu menu = new Menu(MenuData.MODIFY_TITLE, MenuData.MODIFY_CONTENT, this);

            do
            {
                selItem = menu.getItem();
                switch (selItem)
                {
                case MenuData.MODIFY_SET_CELL:
                    trackPuzzleUndo();
                    setCell();
                    break;

                case MenuData.MODIFY_ROTATE_CLOCK_WISE:
                    trackPuzzleUndo();
                    puzzle = SudokuStore.rotateClockWise(puzzle);
                    break;

                case MenuData.MODIFY_ROTATE_COUNTER_CLOCK_WISE:
                    trackPuzzleUndo();
                    puzzle = SudokuStore.rotateCounterclockWise(puzzle);
                    break;

                case MenuData.MODIFY_TRANSPOSE_TL_BR:
                    trackPuzzleUndo();
                    puzzle = SudokuStore.transposeTlBr(puzzle);
                    break;

                case MenuData.MODIFY_TRANSPOSE_TR_BL:
                    trackPuzzleUndo();
                    puzzle = SudokuStore.transposeTrBl(puzzle);
                    break;

                case MenuData.MODIFY_REFLECT_HORIZ:
                    trackPuzzleUndo();
                    puzzle = SudokuStore.reflectHorizontally(puzzle);
                    break;

                case MenuData.MODIFY_REFLECT_VERT:
                    trackPuzzleUndo();
                    puzzle = SudokuStore.reflectVertically(puzzle);
                    break;

                case MenuData.MODIFY_SWAP_COL_SEGMENTS:
                    trackPuzzleUndo();
                    puzzle = SudokuStore.swapColSegmentsRandomly(puzzle);
                    break;

                case MenuData.MODIFY_SWAP_ROW_SEGMENTS:
                    trackPuzzleUndo();
                    puzzle = SudokuStore.swapRowSegmentsRandomly(puzzle);
                    break;

                case MenuData.MODIFY_SWAP_COLS_IN_SEGMENTS:
                    trackPuzzleUndo();
                    puzzle = SudokuStore.swapColsInSegmentRandomly(puzzle);
                    break;

                case MenuData.MODIFY_SWAP_ROWS_IN_SEGMENTS:
                    trackPuzzleUndo();
                    puzzle = SudokuStore.swapRowsInSegmentRandomly(puzzle);
                    break;

                case MenuData.MODIFY_PERMUTE:
                    trackPuzzleUndo();
                    puzzle = SudokuStore.permuteBoard(puzzle);
                    break;

                case MenuData.MODIFY_RANDOM_TRANSF_ONE:
                    trackPuzzleUndo();
                    puzzle = SudokuStore.randomBoardTransf(puzzle);
                    break;

                case MenuData.MODIFY_RANDOM_TRANSF_SEQ:
                    trackPuzzleUndo();
                    puzzle = SudokuStore.seqOfRandomBoardTransf(puzzle);
                    break;

                case MenuData.UNDO:
                    performPuzzleUndo();
                    break;

                case MenuData.REDO:
                    performPuzzleRedo();
                    break;

                default: incorrectSelection(); break;
                }
            } while (selItem != MenuData.RETURN);
        }