Ejemplo n.º 1
0
		/**
		 * 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;
			}
		}