示例#1
0
def findShortestPath():
    grid = Base.Grid("map3.txt", {
        "score": -1,
        "distance": -1,
        "heuristic": -1
    })  #Load map

    grid.printGrid()  #Show map

    currentX = grid.startX
    currentY = grid.startY
    opList = Base.Heap(100)
    #opList = Base.PriorityQueue()
    run = True

    #Set start node value
    grid.setAttr(currentX, currentY, "score", 0)
    grid.setAttr(currentX, currentY, "distance", 0)
    grid.setAttr(currentX, currentY, "heuristic", 0)
    grid.closeCell(currentX, currentY)

    addNeighbours(grid, opList, currentX, currentY)

    while (opList.length() > 0):  #While there are still possible routes
        nextCell = opList.pop(
            grid)  #Gets the node most likely to lead you to the end
        currentX = nextCell.x
        currentY = nextCell.y

        #Have we found the end
        if (currentX == grid.endX and currentY == grid.endY):
            printPath(grid)
            return

        #print(currentX, currentY)

        #Close the cell so it cannot be visited again
        grid.closeCell(currentX, currentY)

        #Add neighbouring nodes to open list
        addNeighbours(grid, opList, currentX, currentY)