Working Dijkstra
This commit is contained in:
parent
7823a422b9
commit
50d806cdcf
|
@ -29,16 +29,16 @@ class MapDisplay(Display):
|
||||||
for x in range(self.map.width):
|
for x in range(self.map.width):
|
||||||
for y in range(self.map.height):
|
for y in range(self.map.height):
|
||||||
if (y,x) in player.paths:
|
if (y,x) in player.paths:
|
||||||
deltay, deltax = (y - player.path[(y, x)][0],
|
deltay, deltax = (y - player.paths[(y, x)][0],
|
||||||
x - player.path[(y, x)][1])
|
x - player.paths[(y, x)][1])
|
||||||
if (deltay, deltax) == (-1, 0):
|
if (deltay, deltax) == (-1, 0):
|
||||||
character = '╹'
|
character = '↓'
|
||||||
elif (deltay, deltax) == (1, 0):
|
elif (deltay, deltax) == (1, 0):
|
||||||
character = '╻'
|
character = '↑'
|
||||||
elif (deltay, deltax) == (0, -1):
|
elif (deltay, deltax) == (0, -1):
|
||||||
character = '╺'
|
character = '→'
|
||||||
else:
|
else:
|
||||||
character = '╸'
|
character = '←'
|
||||||
self.addstr(self.pad, y, self.pack.tile_width * x,
|
self.addstr(self.pad, y, self.pack.tile_width * x,
|
||||||
character, self.color_pair(1))
|
character, self.color_pair(1))
|
||||||
|
|
||||||
|
|
|
@ -101,7 +101,7 @@ class Player(FightingEntity):
|
||||||
visited = []
|
visited = []
|
||||||
distances = {(self.y, self.x): 0}
|
distances = {(self.y, self.x): 0}
|
||||||
predecessors = {}
|
predecessors = {}
|
||||||
while not queue.empty:
|
while not queue.empty():
|
||||||
dist, (y, x) = queue.get()
|
dist, (y, x) = queue.get()
|
||||||
if dist >= max_distance or (y,x) in visited:
|
if dist >= max_distance or (y,x) in visited:
|
||||||
continue
|
continue
|
||||||
|
@ -110,14 +110,14 @@ class Player(FightingEntity):
|
||||||
new_y, new_x = y + diff_y, x + diff_x
|
new_y, new_x = y + diff_y, x + diff_x
|
||||||
if not 0 <= new_y < self.map.height or \
|
if not 0 <= new_y < self.map.height or \
|
||||||
not 0 <= new_x < self.map.width 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
|
continue
|
||||||
new_distance = dist + 1
|
new_distance = dist + 1
|
||||||
if not (new_y, new_x) in distances or \
|
if not (new_y, new_x) in distances or \
|
||||||
distances[(new_y, new_x)] > new_distance:
|
distances[(new_y, new_x)] > new_distance:
|
||||||
predecessors[(new_y, new_x)] = (y, x)
|
predecessors[(new_y, new_x)] = (y, x)
|
||||||
distances[(new_y, new_x)] = new_distance
|
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
|
self.paths = predecessors
|
||||||
|
|
||||||
def save_state(self) -> dict:
|
def save_state(self) -> dict:
|
||||||
|
|
Loading…
Reference in New Issue