Working Dijkstra

This commit is contained in:
Nicolas Margulies 2020-12-08 22:22:20 +01:00
parent 7823a422b9
commit 50d806cdcf
2 changed files with 9 additions and 9 deletions

View File

@ -29,16 +29,16 @@ class MapDisplay(Display):
for x in range(self.map.width):
for y in range(self.map.height):
if (y,x) in player.paths:
deltay, deltax = (y - player.path[(y, x)][0],
x - player.path[(y, x)][1])
deltay, deltax = (y - player.paths[(y, x)][0],
x - player.paths[(y, x)][1])
if (deltay, deltax) == (-1, 0):
character = ''
character = ''
elif (deltay, deltax) == (1, 0):
character = ''
character = ''
elif (deltay, deltax) == (0, -1):
character = ''
character = ''
else:
character = ''
character = ''
self.addstr(self.pad, y, self.pack.tile_width * x,
character, self.color_pair(1))

View File

@ -101,7 +101,7 @@ class Player(FightingEntity):
visited = []
distances = {(self.y, self.x): 0}
predecessors = {}
while not queue.empty:
while not queue.empty():
dist, (y, x) = queue.get()
if dist >= max_distance or (y,x) in visited:
continue
@ -110,14 +110,14 @@ class Player(FightingEntity):
new_y, new_x = y + diff_y, x + diff_x
if not 0 <= new_y < self.map.height or \
not 0 <= new_x < self.map.width or \
not self.map.tiles[y][x].can_walk():
not self.map.tiles[new_y][new_x].can_walk():
continue
new_distance = dist + 1
if not (new_y, new_x) in distances or \
distances[(new_y, new_x)] > new_distance:
predecessors[(new_y, new_x)] = (y, x)
distances[(new_y, new_x)] = new_distance
queue.put(new_distance, (new_y, new_x))
queue.put((new_distance, (new_y, new_x)))
self.paths = predecessors
def save_state(self) -> dict: