def set_val(self,val):
     i,j = self.selected_pos
     if self.board[i][j] == 0:
         self.cells[i][j].value = val
         self.set_board()
         if valid(self.board,i,j, val) and solve_board(self.board):
             return True
         else:
             self.cells[i][j].value =  0
             self.cells[i][j].tmp = 0
             self.board = self.board
             self.set_board()
             return False
Пример #2
0
    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
Пример #3
0
    def place(self, val):
        row, col = self.selected
        if self.cubes[row][col].value == 0:
            self.cubes[row][col].set(val)
            self.update_board()

            if valid(self.board, row, col, val) and solve_board(self.board):
                return True
            else:
                self.cubes[row][col].set(0)
                self.cubes[row][col].set_temp(0)
                self.update_board()
                return False
Пример #4
0
    def place(self, val):

        # sets selected cell with value
        row, col = self.selected
        if self.cells[row][col].value == 0:
            self.cells[row][col].set(val)
            self.update_model()

        # checks if value is correct (if correct place it, if not remove it)
        if valid(self.model, val, (row, col)) and solve(self.model):
            self.board[row][col] = val
            return True
        else:
            self.cells[row][col].set(0)
            self.cells[row][col].set_temp(0)
            self.update_model()
            return False
Пример #5
0
def main():
    # Initializing game and play time
    pygame.init()
    play_time = 0

    finished = False
    key = None

    screen = pygame.display.set_mode((540, 600))
    pygame.display.set_caption("Sudoku")

    # Initializing board and sudoku table
    play_board = Board()
    play_board.initialize_board()
    sudoku_board = play_board.playing_board

    # Starting the time
    start = time.time()

    running = True

    # Game loop
    while running:
        # Updates time as long as game isn't finished yet
        if not finished:
            play_time = round(time.time() - start)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_1:
                    key = 1
                if event.key == pygame.K_2:
                    key = 2
                if event.key == pygame.K_3:
                    key = 3
                if event.key == pygame.K_4:
                    key = 4
                if event.key == pygame.K_5:
                    key = 5
                if event.key == pygame.K_6:
                    key = 6
                if event.key == pygame.K_7:
                    key = 7
                if event.key == pygame.K_8:
                    key = 8
                if event.key == pygame.K_9:
                    key = 9
                if event.key == pygame.K_DELETE:
                    play_board.temp = None
                    key = None

                # Solves the board if user clicks space bar
                # TODO: Change this to a "Solve it!" button at the bottom
                if event.key == pygame.K_SPACE:
                    solver.solve(sudoku_board, True)
                    finished = True

                # Checks if current move is valid
                # Allows if so, does nothing if not
                # TODO: show clearly to the user that the move is invalid, rather than just doing nothing
                if event.key == pygame.K_RETURN:
                    i, j = play_board.selected_key

                    # Checks if user actually put in a move and if selected space is empty
                    if play_board.temp is not None and sudoku_board[i][j] == 0:

                        # Checks if move is valid
                        if solver.valid(i, j, play_board.temp, play_board.playing_board):

                            sudoku_board[i][j] = play_board.temp

                            # Checks if the board created by valid move is solvable
                            # Negates the move if not
                            if solver.solve(play_board.playing_board):
                                sudoku_board[i][j] = play_board.temp
                                play_board.temp = None
                            else:
                                sudoku_board[i][j] = 0
                        key = None

                        if solver.find_empty_element(sudoku_board) is None:
                            finished = True
                            print("You win!")

            if event.type == pygame.MOUSEBUTTONDOWN:
                play_board.temp = None

                pos = pygame.mouse.get_pos()
                button_clicked = click(pos)
                if button_clicked:
                    play_board.select(button_clicked[0], button_clicked[1])
                    key = None

        if play_board.selected_key and key is not None:
            play_board.temp = key

        update_window(screen, play_board, play_time)
        pygame.display.flip()