/* * ======================================== * Load Menu * ======================================== */ /** * Load menu loop. */ private void loopMenuLoad() { int selItem; Menu menu = new Menu(MenuData.LOAD_TITLE, MenuData.LOAD_CONTENT, this); do { selItem = menu.getItem(); switch (selItem) { case MenuData.LOAD_FROM_FILE: loadFromFile(); break; case MenuData.LOAD_EXAMPLE: loadFromExample(); break; case MenuData.LOAD_EMPTY_PUZZLE: trackPuzzleUndo(); puzzle = SudokuStore.boardCopy(SudokuPuzzles.PUZZLE_EMPTY); break; case MenuData.LOAD_LIST_EXAMPLES: listPuzzleExamples(); break; case MenuData.UNDO: performPuzzleUndo(); break; case MenuData.REDO: performPuzzleRedo(); break; default: incorrectSelection(); break; } } while (selItem != MenuData.RETURN); }
/** * 13 rows keyboard input (4 supporting) - 0 or '.' as empty cell. * Any other character is being filtered out. * * @see SudokuStore#loadBoardFromStrings(String...) * @see SudokuStore#loadBoard(String[]) */ private void inputPuzzleFromKeyboard13rows() { JanetConsole.println("You will be asked for inputting 13 rows (4 supporting)."); JanetConsole.print("Row 1/13: "); String r1 = JanetConsole.readLine(); JanetConsole.print("Row 2/13: "); String r2 = JanetConsole.readLine(); JanetConsole.print("Row 3/13: "); String r3 = JanetConsole.readLine(); JanetConsole.print("Row 4/13: "); String r4 = JanetConsole.readLine(); JanetConsole.print("Row 5/13: "); String r5 = JanetConsole.readLine(); JanetConsole.print("Row 6/13: "); String r6 = JanetConsole.readLine(); JanetConsole.print("Row 7/13: "); String r7 = JanetConsole.readLine(); JanetConsole.print("Row 8/13: "); String r8 = JanetConsole.readLine(); JanetConsole.print("Row 9/13: "); String r9 = JanetConsole.readLine(); JanetConsole.print("Row 10/13: "); String r10 = JanetConsole.readLine(); JanetConsole.print("Row 11/13: "); String r11 = JanetConsole.readLine(); JanetConsole.print("Row 12/13: "); String r12 = JanetConsole.readLine(); JanetConsole.print("Row 13/13: "); String r13 = JanetConsole.readLine(); int[,] parsedPuzzle = SudokuStore.loadBoardFromStrings(r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13); if (parsedPuzzle != null) { trackPuzzleUndo(); puzzle = parsedPuzzle; } else { JanetConsole.println(">>> !!! Error - incorrect puzzle definition !!! <<<"); } }
/** * 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()); } }
/** * Start all regression tests. * * @param threadsNumber Threads number. * @return Number of tests with error result. */ public static int Start(int threadsNumber) { SudokuStore.consolePrintln("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"); SudokuStore.consolePrintln("All regression tests - starting."); SudokuStore.consolePrintln(" - RegTestsSolver.start()"); SudokuStore.consolePrintln(" - RegTestsGenerator.start()"); SudokuStore.consolePrintln(" - RegTestsStore.start()"); SudokuStore.consolePrintln(" - RegTestsApi.start()"); SudokuStore.consolePrintln("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"); long startTime = DateTimeX.currentTimeMillis(); int solverErrors = RegTestsSolver.Start(threadsNumber); int generatorErrors = RegTestsGenerator.Start(threadsNumber); int storeErrors = RegTestsStore.Start(threadsNumber); int apiErrors = RegTestsApi.Start(threadsNumber); long endTime = DateTimeX.currentTimeMillis(); double computingTime = (endTime - startTime) / 1000.0; int totalErrors = solverErrors + generatorErrors + storeErrors + apiErrors; SudokuStore.consolePrintln("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"); SudokuStore.consolePrintln("All regression tests - finished."); SudokuStore.consolePrintln("Errors: " + totalErrors); SudokuStore.consolePrintln(" - RegTestsSolver errors: " + solverErrors); SudokuStore.consolePrintln(" - RegTestsGenerator errors: " + generatorErrors); SudokuStore.consolePrintln(" - RegTestsStore errors: " + storeErrors); SudokuStore.consolePrintln(" - RegTestsApi errors: " + apiErrors); SudokuStore.consolePrintln(""); SudokuStore.consolePrintln("Computing time: " + computingTime + " s."); SudokuStore.consolePrintln("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"); return(totalErrors); }
/** * 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"); } } }
/** * Load puzzle from file. * @see SudokuStore#loadBoard(String) */ private void loadFromFile() { JanetConsole.print("File path: "); String filePath = JanetConsole.readLine(); FileInfo file = new FileInfo(filePath); if (file.Exists == false) { JanetConsole.println(">>> !!! Error - file does not exist !!! <<<"); return; } DirectoryInfo dir = new DirectoryInfo(filePath); if (dir.Exists == true) { JanetConsole.println(">>> !!! Error - not a file !!! <<<"); return; } int[,] puzzleLoaded = SudokuStore.loadBoard(filePath); if (puzzleLoaded == null) { JanetConsole.println(">>> !!! Error - incorrect file content !!! <<<"); return; } trackPuzzleUndo(); puzzle = puzzleLoaded; }
/** * Cell is not pointing to any cells on the board. */ public BoardCell() { rowIndex = INDEX_NULL; colIndex = INDEX_NULL; digit = EMPTY; randomSeed = SudokuStore.nextRandom(); digitsStillFreeNumber = -1; }
/** * Constructor - initialized entry. * @param rowIndex Row index. * @param colIndex Column index. * @param digit Entry digit. */ public BoardCell(int rowIndex, int colIndex, int digit) { this.rowIndex = rowIndex; this.colIndex = colIndex; this.digit = digit; randomSeed = SudokuStore.nextRandom(); digitsStillFreeNumber = -1; }
/** * Default constructor. * @param threadsNumber Threads number. */ internal SolverTests(int threadsNumber) { THREADS_NUMBER = threadsNumber; threads = new Thread[THREADS_NUMBER]; runners = new TestRunner[THREADS_NUMBER]; testsResults = new bool[NUMBER_OF_TESTS]; int[] testsIds = new int[NUMBER_OF_TESTS]; for (int i = 0; i < NUMBER_OF_TESTS; i++) { testsIds[i] = i; } /* * Randomization of tests before assignments to threads */ for (int i = 0; i < NUMBER_OF_TESTS; i++) { int lastIndex = NUMBER_OF_TESTS - i - 1; int j = SudokuStore.randomIndex(NUMBER_OF_TESTS - i); if (j != lastIndex) { int l = testsIds[lastIndex]; testsIds[lastIndex] = testsIds[j]; testsIds[j] = l; } } /* * Tests assignments to threads */ int defThreadSize = NUMBER_OF_TESTS / THREADS_NUMBER; int remaining = NUMBER_OF_TESTS - defThreadSize * THREADS_NUMBER; int t = 0; for (int i = 0; i < THREADS_NUMBER; i++) { int threadSize = defThreadSize; if (i < remaining) { threadSize++; } int[] assigments = new int[threadSize]; for (int j = 0; j < threadSize; j++) { assigments[j] = testsIds[t]; t++; } runners[i] = new TestRunner(i, assigments, testsResults); threads[i] = new Thread(runners[i].run); } }
/** * Saves board to the text file. * * @param filePath Path to the file. * @param headComment Comment to be added at the head. * @param tailComment Comment to be added at the tail. * @return True if saving was successful, otherwise false. * * @see SudokuStore#saveBoard(int[,], String, String, String) * @see SudokuStore#boardToString(int[,], String, String) */ public bool saveBoard(String filePath, String headComment, String tailComment) { bool savingStatus = SudokuStore.saveBoard(sudokuBoard, filePath, headComment, tailComment); if (savingStatus == true) { addMessage("(saveBoard) Saving successful, file: " + filePath, MSG_INFO); } else { addMessage("(saveBoard) Saving failed, file: " + filePath, MSG_ERROR); } return(savingStatus); }
/** * Rate puzzle difficulty meaning as number of closed routes (number of * wrong guesses). * * @see SudokuStore#calculatePuzzleRating(int[,]) */ private void ratePuzzleDifficulty() { int rating = SudokuStore.calculatePuzzleRating(puzzle); if (rating >= 0) { JanetConsole.println(">>>"); JanetConsole.println(">>> Puzzle rating: " + rating); JanetConsole.println(">>>"); } else { JanetConsole.println(">>> !!! Error code: " + rating + " !!! <<<"); JanetConsole.println(">>> " + ErrorCodes.getErrorDescription(rating)); } }
/** * One line keyboard input (81 characters) * 0 or '.' as empty cell. * * @see SudokuStore#loadBoardFromStringLine(String) */ private void inputPuzzleFromKeyboard1Line() { JanetConsole.print("One line definition: "); String line = JanetConsole.readLine(); int[,] parsedPuzzle = SudokuStore.loadBoardFromStringLine(line); if (parsedPuzzle != null) { trackPuzzleUndo(); puzzle = parsedPuzzle; } else { JanetConsole.println(">>> !!! Error - incorrect puzzle definition !!! <<<"); } }
/** * Load puzzle from provided puzzle examples. * @see SudokuPuzzles * @see SudokuStore#getPuzzleExample(int) */ private void loadFromExample() { JanetConsole.println(); JanetConsole.print("Please provide example number (between 0 and " + (SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES - 1) + "): "); int example = JanetConsole.readInt(); if ((example >= 0) && (example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES)) { JanetConsole.println("Loading example: " + example); trackPuzzleUndo(); puzzle = SudokuStore.boardCopy(SudokuStore.getPuzzleExample(example)); } else { JanetConsole.println(">>> !!! Incorrect example number !!! <<<"); } }
/* * ======================================== * Save Menu * ======================================== */ /** * Saves current puzzle in the txt file. * * @see SudokuStore#saveBoard(int[,], String) */ private void savePuzzle() { JanetConsole.print("File path: "); String filePath = JanetConsole.readLine(); FileInfo file = new FileInfo(filePath); DirectoryInfo dir = new DirectoryInfo(filePath); if ((file.Exists == true) || (dir.Exists == true)) { JanetConsole.println(">>> !!! Error - file already exists !!! <<<"); return; } bool puzzleSaved = SudokuStore.saveBoard(puzzle, filePath); if (puzzleSaved == false) { JanetConsole.println(">>> !!! Error while saving !!! <<<"); } }
/** * Random string generator. * @param length Length of random string. * @return Random string containing a-zA-Z0-9. */ public static String randomString(int length) { if (length < 1) { return(""); } char[] chars = { 'z', 'x', 'c', 'v', 'b', 'n', 'm', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; String rndStr = ""; for (int i = 0; i < length; i++) { rndStr = rndStr + chars[SudokuStore.randomIndex(chars.Length)]; } return(rndStr); }
/** * 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); } }
/** * 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); } } }
/** * 9 rows keyboard input - 0 or '.' as empty cell. * Any other character is being filtered out. * * @see SudokuStore#loadBoardFromStrings(String...) * @see SudokuStore#loadBoard(String[]) */ private void inputPuzzleFromKeyboard9rows() { JanetConsole.println("You will be asked for inputting 9 rows."); JanetConsole.print("Row 1/9: "); String r1 = JanetConsole.readLine(); JanetConsole.print("Row 2/9: "); String r2 = JanetConsole.readLine(); JanetConsole.print("Row 3/9: "); String r3 = JanetConsole.readLine(); JanetConsole.print("Row 4/9: "); String r4 = JanetConsole.readLine(); JanetConsole.print("Row 5/9: "); String r5 = JanetConsole.readLine(); JanetConsole.print("Row 6/9: "); String r6 = JanetConsole.readLine(); JanetConsole.print("Row 7/9: "); String r7 = JanetConsole.readLine(); JanetConsole.print("Row 8/9: "); String r8 = JanetConsole.readLine(); JanetConsole.print("Row 9/9: "); String r9 = JanetConsole.readLine(); int[,] parsedPuzzle = SudokuStore.loadBoardFromStrings(r1, r2, r3, r4, r5, r6, r7, r8, r9); if (parsedPuzzle != null) { trackPuzzleUndo(); puzzle = parsedPuzzle; } else { JanetConsole.println(">>> !!! Error - incorrect puzzle definition !!! <<<"); } }
/** * 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"); } } }
/** * Runs SudokuSolver regression tests. * @param threadsNumber Number of threads. * @return Number of tests with errors. */ public static int Start(int threadsNumber) { int numberOfTests = SolverTests.NUMBER_OF_TESTS; int resultsError = 0; int resultsOk = 0; long startTime = DateTimeX.currentTimeMillis(); SolverTests st = new SolverTests(threadsNumber); st.start(); bool[] testResults = st.testsResults; for (int t = 0; t < numberOfTests; t++) { if (testResults[t] == true) { resultsOk++; } else { resultsError++; } } long endtTime = DateTimeX.currentTimeMillis(); double computingTime = (endtTime - startTime) / 1000.0; SudokuStore.consolePrintln("============================================================="); SudokuStore.consolePrintln("Number of SudokuSolver test: " + numberOfTests + ", OK: " + resultsOk + ", ERRORS: " + resultsError + ", computing time: " + computingTime); for (int t = 0; t < numberOfTests; t++) { if (testResults[t] == false) { SudokuStore.consolePrintln("ERROR: " + t); } } SudokuStore.consolePrintln("============================================================="); return(resultsError); }
/** * 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); }
/** * Board initialization method. * @param initBoard Initial board. * @param info The string to pass to the msg builder. */ private void boardInit(int[,] initBoard, String info) { SudokuSolver puzzle; if ((initBoard == null) && (generateRandomBoard == true)) { puzzle = new SudokuSolver(SudokuPuzzles.PUZZLE_EMPTY); puzzle.solve(); if (puzzle.getSolvingState() == SudokuSolver.SOLVING_STATE_SOLVED) { sudokuBoard = puzzle.solvedBoard; addMessage("(SudokuGenerator) Generator initialized using random board (" + info + ").", MSG_INFO); generatorState = GENERATOR_INIT_FINISHED; return; } else { addMessage("(SudokuGenerator) Generator initialization using random board (" + info + ") failed. Board with error?", MSG_ERROR); addMessage(puzzle.getLastErrorMessage(), MSG_ERROR); generatorState = GENERATOR_INIT_FAILED; return; } } if (SudokuStore.checkPuzzle(initBoard) == false) { generatorState = GENERATOR_INIT_FAILED; addMessage("(SudokuGenerator) Generator initialization (" + info + ") failed. Board with error?", MSG_ERROR); return; } if (solveBeforeGeneration == true) { puzzle = new SudokuSolver(initBoard); puzzle.solve(); if (puzzle.getSolvingState() == SudokuSolver.SOLVING_STATE_SOLVED) { sudokuBoard = puzzle.solvedBoard; addMessage("(SudokuGenerator) Generator initialized usign provided board + finding solution (" + info + ").", MSG_INFO); generatorState = GENERATOR_INIT_FINISHED; return; } else { addMessage("(SudokuGenerator) Generator initialization usign provided board + finding solution (" + info + ") failed. Board with error?", MSG_ERROR); addMessage(puzzle.getLastErrorMessage(), MSG_ERROR); generatorState = GENERATOR_INIT_FAILED; return; } } int[,] board = initBoard; puzzle = new SudokuSolver(board); if (puzzle.checkIfUniqueSolution() == SudokuSolver.SOLUTION_UNIQUE) { sudokuBoard = board; addMessage("(SudokuGenerator) Generator initialized usign provided board (" + info + ").", MSG_INFO); generatorState = GENERATOR_INIT_FINISHED; return; } else { addMessage("(SudokuGenerator) Generator initialization usign provided board (" + info + ") failed. Solution not exists or is non unique.", MSG_ERROR); addMessage(puzzle.getLastErrorMessage(), MSG_ERROR); generatorState = GENERATOR_INIT_FAILED; return; } }
/** * 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 :-) */ }
/** * Tracks puzzle redo. */ private void trackPuzzleRedo() { puzzleRedo = SudokuStore.boardCopy(puzzle); }
/** * 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); }
/** * Default constructor. */ public JanetSudoku() { puzzle = SudokuStore.boardCopy(SudokuPuzzles.PUZZLE_EMPTY); rndSeedOnCells = true; rndSeedOnDigits = true; }
/* * ======================================== * 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); }
/** * 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); }
/** * 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) { bool testResult = true; String testDesc = "", resultDesc = ""; int[,] a = { { 0, 0, 0, 8, 0, 0, 0, 0, 0 }, { 4, 0, 0, 0, 1, 5, 0, 3, 0 }, { 0, 2, 9, 0, 4, 0, 5, 1, 8 }, { 0, 4, 0, 0, 0, 0, 1, 2, 0 }, { 0, 0, 0, 6, 0, 2, 0, 0, 0 }, { 0, 3, 2, 0, 0, 0, 0, 9, 0 }, { 6, 9, 3, 0, 5, 0, 8, 7, 0 }, { 0, 5, 0, 4, 8, 0, 0, 0, 1 }, { 0, 0, 0, 0, 0, 3, 0, 0, 0 } }; switch (testId) { case 0: testDesc = "SudokuSolver.setCell(int, int, int)"; { SudokuSolver s = new SudokuSolver(); s.setCell(0, 0, 0); s.setCell(1, 0, 4); s.setCell(2, 0, 0); s.setCell(3, 0, 0); s.setCell(4, 0, 0); s.setCell(5, 0, 0); s.setCell(6, 0, 6); s.setCell(7, 0, 0); s.setCell(8, 0, 0); s.setCell(0, 1, 0); s.setCell(1, 1, 0); s.setCell(2, 1, 2); s.setCell(3, 1, 4); s.setCell(4, 1, 0); s.setCell(5, 1, 3); s.setCell(6, 1, 9); s.setCell(7, 1, 5); s.setCell(8, 1, 0); s.setCell(0, 2, 0); s.setCell(1, 2, 0); s.setCell(2, 2, 9); s.setCell(3, 2, 0); s.setCell(4, 2, 0); s.setCell(5, 2, 2); s.setCell(6, 2, 3); s.setCell(7, 2, 0); s.setCell(8, 2, 0); s.setCell(0, 3, 8); s.setCell(1, 3, 0); s.setCell(2, 3, 0); s.setCell(3, 3, 0); s.setCell(4, 3, 6); s.setCell(5, 3, 0); s.setCell(6, 3, 0); s.setCell(7, 3, 4); s.setCell(8, 3, 0); s.setCell(0, 4, 0); s.setCell(1, 4, 1); s.setCell(2, 4, 4); s.setCell(3, 4, 0); s.setCell(4, 4, 0); s.setCell(5, 4, 0); s.setCell(6, 4, 5); s.setCell(7, 4, 8); s.setCell(8, 4, 0); s.setCell(0, 5, 0); s.setCell(1, 5, 5); s.setCell(2, 5, 0); s.setCell(3, 5, 0); s.setCell(4, 5, 2); s.setCell(5, 5, 0); s.setCell(6, 5, 0); s.setCell(7, 5, 0); s.setCell(8, 5, 3); s.setCell(0, 6, 0); s.setCell(1, 6, 0); s.setCell(2, 6, 5); s.setCell(3, 6, 1); s.setCell(4, 6, 0); s.setCell(5, 6, 0); s.setCell(6, 6, 8); s.setCell(7, 6, 0); s.setCell(8, 6, 0); s.setCell(0, 7, 0); s.setCell(1, 7, 3); s.setCell(2, 7, 1); s.setCell(3, 7, 2); s.setCell(4, 7, 0); s.setCell(5, 7, 9); s.setCell(6, 7, 7); s.setCell(7, 7, 0); s.setCell(8, 7, 0); s.setCell(0, 8, 0); s.setCell(1, 8, 0); s.setCell(2, 8, 8); s.setCell(3, 8, 0); s.setCell(4, 8, 0); s.setCell(5, 8, 0); s.setCell(6, 8, 0); s.setCell(7, 8, 1); s.setCell(8, 8, 0); int[,] b = s.getBoard(); if ((SudokuStore.boardsAreEqual(a, b) == true)) { resultDesc = "Expecting equal - are equal."; } else { resultDesc = "Expecting equal - are not equal."; testResult = false; SudokuStore.consolePrintln(s.getMessages()); } } break; case 1: testDesc = "SudokuSolver.getCellDigit(int, int)"; { SudokuSolver s1 = new SudokuSolver(a); SudokuSolver s2 = new SudokuSolver(); for (int i = 0; i < SudokuBoard.BOARD_SIZE; i++) { for (int j = 0; j < SudokuBoard.BOARD_SIZE; j++) { int d = s1.getCellDigit(i, j); s2.setCell(i, j, d); } } int[,] b = s2.getBoard(); if ((SudokuStore.boardsAreEqual(a, b) == true)) { resultDesc = "Expecting equal - are equal."; } else { resultDesc = "Expecting equal - are not equal."; testResult = false; } } break; case 2: testDesc = "SudokuSolver.getBoardCopy()"; { SudokuSolver s = new SudokuSolver(a); int[,] b = s.getBoard(); int[,] c = s.getBoardCopy(); if ((SudokuStore.boardsAreEqual(b, c) == true)) { resultDesc = "Expecting equal - are equal."; } else { resultDesc = "Expecting equal - are not equal."; testResult = false; } } break; case 3: testDesc = "SudokuSolver.getSolutionBoardCells()"; { SudokuSolver s = new SudokuSolver(a); s.solve(); int[,] b = s.getSolvedBoard(); int[,] c = SudokuStore.boardCopy(a); BoardCell[] sol = s.getSolutionBoardCells(); foreach (BoardCell bc in sol) { c[bc.rowIndex, bc.colIndex] = bc.digit; } if ((SudokuStore.boardsAreEqual(b, c) == true)) { resultDesc = "Expecting equal - are equal."; } else { resultDesc = "Expecting equal - are not equal."; testResult = false; } } break; case 4: testDesc = "SudokuSolver.getAllBoardCells()"; { SudokuSolver s = new SudokuSolver(a); int[,] b = s.getBoardCopy(); int[,] c = new int[SudokuBoard.BOARD_SIZE, SudokuBoard.BOARD_SIZE]; BoardCell[] bc = s.getAllBoardCells(); foreach (BoardCell cell in bc) { c[cell.rowIndex, cell.colIndex] = cell.digit; } if ((SudokuStore.boardsAreEqual(b, c) == true)) { resultDesc = "Expecting equal - are equal."; } else { resultDesc = "Expecting equal - are not equal."; testResult = false; } } break; case 5: testDesc = "SudokuSolver.getAllSolutionsList()"; { SudokuSolver s = new SudokuSolver(SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION); s.findAllSolutions(); List <SudokuBoard> solList; solList = s.getAllSolutionsList(); foreach (SudokuBoard sb in solList) { if (SudokuStore.checkSolvedBoard(sb.board) == false) { testResult = false; break; } } if (testResult == true) { resultDesc = "Expecting each solution valid - each is valid."; } else { resultDesc = "Expecting each solution valid - found not valid."; } } break; case 6: testDesc = "SudokuGenerator -> generate -> save -> load -> compare"; { String filePath = FileX.getTmpDir() + FileX.genRndFileName(20, "txt"); SudokuGenerator g = new SudokuGenerator(SudokuGenerator.PARAM_GEN_RND_BOARD); int[,] generated = g.generate(); g.saveBoard(filePath, "generated", "saved"); int[,] loaded = SudokuStore.loadBoard(filePath); FileX.removeFile(filePath); if (SudokuStore.boardsAreEqual(generated, loaded) == false) { testResult = false; } if (testResult == true) { resultDesc = "Expecting equal - are equal."; } else { resultDesc = "Expecting equal - are not equal."; } } break; case 7: testDesc = "SudokuSolver -> solve -> save -> load -> compare"; { String filePath = FileX.getTmpDir() + FileX.genRndFileName(20, "txt"); SudokuSolver s = new SudokuSolver(SudokuStore.getPuzzleExample()); s.solve(); int[,] solved = s.getSolvedBoard(); s.saveSolvedBoard(filePath, "solved", "saved"); int[,] loaded = SudokuStore.loadBoard(filePath); FileX.removeFile(filePath); if (SudokuStore.boardsAreEqual(solved, loaded) == false) { testResult = false; } if (testResult == true) { resultDesc = "Expecting equal - are equal."; } else { resultDesc = "Expecting equal - are not equal."; } } break; } if (testResult == true) { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + " " + testDesc + " " + resultDesc + " >>> ApiTests, result: OK"); } else { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + " " + testDesc + " " + resultDesc + " >>> ApiTests, result: ERROR"); } return(testResult); }