Exemple #1
0
 def on_click(self, event):
     x, y = event.x, event.y
     c, r = self.pix_to_cell((x, y))
     if self.gamestate == END_STATE:
         self.reset()
         self.gamestate = MENU_STATE
         self.title_screen()
     elif self.gamestate == MENU_STATE:
         btn = self.pix_to_button((x, y))
         if btn != None:
             self.reset()
             self.first_isX = not self.first_isX
             self.gamestate = X_TURN_STATE if self.first_isX else O_TURN_STATE
             if btn == 0:
                 self.is_multi = False
                 self.diff = 100
             elif btn == 1:
                 self.is_multi = False
                 self.diff = 50
             elif btn == 2:
                 self.is_multi = False
                 self.diff = 0
             elif btn == 3:
                 self.is_multi = True
     elif (
         (self.gamestate == X_TURN_STATE or self.gamestate == O_TURN_STATE)
             and board.is_valid(self.game_board, (r, c))):
         cur_player = 1 if self.gamestate == X_TURN_STATE else 0
         if (self.is_multi or cur_player == 1):
             self.new_move((r, c))
             self.moves_remaining -= 1
             if self.has_won((r, c)):
                 self.is_won = True
                 self.gamestate = END_STATE
                 self.game_over_screen(cur_player + 1)
             elif self.has_tie():
                 self.gamestate = END_STATE
                 self.game_over_screen(0)
             else:
                 self.gamestate = int(not (self.gamestate - 1)) + 1
         if not self.is_multi and self.moves_remaining > 0:
             r1, c1 = minimax.best_move(self.game_board, False, self.diff,
                                        self.moves_remaining)
             self.new_move((r1, c1))
             self.moves_remaining -= 1
             if self.has_won((r1, c1)):
                 self.is_won = True
                 self.gamestate = END_STATE
                 self.game_over_screen(1)
             elif self.has_tie():
                 self.gamestate = END_STATE
                 self.game_over_screen(0)
             else:
                 self.gamestate = int(not (self.gamestate - 1)) + 1
Exemple #2
0
    def count_solutions(self, board, maximum = 100):
        if board.is_valid():
            return 1
        
        self.initialize_vars(board)
        self.goal = lambda solver : solver.done or solver.solutions >= maximum

        start = t.time()
        if not board.is_possible():
            self.done = True

        self.step()
        end = t.time()

        print('Sudoku board solved in ' + str(end - start))
        self.clean_board()
        return self.solutions
Exemple #3
0
 def test_is_valid(self):
     test_board_empty = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']]
     test_board_full = [['X', 'O', 'O'], ['O', 'X', 'X'], ['X', 'X', '0']]
     for i in range(3):
         for j in range(3):
             self.assertTrue(board.is_valid(test_board_empty, (i, j)))
     for i in range(3):
         for j in range(3):
             self.assertFalse(board.is_valid(test_board_full, (i, j)))
     self.assertFalse(board.is_valid(test_board_empty, (-1, -1)))
     self.assertFalse(board.is_valid(test_board_full, (4, 4)))
     self.assertFalse(board.is_valid(test_board_empty, (-100, 0)))
     self.assertFalse(board.is_valid(test_board_empty, (2, 1000)))
Exemple #4
0
    def solve(self, board):
        if board.is_valid():
            return board

        self.initialize_vars(board)
        self.goal = lambda solver : solver.done or solver.board.is_valid()

        start = t.time()
        if not board.is_possible():
            self.done = True

        self.step()
        end = t.time()

        print('Sudoku board solved in ' + str(end - start))

        if self.done:
            return board_impossible()
        else:
            return board