def solve(): """this function solves the wordseaerch puzzle given and returns the correspondant cell and direction of each word""" self.solveButton.config(state="disabled") self.grid = empty((9, 9), dtype=str) try: for row in range(9): for column in range(9): self.grid[row, column] = int( self.numberStringVar[(row, column)].get()) except: mb.showerror("Entry Error", "Input must be single digit numbers") self.solveButton.config(state="normal") start = time() solver = SudokuSolver(self.grid) try: mb.showinfo( "Loding", "The solving process could take a long time depending on te complexity of the Sudoku grid \nPlease Wait !!!" ) self.grid = solver.solve() for row in range(9): for column in range(9): self.numberStringVar[(row, column)].set( str(self.grid[row, column])) mb.showinfo( "Finished", "The solving process took about " + str(time() - start) + " seconds to finish !!!") except: mb.showerror( "Entry Error", "There is an error in your input, please re-enter a coorect grid" ) self.solveButton.config(state="normal")
def main(): """Spreadsheet Column Printer This script allows the user to solve a Sudoku board by specifying the image-path as an argument (with quotation marks), the image needs to contain a Sudoku board. This script works best on image taken on real paper where the Sudoku board takes up almost the whole picture. If no path is specified, a random image will be taken from the folder Example images. This script requires that the following dependencies to be within the Python environment you are running this script in: - keras - tensorflow - numpy - matplotlib - scikit-image This script can also be run in an IDE, but then the user has to manipulate and manually set the image path. """ if len(sys.argv) > 1: image_path = str(sys.argv[1]) else: print("No image specified. Taking a random image.") base_path = os.getcwd() + "\Example images\\" image_path = base_path + os.listdir(base_path)[random.randint(0,len(os.listdir(base_path))-1)] input_sudoku_img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) plt.imshow(input_sudoku_img, cmap='gray') plt.show() resized_sudoku_img = cv2.resize(input_sudoku_img, (1200, 900), interpolation=cv2.INTER_AREA) processed_sudoku_img = preprocess_sudoku_board(resized_sudoku_img.copy(), MNIST_DIMENSION) sudoku_board = np.zeros((9, 9), dtype=np.int8) # Import final model model = load_model(os.path.dirname(os.path.realpath(__file__)) + "\Final model") for index, square in enumerate(processed_sudoku_img): if square[1]: # Normalize number number = square[0] / 255 number = np.reshape(number, newshape=(1, 28, 28, 1)) prediction = model.predict(number)[0] sudoku_board[int(index / 9)][index % 9] = int(np.argmax(prediction)) + 1 else: sudoku_board[int(index / 9)][index % 9] = -1 print("Interpreted the Sudoku to be:") sudoku_solver = SudokuSolver() sudoku_solver.print_board(sudoku_board) if not sudoku_solver.is_valid_board(sudoku_board): print("Sorry, no solution was found. Take a new picture and try again.") quit() solved_sudoku = np.array(sudoku_solver.solve(sudoku_board)[0]) sudoku_solver.print_board(solved_sudoku)
def generate_puzzle(iterations): solution = SudokuBoard() solution.build() playable = solution.clone() difficult_coefficient = 1 for _ in range(iterations): new_playable = playable.clone() row_num, column_num = random.randrange(9), random.randrange(9) while new_playable.is_cell_empty(row_num, column_num): row_num, column_num = random.randrange(9), random.randrange(9) new_playable.clear_cell(row_num, column_num) solver = SudokuSolver(new_playable) try: solver.solve() playable = new_playable difficult_coefficient = solver.difficult_coefficient except: continue difficult_level = None if difficult_coefficient < DifficultLevel.VERY_EASY.value: difficult_level = DifficultLevel.VERY_EASY elif difficult_coefficient < DifficultLevel.EASY.value: difficult_level = DifficultLevel.EASY elif difficult_coefficient < DifficultLevel.MEDIUM.value: difficult_level = DifficultLevel.MEDIUM elif difficult_coefficient < DifficultLevel.HARD.value: difficult_level = DifficultLevel.HARD elif difficult_coefficient < DifficultLevel.VERY_HARD.value: difficult_level = DifficultLevel.VERY_HARD elif difficult_coefficient < DifficultLevel.MASTER.value: difficult_level = DifficultLevel.MASTER return SudokuGame(playable, solution, difficult_level, difficult_coefficient)
def click(): def new_int(t): return int(t) if t else 0 grid = [ list(new_int(t.get()) for t in self.texts[i * 9:i * 9 + 9]) for i in range(9) ] s = SudokuSolver(grid) r = s.solve() if r.startswith('+'): lbs = [ list(t for t in labels[i * 9:i * 9 + 9]) for i in range(9) ] for i in range(9): for j in range(9): lbs[i][j].config(text=s.b[i][j]) else: mb.showerror(message=r)
from CheckNumber import checkIfNumberOk from SudokuGen import SudokuGenerator from SudokuSolver import SudokuSolver print('generating...') s1 = SudokuGenerator() sudoku1 = s1.getSudokuArray(9, 0.3) print(sudoku1) print('start solving...') s = SudokuSolver() print(s.solve(sudoku1))
if not board_is_valid: if settings.VERBOSE_EXIT: print("ERROR -- Starting values found in sudoku were not valid.") print("The following board was found: ") sudoku_solver.print_sudoku() exit() if settings.ENABLE_DEBUG: print("DEBUG -- Following starting board was found: ") sudoku_solver.print_sudoku() if settings.ENABLE_DEBUG: print("DEBUG -- Attempting to solve the sudoku.") sudoku_solver.solve(sudoku_solver.board) if settings.ENABLE_DEBUG: print("DEBUG -- Solution to sudoku was found:") sudoku_solver.print_sudoku() # Write the solution to the image and preview it. if sudoku_solver.is_solved: if settings.ENABLE_DEBUG: print("DEBUG -- Sudoku succesfully solved.") else: if settings.VERBOSE_EXIT: print("ERROR -- Sudoku could not be solved.") exit()
from SudokuSolver import SudokuSolver obj = SudokuSolver() grid = obj.readSudoku() obj.solve(grid)