def test_region_valid(self, number, row, col): coordinates = (row, col) board = [[7, 8, 0, 4, 0, 0, 1, 2, 0], [6, 0, 0, 0, 7, 5, 0, 0, 9], [0, 0, 0, 6, 0, 1, 0, 7, 8], [0, 0, 7, 0, 4, 0, 2, 6, 0], [0, 0, 1, 0, 5, 0, 9, 3, 0], [9, 0, 4, 0, 6, 0, 0, 0, 5], [0, 7, 0, 3, 0, 0, 0, 1, 2], [1, 2, 0, 0, 0, 7, 4, 0, 0], [0, 4, 9, 2, 0, 6, 0, 0, 7]] self.assertTrue(valid(board, number, coordinates))
def place(self, val): row, col = self.selected if self.cubes[row][col].value == 0: self.cubes[row][col].set(val) self.update_model() if valid(self.model, val, (row, col)) and solve(self.model): return True else: self.cubes[row][col].set(0) self.cubes[row][col].set_temp(0) self.update_model() return False
def place(self, val): row, col = self.selected if self.cubes[row][col].value == 0: self.cubes[row][col].set(val) self.update_model() #this is where the solve methods from the other python script are used if valid(self.model, val, (row, col)) and solve(self.model): return True else: self.cubes[row][col].set(0) self.cubes[row][col].set_temp(0) self.update_model() return False
def place(self, val): row, col = self.selected_pos self.model = [[self.box[i][j].value for j in range(self.cols)] for i in range(self.rows)] if valid(self.model, row, col, val): self.box[row][col].value = val self.model = [[self.box[i][j].value for j in range(self.cols)] for i in range(self.rows)] if solve(self.model): return True else: self.box[row][col].set_temp(0) self.box[row][col].set_val(0) return False
def auto_solve(self): self.model = [[self.box[r][c].value for c in range(self.cols)] for r in range(self.rows)] for i in range(self.rows): for j in range(self.cols): if (self.box[i][j].value == 0): for box_val in range(1, 10): if (valid(self.model, i, j, box_val)): self.box[i][j].value = box_val self.model = [[ self.box[r][c].value for c in range(self.cols) ] for r in range(self.rows)] if solve(self.model): return True else: self.box[i][j].value = 0
def solve(self, board): find = self.find_empty(self.board) if not find: return True else: row, col = find for i in range(1, 10): if valid(self.board, i, (row, col)): self.board[row][col] = i if self.solve(self.board): return True self.board[row][col] = 0 return False
def solve_gui(self): """Solves the board while displaying the process. This function is based on the solver in the sudoku module. It uses the backtracking algorithm and updates both the unformatted and formatted grid. The unformatted is used for the solver functions, while the formatted grid is used to display the numbers and highlights. Returns: bool: a boolean that describes whether the board is solvable. It is fed back into the function in a recursive manner """ empty = sudoku.find_empty(self.grid) if not empty: return True for i in range(1, 10): if sudoku.valid(self.grid, i, empty): y, x = empty self.grid[y][x] = i self.update_formatted(i, empty) self.formatted[y][x].correct = True screen_update() if self.solve_gui(): self.formatted[y][x].wrong = False self.formatted[y][x].correct = False pg.time.delay(10) return True self.grid[y][x] = 0 self.update_formatted(0, empty) self.formatted[y][x].wrong = True self.formatted[y][x].correct = False screen_update() return False
def solve_gui(self): self.update_model() find = find_empty(self.model) if not find: return True else: row, col = find for i in range(1, 10): if valid(self.model, i, (row, col)): self.model[row][col] = i self.cubes[row][col].set(i) self.cubes[row][col].draw_change(self.win, True) self.update_model() pygame.display.update() pygame.time.delay(100) if self.solve_gui(): return True self.model[row][col] = 0 self.cubes[row][col].set(0) self.update_model() self.cubes[row][col].draw_change(self.win, False) pygame.display.update() pygame.time.delay(100) return False
if keys[pg.K_SPACE]: play_board.solve_gui() nums = [keys[pg.K_1], keys[pg.K_2], keys[pg.K_3], keys[pg.K_4], keys[pg.K_5], keys[pg.K_6], keys[pg.K_7], keys[pg.K_8], keys[pg.K_9]] if selected and not selected.fixed: if any(nums): selected.pencil = nums.index(True) + 1 if keys[pg.K_RETURN] and selected.pencil: selected.value = selected.pencil selected.pencil = 0 y, x = selected.pos play_board.player_grid[y][x] = selected.value if play_board.solved(): completed = True if not sudoku.valid(play_board.player_grid, selected.value, selected.pos): selected.wrong = True else: selected.wrong = False if keys[pg.K_p]: pencil_mode = not pencil_mode if keys[pg.K_BACKSPACE]: if pencil_mode: selected.pencil = 0 else: selected.value = 0 screen_update() pg.quit()
def add_number(self): if self.user_inputs() == True: coords = (self.row, self.col) if valid(self.board, self.number, coords) == True and self.board[self.row][self.col] == 0: self.board[self.row][self.col] = self.number