Skip to content

Commit 24f7baf

Browse files
committed
added length of the path
1 parent e742bdf commit 24f7baf

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

projet_astar.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import random
33
import time
44

5-
root = Tk()
6-
75
TIME = 0.05
86

97
CELL_SIZE = 40
@@ -83,7 +81,7 @@ def create_path():
8381
while pos2 == pos1 or pos2 in OBSTACLES:
8482
pos2 = (random.randint(0,15), random.randint(0,15))
8583

86-
print("path between ", pos1, pos2)
84+
print("path between ", pos1, pos2, "length : ", end = "")
8785
x1, y1 = pos1
8886
x2, y2 = pos2
8987

@@ -93,28 +91,30 @@ def create_path():
9391
time.sleep(TIME)
9492
path = getPath(pos1, pos2)
9593
if path != -1:
94+
print(len(path))
9695
for cell in path:
9796
x, y = cell
9897
grid.create_rectangle(x*CELL_SIZE, y*CELL_SIZE, (x+1)*CELL_SIZE, (y+1)*CELL_SIZE, fill='blue')
9998
else:
10099
print("NO PATH")
101100
grid.create_rectangle(x2*CELL_SIZE, y2*CELL_SIZE, (x2+1)*CELL_SIZE, (y2+1)*CELL_SIZE, fill='red')
102-
103-
startButton.config(state = NORMAL)
101+
startButton.config(state=NORMAL)
104102

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)
103+
if __name__ == "__main__":
111104

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()
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)
119112

120-
root.mainloop()
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()

0 commit comments

Comments
 (0)