def game_loop():
    game_exit = False
    clock = pygame.time.Clock()
    global initial_cell_row
    global initial_cell_col
    global initial_cell_dragging
    global terminal_cell_row
    global terminal_cell_col
    global terminal_cell_dragging
    while (not game_exit):
        for event in pygame.event.get():
            if ((event.type == pygame.QUIT) and (not processing)):
                game_exit = True
            elif ((event.type == pygame.MOUSEBUTTONDOWN) and (not processing)):
                (mouse_x, mouse_y) = pygame.mouse.get_pos()
                cell_col = int((mouse_x / CELL_WIDTH))
                cell_row = int((mouse_y / CELL_HEIGHT))
                if valid_cell(cell_col, cell_row):
                    if (grid[(cell_col, cell_row)] == INITIAL):
                        initial_cell_dragging = True
                    elif (grid[(cell_col, cell_row)] == TERMINAL):
                        terminal_cell_dragging = True
                    elif (not (initial_cell_dragging or terminal_cell_dragging)):
                        if (grid[(cell_col, cell_row)] == WALL):
                            grid[(cell_col, cell_row)] = EMPTY
                        elif (grid[(cell_col, cell_row)] == EMPTY):
                            grid[(cell_col, cell_row)] = WALL
            elif ((event.type == pygame.MOUSEBUTTONUP) and (not processing)):
                if clear_button.is_over(mouse_x, mouse_y):
                    thread = threading.Thread(target=initialise, args=())
                    thread.start()
                elif create_button.is_over(mouse_x, mouse_y):
                    thread = threading.Thread(target=create_maze, args=())
                    thread.start()
                elif dfs_button.is_over(mouse_x, mouse_y):
                    thread = threading.Thread(target=depth_first_search, args=())
                    thread.start()
                elif bfs_button.is_over(mouse_x, mouse_y):
                    thread = threading.Thread(target=breadth_first_search, args=())
                    thread.start()
                elif quit_button.is_over(mouse_x, mouse_y):
                    game_exit = True
                elif initial_cell_dragging:
                    (mouse_x, mouse_y) = pygame.mouse.get_pos()
                    cell_col = int((mouse_x / CELL_WIDTH))
                    cell_row = int((mouse_y / CELL_HEIGHT))
                    if valid_cell(cell_col, cell_row):
                        if (not ((cell_col == terminal_cell_col) and (cell_row == terminal_cell_row))):
                            grid[(initial_cell_col, initial_cell_row)] = EMPTY
                            initial_cell_col = cell_col
                            initial_cell_row = cell_row
                            grid[(initial_cell_col, initial_cell_row)] = INITIAL
                    initial_cell_dragging = False
                elif terminal_cell_dragging:
                    (mouse_x, mouse_y) = pygame.mouse.get_pos()
                    cell_col = int((mouse_x / CELL_WIDTH))
                    cell_row = int((mouse_y / CELL_HEIGHT))
                    if valid_cell(cell_col, cell_row):
                        if (not ((cell_col == initial_cell_col) and (cell_row == initial_cell_row))):
                            grid[(terminal_cell_col, terminal_cell_row)] = EMPTY
                            terminal_cell_col = cell_col
                            terminal_cell_row = cell_row
                            grid[(terminal_cell_col, terminal_cell_row)] = TERMINAL
                    terminal_cell_dragging = False
        draw_grid()
        pygame.display.update()
        clock.tick(CLOCK_TICK)
    pygame.quit()
def game_loop():
    game_exit = False
    clock = pygame.time.Clock()
    global initial_cell_row
    global initial_cell_col
    global initial_cell_dragging
    global terminal_cell_row
    global terminal_cell_col
    global terminal_cell_dragging
    while (not game_exit):
        for event in pygame.event.get():
            if ((event.type == pygame.QUIT) and (not processing)):
                game_exit = True
            elif ((event.type == pygame.MOUSEBUTTONDOWN) and (not processing)):
                (mouse_x, mouse_y) = pygame.mouse.get_pos()
                cell_col = int((mouse_x / CELL_WIDTH))
                cell_row = int((mouse_y / CELL_HEIGHT))
                if valid_cell(cell_col, cell_row):
                    if (grid[(cell_col, cell_row)] == INITIAL):
                        initial_cell_dragging = True
                    elif (grid[(cell_col, cell_row)] == TERMINAL):
                        terminal_cell_dragging = True
                    elif (not (initial_cell_dragging or terminal_cell_dragging)):
                        if (grid[(cell_col, cell_row)] == WALL):
                            grid[(cell_col, cell_row)] = EMPTY
                        elif (grid[(cell_col, cell_row)] == EMPTY):
                            grid[(cell_col, cell_row)] = WALL
            elif ((event.type == pygame.MOUSEBUTTONUP) and (not processing)):
                if clear_button.is_over(mouse_x, mouse_y):
                    thread = threading.Thread(target=initialise, args=())
                    thread.start()
                elif create_button.is_over(mouse_x, mouse_y):
                    thread = threading.Thread(target=create_maze, args=())
                    thread.start()
                elif dfs_button.is_over(mouse_x, mouse_y):
                    thread = threading.Thread(target=depth_first_search, args=())
                    thread.start()
                elif bfs_button.is_over(mouse_x, mouse_y):
                    thread = threading.Thread(target=breadth_first_search, args=())
                    thread.start()
                elif quit_button.is_over(mouse_x, mouse_y):
                    game_exit = True
                elif initial_cell_dragging:
                    (mouse_x, mouse_y) = pygame.mouse.get_pos()
                    cell_col = int((mouse_x / CELL_WIDTH))
                    cell_row = int((mouse_y / CELL_HEIGHT))
                    if valid_cell(cell_col, cell_row):
                        if (not ((cell_col == terminal_cell_col) and (cell_row == terminal_cell_row))):
                            grid[(initial_cell_col, initial_cell_row)] = EMPTY
                            initial_cell_col = cell_col
                            initial_cell_row = cell_row
                            grid[(initial_cell_col, initial_cell_row)] = INITIAL
                    initial_cell_dragging = False
                elif terminal_cell_dragging:
                    (mouse_x, mouse_y) = pygame.mouse.get_pos()
                    cell_col = int((mouse_x / CELL_WIDTH))
                    cell_row = int((mouse_y / CELL_HEIGHT))
                    if valid_cell(cell_col, cell_row):
                        if (not ((cell_col == initial_cell_col) and (cell_row == initial_cell_row))):
                            grid[(terminal_cell_col, terminal_cell_row)] = EMPTY
                            terminal_cell_col = cell_col
                            terminal_cell_row = cell_row
                            grid[(terminal_cell_col, terminal_cell_row)] = TERMINAL
                    terminal_cell_dragging = False
        draw_grid()
        pygame.display.update()
        clock.tick(CLOCK_TICK)
    pygame.quit()