1

だから私は 9x9 グリッドでパスを計画しようとしているので、boardSize は 9 です。while ループは停止する必要があります。パス リストの長さは 81 以上なので、クリーチャーが7.5 でゴールは 5.2 で標高は 0 ですか? 私の while ループは間違っていますか、それとも他の場所にあると思いますか?

def planPath(self, creature, goal, board):
        print("in the path")      
        path = [board[creature.x][creature.y]]       
        while goal not in path or len(path) < self.boardSize ** 2:
            print("path length")
            print(len(path))
            nextPossible = {}
            for neighbor in path[-1].neighbors:
                if type(neighbor) is not Land.Water:
                    nextPossible[neighbor] = abs(neighbor.location[0] - goal.location[0]) + abs(neighbor.location[1] - goal.location[1]) + abs(neighbor.elevation - goal.elevation)      
            path.append(min(nextPossible, key=nextPossible.get))
        return path
4

1 に答える 1

2

whileパスの長さがボード上の正方形の数に達したときにループを終了する必要がありました。 while ループの代わりに使用するandor、次の式のいずれかで終了します。

goal not in path

またはこの表現:

len(path) < self.boardSize ** 2

に評価されFalseます。を使用するorと、これらの式のいずれかが true である限り、ループは実行され続けます。したがって、固定コードは次のようになります。

def planPath(self, creature, goal, board):
        print("in the path")      
        path = [board[creature.x][creature.y]]       
        while goal not in path and len(path) < self.boardSize ** 2:
            print("path length")
            print(len(path))
            nextPossible = {}
            for neighbor in path[-1].neighbors:
                if type(neighbor) is not Land.Water:
                    nextPossible[neighbor] = abs(neighbor.location[0] - goal.location[0]) + abs(neighbor.location[1] - goal.location[1]) + abs(neighbor.elevation - goal.elevation)      
            path.append(min(nextPossible, key=nextPossible.get))
        return path
于 2013-03-28T07:32:55.377 に答える