2
2
import random
3
3
import time
4
4
5
- root = Tk ()
6
-
7
5
TIME = 0.05
8
6
9
7
CELL_SIZE = 40
@@ -83,7 +81,7 @@ def create_path():
83
81
while pos2 == pos1 or pos2 in OBSTACLES :
84
82
pos2 = (random .randint (0 ,15 ), random .randint (0 ,15 ))
85
83
86
- print ("path between " , pos1 , pos2 )
84
+ print ("path between " , pos1 , pos2 , "length : " , end = "" )
87
85
x1 , y1 = pos1
88
86
x2 , y2 = pos2
89
87
@@ -93,28 +91,30 @@ def create_path():
93
91
time .sleep (TIME )
94
92
path = getPath (pos1 , pos2 )
95
93
if path != - 1 :
94
+ print (len (path ))
96
95
for cell in path :
97
96
x , y = cell
98
97
grid .create_rectangle (x * CELL_SIZE , y * CELL_SIZE , (x + 1 )* CELL_SIZE , (y + 1 )* CELL_SIZE , fill = 'blue' )
99
98
else :
100
99
print ("NO PATH" )
101
100
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 )
104
102
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__" :
111
104
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 )
119
112
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