|
1 |
| -from tkinter import * |
2 |
| -import random |
3 |
| -import time |
4 |
| - |
5 |
| -TIME = 0.05 |
6 |
| - |
7 |
| -CELL_SIZE = 40 |
8 |
| - |
9 |
| -NUMBER_OF_OBSTACLES = 90 |
10 |
| - |
11 |
| -def getDistance(pos1, pos2): |
12 |
| - """Returns the Manhattan distance between pos1 and pos2""" |
13 |
| - return abs(pos1[0]-pos2[0])+abs(pos1[1]-pos2[1]) |
14 |
| - |
15 |
| -def getPath(start, end): |
16 |
| - """Returns a path between start and end, if it exists""" |
17 |
| - global OBSTACLES |
18 |
| - if start == end: |
19 |
| - return -1 |
20 |
| - openList = [start] |
21 |
| - nodes = {start : ['',0]} # nodes['[x, y]'] = [parent, distance from the start] |
22 |
| - closedList = [] |
23 |
| - x1, y1 = start |
24 |
| - while openList: |
25 |
| - current = openList[0] |
26 |
| - for tmp in openList: |
27 |
| - if nodes[tmp][1] + getDistance(tmp, end) < nodes[current][1] + getDistance(current, end): |
28 |
| - current = tmp |
29 |
| - x, y = current |
30 |
| - grid.create_rectangle(x*CELL_SIZE, y*CELL_SIZE, (x+1)*CELL_SIZE, (y+1)*CELL_SIZE, fill='yellow') |
31 |
| - grid.create_rectangle(x1*CELL_SIZE, y1*CELL_SIZE, (x1+1)*CELL_SIZE, (y1+1)*CELL_SIZE, fill='green') |
32 |
| - if current == end: |
33 |
| - break |
34 |
| - |
35 |
| - del openList[openList.index(current)] |
36 |
| - closedList.append(current) |
37 |
| - |
38 |
| - for a,b in [(0,1), (0,-1), (1,0), (-1,0)]: |
39 |
| - X = current[0] + a |
40 |
| - Y = current[1] + b |
41 |
| - if (X, Y) in OBSTACLES or (X,Y) in closedList or X < 0 or X > 15 or Y < 0 or Y > 15: |
42 |
| - continue |
43 |
| - elif not (X,Y) in openList: |
44 |
| - openList.append((X,Y)) |
45 |
| - nodes[(X,Y)] = [current, nodes[current][1] + 1] |
46 |
| - elif not (X,Y) in nodes or nodes[(X,Y)][1] > nodes[current][1] + 1: |
47 |
| - nodes[(X,Y)] = [current, nodes[current][1] + 1] |
48 |
| - root.update() |
49 |
| - |
50 |
| - time.sleep(TIME) |
51 |
| - grid.create_rectangle(x*CELL_SIZE, y*CELL_SIZE, (x+1)*CELL_SIZE, (y+1)*CELL_SIZE, fill='#bbbbdd') |
52 |
| - |
53 |
| - if current != end: # if the path does not exist |
54 |
| - return -1 |
55 |
| - |
56 |
| - tmp = nodes[end][0] |
57 |
| - path = [end] |
58 |
| - while tmp != start: # rewind the parents of the nodes to get the path |
59 |
| - path.append(tmp) |
60 |
| - tmp = nodes[tmp][0] |
61 |
| - return path[::-1] |
62 |
| - |
63 |
| -def refreshMap(): |
64 |
| - global CELL_SIZE |
65 |
| - grid.delete(ALL) |
66 |
| - for x in range(16): |
67 |
| - grid.create_line(0, x*CELL_SIZE, 16*CELL_SIZE, x*CELL_SIZE) |
68 |
| - grid.create_line(x*CELL_SIZE, 0, x*CELL_SIZE, 16*CELL_SIZE) |
69 |
| - for obstacle in OBSTACLES: |
70 |
| - x, y = obstacle |
71 |
| - grid.create_rectangle(x*CELL_SIZE, y*CELL_SIZE, (x+1)*CELL_SIZE, (y+1)*CELL_SIZE, fill='black') |
72 |
| - |
73 |
| -def create_path(): |
74 |
| - startButton.config(state = DISABLED) |
75 |
| - global CELL_SIZE |
76 |
| - refreshMap() |
77 |
| - pos1 = (random.randint(0,15), random.randint(0,15)) |
78 |
| - while pos1 in OBSTACLES: |
79 |
| - pos1 = (random.randint(0,15), random.randint(0,15)) |
80 |
| - pos2 = pos1 |
81 |
| - while pos2 == pos1 or pos2 in OBSTACLES: |
82 |
| - pos2 = (random.randint(0,15), random.randint(0,15)) |
83 |
| - |
84 |
| - print("path between ", pos1, pos2, "length : ", end = "") |
85 |
| - x1, y1 = pos1 |
86 |
| - x2, y2 = pos2 |
87 |
| - |
88 |
| - grid.create_rectangle(x1*CELL_SIZE, y1*CELL_SIZE, (x1+1)*CELL_SIZE, (y1+1)*CELL_SIZE, fill='green') |
89 |
| - grid.create_rectangle(x2*CELL_SIZE, y2*CELL_SIZE, (x2+1)*CELL_SIZE, (y2+1)*CELL_SIZE, fill='red') |
90 |
| - root.update() |
91 |
| - time.sleep(TIME) |
92 |
| - path = getPath(pos1, pos2) |
93 |
| - if path != -1: |
94 |
| - print(len(path)) |
95 |
| - for cell in path: |
96 |
| - x, y = cell |
97 |
| - grid.create_rectangle(x*CELL_SIZE, y*CELL_SIZE, (x+1)*CELL_SIZE, (y+1)*CELL_SIZE, fill='blue') |
98 |
| - else: |
99 |
| - print("NO PATH") |
100 |
| - grid.create_rectangle(x2*CELL_SIZE, y2*CELL_SIZE, (x2+1)*CELL_SIZE, (y2+1)*CELL_SIZE, fill='red') |
101 |
| - startButton.config(state=NORMAL) |
102 |
| - |
103 |
| -if __name__ == "__main__": |
104 |
| - |
105 |
| - root = Tk() |
106 |
| - grid = Canvas(root, width = 16*CELL_SIZE, height = 16*CELL_SIZE, bg = 'white') |
107 |
| - grid.pack() |
108 |
| - OBSTACLES = [] |
109 |
| - for x in range(16): |
110 |
| - grid.create_line(0, x*CELL_SIZE, 16*CELL_SIZE, x*CELL_SIZE) |
111 |
| - grid.create_line(x*CELL_SIZE, 0, x*CELL_SIZE, 16*CELL_SIZE) |
112 |
| - |
113 |
| - for i in range(NUMBER_OF_OBSTACLES): |
114 |
| - x, y = random.randint(0, 15), random.randint(0, 15) |
115 |
| - OBSTACLES.append((x, y)) |
116 |
| - grid.create_rectangle(x*CELL_SIZE, y*CELL_SIZE, (x+1)*CELL_SIZE, (y+1)*CELL_SIZE, fill='black') |
117 |
| - |
118 |
| - startButton = Button(root, text = 'start', command = create_path) |
119 |
| - startButton.pack() |
120 |
| - root.mainloop() |
| 1 | +from tkinter import * |
| 2 | +import random |
| 3 | +import time |
| 4 | + |
| 5 | +TIME = 0.05 |
| 6 | + |
| 7 | +CELL_SIZE = 40 |
| 8 | + |
| 9 | +NUMBER_OF_OBSTACLES = 90 |
| 10 | + |
| 11 | +def getDistance(pos1, pos2): |
| 12 | + """Returns the Manhattan distance between pos1 and pos2""" |
| 13 | + return abs(pos1[0]-pos2[0])+abs(pos1[1]-pos2[1]) |
| 14 | + |
| 15 | +def getPath(start, end, OBSTACLES): |
| 16 | + """Returns a path between start and end, if it exists""" |
| 17 | + if start == end: |
| 18 | + return -1 |
| 19 | + openList = [start] |
| 20 | + nodes = {start : ['',0]} # nodes['[x, y]'] = [parent, distance from the start] |
| 21 | + closedList = [] |
| 22 | + x1, y1 = start |
| 23 | + while openList: |
| 24 | + current = openList[0] |
| 25 | + for tmp in openList: |
| 26 | + if nodes[tmp][1] + getDistance(tmp, end) < nodes[current][1] + getDistance(current, end): |
| 27 | + current = tmp |
| 28 | + x, y = current |
| 29 | + #grid.create_rectangle(x*CELL_SIZE, y*CELL_SIZE, (x+1)*CELL_SIZE, (y+1)*CELL_SIZE, fill='yellow') |
| 30 | + #grid.create_rectangle(x1*CELL_SIZE, y1*CELL_SIZE, (x1+1)*CELL_SIZE, (y1+1)*CELL_SIZE, fill='green') |
| 31 | + if current == end: |
| 32 | + break |
| 33 | + |
| 34 | + del openList[openList.index(current)] |
| 35 | + closedList.append(current) |
| 36 | + |
| 37 | + for a,b in [(0,1), (0,-1), (1,0), (-1,0)]: |
| 38 | + X = current[0] + a |
| 39 | + Y = current[1] + b |
| 40 | + if (X, Y) in OBSTACLES or (X,Y) in closedList or X < 0 or X > 15 or Y < 0 or Y > 15: |
| 41 | + continue |
| 42 | + elif not (X,Y) in openList: |
| 43 | + openList.append((X,Y)) |
| 44 | + nodes[(X,Y)] = [current, nodes[current][1] + 1] |
| 45 | + elif not (X,Y) in nodes or nodes[(X,Y)][1] > nodes[current][1] + 1: |
| 46 | + nodes[(X,Y)] = [current, nodes[current][1] + 1] |
| 47 | + #root.update() |
| 48 | + |
| 49 | + #time.sleep(TIME) |
| 50 | + #grid.create_rectangle(x*CELL_SIZE, y*CELL_SIZE, (x+1)*CELL_SIZE, (y+1)*CELL_SIZE, fill='#bbbbdd') |
| 51 | + |
| 52 | + if current != end: # if the path does not exist |
| 53 | + return -1, closedList |
| 54 | + |
| 55 | + tmp = nodes[end][0] |
| 56 | + path = [end] |
| 57 | + while tmp != start: # rewind the parents of the nodes to get the path |
| 58 | + path.append(tmp) |
| 59 | + tmp = nodes[tmp][0] |
| 60 | + return path[::-1], closedList |
| 61 | + |
| 62 | +def refreshMap(): |
| 63 | + global CELL_SIZE |
| 64 | + grid.delete(ALL) |
| 65 | + for x in range(16): |
| 66 | + grid.create_line(0, x*CELL_SIZE, 16*CELL_SIZE, x*CELL_SIZE) |
| 67 | + grid.create_line(x*CELL_SIZE, 0, x*CELL_SIZE, 16*CELL_SIZE) |
| 68 | + for obstacle in OBSTACLES: |
| 69 | + x, y = obstacle |
| 70 | + grid.create_rectangle(x*CELL_SIZE, y*CELL_SIZE, (x+1)*CELL_SIZE, (y+1)*CELL_SIZE, fill='black') |
| 71 | + |
| 72 | +def create_path(): |
| 73 | + startButton.config(state = DISABLED) |
| 74 | + global CELL_SIZE |
| 75 | + refreshMap() |
| 76 | + pos1 = (random.randint(0,15), random.randint(0,15)) |
| 77 | + while pos1 in OBSTACLES: |
| 78 | + pos1 = (random.randint(0,15), random.randint(0,15)) |
| 79 | + pos2 = pos1 |
| 80 | + while pos2 == pos1 or pos2 in OBSTACLES: |
| 81 | + pos2 = (random.randint(0,15), random.randint(0,15)) |
| 82 | + |
| 83 | + print("path between", pos1, pos2, "length : ", end = "") |
| 84 | + x1, y1 = pos1 |
| 85 | + x2, y2 = pos2 |
| 86 | + |
| 87 | + grid.create_rectangle(x1*CELL_SIZE, y1*CELL_SIZE, (x1+1)*CELL_SIZE, (y1+1)*CELL_SIZE, fill='green') |
| 88 | + grid.create_rectangle(x2*CELL_SIZE, y2*CELL_SIZE, (x2+1)*CELL_SIZE, (y2+1)*CELL_SIZE, fill='red') |
| 89 | + root.update() |
| 90 | + time.sleep(TIME) |
| 91 | + path, debug = getPath(pos1, pos2, OBSTACLES) |
| 92 | + if path != -1: |
| 93 | + print(len(path)) |
| 94 | + for cell in path: |
| 95 | + x, y = cell |
| 96 | + grid.create_rectangle(x*CELL_SIZE, y*CELL_SIZE, (x+1)*CELL_SIZE, (y+1)*CELL_SIZE, fill='blue') |
| 97 | + else: |
| 98 | + print("NO PATH") |
| 99 | + grid.create_rectangle(x2*CELL_SIZE, y2*CELL_SIZE, (x2+1)*CELL_SIZE, (y2+1)*CELL_SIZE, fill='red') |
| 100 | + startButton.config(state=NORMAL) |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + |
| 104 | + root = Tk() |
| 105 | + grid = Canvas(root, width = 16*CELL_SIZE, height = 16*CELL_SIZE, bg = 'white') |
| 106 | + grid.pack() |
| 107 | + OBSTACLES = [] |
| 108 | + for x in range(16): |
| 109 | + grid.create_line(0, x*CELL_SIZE, 16*CELL_SIZE, x*CELL_SIZE) |
| 110 | + grid.create_line(x*CELL_SIZE, 0, x*CELL_SIZE, 16*CELL_SIZE) |
| 111 | + |
| 112 | + for i in range(NUMBER_OF_OBSTACLES): |
| 113 | + x, y = random.randint(0, 15), random.randint(0, 15) |
| 114 | + OBSTACLES.append((x, y)) |
| 115 | + grid.create_rectangle(x*CELL_SIZE, y*CELL_SIZE, (x+1)*CELL_SIZE, (y+1)*CELL_SIZE, fill='black') |
| 116 | + |
| 117 | + startButton = Button(root, text = 'start', command = create_path) |
| 118 | + startButton.pack() |
| 119 | + root.mainloop() |
0 commit comments