Python Forum

Full Version: function understanding
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello Everyone.

I want to understand the concept behind this function or simply if anybody could explain the function as what its doing and how is it implemented? line by line- i will be more than happy. Thank you very much
def bfs_search(s, i, slow):

    global width, rows, snack, tempFood, startState, food

    def performActions(dirs, slow):
        for action in dirs:
            if slow:
                pygame.time.delay(50)
                clock.tick(10)
            s.moveAuto(action)
            redrawWindow(win, s)

    width = 400
    rows = 20
    win = pygame.display.set_mode((width, width))
    pygame.display.set_caption("inSignia - Naseem")
    startState = START_POS
    snack = cube(FOOD_POS[i], color=(0, 255, 0))
    tempFood = snack
    clock = pygame.time.Clock()
    flag = True

    bfs_queue = Queue()  # fringe
    visited = set()
    bfs_queue.push((s.getStartState(), []))

    while 1:
        if bfs_queue.isEmpty():
            break
        current, directions = bfs_queue.pop()
        # print("Current pos:", current)
        if current not in visited:
            visited.add(current)
            if s.isGoalState(current):
                s.score += 1
                s.addCube()
                performActions(directions, slow)
                # print("BFS number of actions:", len(directions))
                actionsList[1].append(len(directions))
                # print("BFS score:", len(s.body))
                scoreList[1] = len(s.body)
                # scoreList[1] = s.score
            for childNode, direction, cost in s.getSuccessors(current):
                if childNode not in bfs_queue.list:
                    if childNode in visited:
                        continue
                    bfs_queue.push((childNode, directions + [direction]))