def solve_boards(file, guess, verbose): """Solve each board in a text file.""" if verbose: print('#', 'solved?', 'board', 'strategy', sep='\t') exclude = None if guess else [999] with open(file, 'r') as boards: for line in boards: line = line.strip() if not line or line.startswith('#'): continue board = Sudoku(line) n = board.num_solved() hardest = board.solve(exclude=exclude) try: board.verify() except: print('*** ERROR:', line) Sudoku(line).solve(exclude=exclude, verbose=True) break if verbose: print(board.num_solved() - n, 'TRUE' if board.solved() else 'FALSE', line, hardest, sep='\t')
def board(self): """ Set up board and detect events. :return: None """ game = Sudoku("easy") # Setting up the screen pygame.init() screen = pygame.display.set_mode((478,478)) screen.fill((250,250,250)) pygame.display.set_caption('CSI Sudoku') nums = game.board #Setting up the game board self.draw_lines(screen) # Setting up rectangles and nums for each box. self.set_up_rect(screen,nums) # Got some issue here, when the mouse button down, the area color is not changing. pressed = [] keys = {'1':pygame.K_1,'2':pygame.K_2,'3':pygame.K_3,'4':pygame.K_4,'5':pygame.K_5, '6':pygame.K_6,'7':pygame.K_7,'8':pygame.K_8,'9':pygame.K_9} while not game.game_over(): for event in pygame.event.get(): if event.type==pygame.QUIT: return elif event.type==pygame.MOUSEBUTTONUP: mouse = pygame.mouse.get_pos() x, y = self.gen_index(mouse[0], mouse[1]) if 0 <= x < 9 and 0 <= y < 9: if mouse[0] < self.list_rect[x][y].position[0] and mouse[1] < \ self.list_rect[x][y].position[1]: x = x - 1 y = y - 1 elif mouse[0] < self.list_rect[x][y].position[0]: x = x - 1 elif mouse[1] < self.list_rect[x][y].position[1]: y = y - 1 rect = self.list_rect[x][y] position = rect.position if rect.editable and (len(pressed)==0 or pressed[0]!=rect): pressed.append(rect) rect.select() rect.change_color((127,255,212)) screen.blit(rect.num_surface, (position[0] + 15, position[1] + 10)) if len(pressed)>1: old_rect = pressed.pop(0) pos = old_rect.position old_rect.unselect() old_rect.change_color(old_rect.color) screen.blit(old_rect.num_surface, (pos[0] + 15, pos[1] + 10)) elif event.type == pygame.KEYDOWN and len(pressed)>0: for num,key in keys.items(): if event.key == key: rect = pressed.pop(0) x, y = self.gen_index(rect.position[0], rect.position[1]) if game.move(x,y,int(num)): rect.change_num(num) rect.unselect() rect.change_color(rect.color) screen.blit(rect.num_surface, (rect.position[0] + 15, rect.position[1] + 10)) else: pressed.append(rect) pygame.display.update()
def solve_board(board, guess, verbose): """Solve a single board.""" board = Sudoku(board) exclude = None if guess else [999] board.solve(exclude=exclude, verbose=verbose) board.verify()