我工作的一個塔防遊戲在Python中,用戶的塔樓可以修改的敵人必須採取的路徑。爲了計算路徑,我認爲Lee算法的實現可能是最好的和最簡單的,特別是如果網格變大。我試圖鬆散地基於我的算法。Python的迷宮路線調查
我的代碼,但是,不能正常工作。我不明白爲什麼。
def getRoute(self):
self.routes = [[(0,0)]]
def _getRoutes():
new_routes = []
for route in self.routes:
for i in [(1,0),(-1,0),(0,1),(0,-1)]:
new_loc = [x + y for x,y in zip(route[-1],i)]
for index in new_loc:
if index < 0:
print('< 0')
continue ## if the index causes a skip across the side of the array, ignore
try:
if self.level.map[new_loc[0]][new_loc[1]].travellable: ## if the tile is able to be travelled on by enemies
route.append(new_loc) ## add the tile to the 'r' array
new_routes.append(route) ## add the new route to the new_routes array
except IndexError: ## if the tile is off the map, ignore
print('index error')
self.routes = new_routes
def _checkRoutesValidity():
for route in self.routes:
if self.level.map[route[-1][0]][route[-1][1]].access == 5:
return route
break
else:
return None
while not _checkRoutesValidity():
_getRoutes()
self.route = _checkRoutesValidity()
for i,j in self.route:
self.level.map[i][j].overlay_col = [0,1,0,1]
路線是,應該包含所有可能路徑的敵人可以利用,並最終與正確路線的變量。目前,該算法應該以最快的綠色陰影路線。 Level是一個level對象,level.map是一個Grid對象的二維數組,每個對象都是一個單獨的單元格。如果cell.access爲5,則意味着它是敵人的出口點。
所有這一切實際發生的,是它創建的元組讀取一個令人難以置信的長列表(0,1)和(1,0)。沒有負數是不斷產生的,沒有什麼以外的1或0
可有人請點我在正確的方向要麼建立一個適當的李算法或修復我當前的代碼?
這是一個李算法的方法? – TemporalWolf
@TemporalWolf代碼嘗試從一個點向外繪製路線,類似於填充洪水。它受lee算法的影響,但它並不代表它。現在不正確的問題 – JellyWX