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(20) ################################## 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(20) return False
def generate(): board = [ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0] ] rand = randint(15, 26) # print(rand) for i in range(rand): # seed(i * rand) row = randint(0, 8) col = randint(0, 8) value = randint(1, 9) # print('row: %r\n col: %r\nvalue:%r' % (row, col, value)) # print_board(board) if valid(board, value, (row, col)): board[row][col] = value else: rand += 1 for i in range(0, 8): # validates the current value so recursion function doesn't hang for j in range(0, 8): if board[i][j] != 0: if valid(board, board[i][j], (i, j)): pass # print(self.model[i][j]) else: generate() temp = copy.deepcopy(board) print_board(temp) with recursionlimit(500): if solve(board): return temp else: clearboard() generate()
def validate(self): self.update_model() # print_board(self.model) for i in range(self.rows): for j in range(self.cols): if self.model[i][j] != 0: if valid(self.model, self.model[i][j], (i, j)): pass # print(self.model[i][j]) else: return False return True
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 placetemp(self): row, col = self.selected if self.box[row][col].value == 0: self.box[row][col].set(self.box[row][col].temp) self.update_model() if valid(self.model, self.box[row][col].temp, (row, col)) and solve(self.model): return True else: self.box[row][col].set(0) self.box[row][col].settemp(0) self.update_model() return False