-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyfinder.py
95 lines (74 loc) · 2.12 KB
/
pyfinder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import astar
import sys
if __name__ == "__main__":
if(len(sys.argv) != 2):
print("USAGE: python {0} file.map".format(sys.argv[0]))
sys.exit(1)
with open(sys.argv[1], 'r') as f:
fdata = f.readlines()
# print map
for line in fdata:
print(line, end='')
print()
# convert map file to usable format
# 1 = walkable cell
# 0 = unwalkable cell
fRows = len(fdata)
if(fRows == 0):
print("ERROR empty map")
sys.exit(1)
# skip last column of '\n'
fCols = len(fdata[0]) - 1
map = []
for r in range(fRows):
map.append([1] * fCols)
for r in range(fRows):
for c in range(fCols):
if(fdata[r][c] == '#'):
map[r][c] = 0
# get size of map
mapRows = len(map)
mapCols = len(map[0])
# ask user for START
r0 , c0 = (int(v) for v in tuple(input("START (a,b): ").split(',')))
# ask user for GOAL
r1 , c1 = (int(v) for v in tuple(input("GOAL (a,b): ").split(',')))
start = (r0, c0)
goal = (r1, c1)
print()
# find path
pf = astar.Pathfinder(map)
try:
path = pf.make_path(start, goal)
except astar.OutOfBoundsError as err:
print("ERROR - {}".format(err))
exit(1)
except astar.UnwalkableError as err:
print("ERROR - {}".format(err))
exit(1)
except astar.SameStartGoalError as err:
print("ERROR - {}".format(err))
exit(1)
if len(path) == 0:
print("NO PATH FOUND")
exit(0)
# print map with path
pathStart = path[0]
pathGoal = path[len(path) - 1]
pathSet = set(path)
for r in range(mapRows):
for c in range(mapCols):
if((r, c) in pathSet):
if((r, c) == pathStart):
print('S', end='')
elif((r, c) == pathGoal):
print('X', end='')
else:
print('.', end='')
else:
if map[r][c] == 1:
print(' ', end='')
else:
print('#', end='')
print('\n', end='')
print()