1515STATE_END = 2
1616
1717def refresh_map ():
18- global CELL_SIZE , SIZE
18+ global CELL_SIZE , SIZE , start , end
1919 grid .delete (ALL )
2020 for x in range (SIZE ):
2121 grid .create_line (0 , x * CELL_SIZE , SIZE * CELL_SIZE , x * CELL_SIZE )
@@ -24,28 +24,33 @@ def refresh_map():
2424 x , y = obstacle
2525 grid .create_rectangle (x * CELL_SIZE , y * CELL_SIZE , (x + 1 )* CELL_SIZE , (y + 1 )* CELL_SIZE , fill = 'black' )
2626
27+ if start :
28+ x1 , y1 = start
29+ grid .create_rectangle (x1 * CELL_SIZE , y1 * CELL_SIZE , (x1 + 1 )* CELL_SIZE , (y1 + 1 )* CELL_SIZE , fill = 'green' )
30+
31+ if end :
32+ x2 , y2 = end
33+ grid .create_rectangle (x2 * CELL_SIZE , y2 * CELL_SIZE , (x2 + 1 )* CELL_SIZE , (y2 + 1 )* CELL_SIZE , fill = 'red' )
34+
2735
2836def create_path ():
29- global CELL_SIZE , SIZE
37+ global CELL_SIZE , SIZE , start , end
3038 startButton .config (state = DISABLED )
3139 customButton .config (state = DISABLED )
3240 refresh_map ()
33- pos1 = (random .randint (0 , SIZE - 1 ), random .randint (0 , SIZE - 1 ))
34- while pos1 in OBSTACLES :
35- pos1 = (random .randint (0 , SIZE - 1 ), random .randint (0 , SIZE - 1 ))
36- pos2 = pos1
37- while pos2 == pos1 or pos2 in OBSTACLES :
38- pos2 = (random .randint (0 , SIZE - 1 ), random .randint (0 , SIZE - 1 ))
3941
40- print ("path between" , pos1 , pos2 )
41- x1 , y1 = pos1
42- x2 , y2 = pos2
42+ if not start and not end :
43+ get_random_positions ()
44+
45+ print ("path between" , start , end )
46+ x1 , y1 = start
47+ x2 , y2 = end
4348
4449 grid .create_rectangle (x1 * CELL_SIZE , y1 * CELL_SIZE , (x1 + 1 )* CELL_SIZE , (y1 + 1 )* CELL_SIZE , fill = 'green' )
4550 grid .create_rectangle (x2 * CELL_SIZE , y2 * CELL_SIZE , (x2 + 1 )* CELL_SIZE , (y2 + 1 )* CELL_SIZE , fill = 'red' )
4651 root .update ()
4752
48- path , debug = get_path (pos1 , pos2 , OBSTACLES , SIZE )
53+ path , debug = get_path (start , end , OBSTACLES , SIZE )
4954
5055 for cell in debug [1 :]:
5156 x , y = cell
@@ -66,33 +71,64 @@ def create_path():
6671 print (" {0} cells analyzed ({1}%)\n " .format (len (debug ), percentage ))
6772 startButton .config (state = NORMAL )
6873 customButton .config (state = NORMAL )
74+ start = None
75+ end = None
6976
7077
7178def custom (state ):
72- global OBSTACLES , customInProgress
79+ global OBSTACLES , current_state
80+
7381 if state == STATE_OBSTACLE :
74- OBSTACLES = []
82+ startButton .config (state = DISABLED )
83+ current_state = STATE_OBSTACLE
84+ customButton .config (text = "place start" , command = lambda :custom (STATE_START ))
7585 refresh_map ()
76- customInProgress = True
7786 print ("place obstacles" )
87+
7888 elif state == STATE_START :
79- pass
89+ current_state = STATE_START
90+ customButton .config (text = "place end" , command = lambda :custom (STATE_END ))
91+
8092 elif state == STATE_END :
81- pass
93+ current_state = STATE_END
94+ customButton .config (text = "place obstacles" , command = lambda :custom (STATE_OBSTACLE ))
8295
8396
8497def click (event ):
85- global OBSTACLES , customInProgress
86- if customInProgress :
87- x , y = event . x // CELL_SIZE , event . y // CELL_SIZE
98+ global OBSTACLES , current_state , start , end
99+ x , y = event . x // CELL_SIZE , event . y // CELL_SIZE
100+ if current_state == STATE_OBSTACLE :
88101 if (x , y ) in OBSTACLES :
89102 OBSTACLES .remove ((x , y ))
90103 else :
91104 OBSTACLES .append ((x , y ))
92105 refresh_map ()
106+
107+ elif current_state == STATE_START :
108+ if (x , y ) not in OBSTACLES :
109+ start = (x , y )
110+ refresh_map ()
111+
112+ elif current_state == STATE_END :
113+ if (x , y ) not in OBSTACLES and (x , y ) != start :
114+ end = (x , y )
115+ refresh_map ()
116+ startButton .config (state = NORMAL )
117+
118+
119+ def get_random_positions ():
120+ global start , end
121+ start = (random .randint (0 , SIZE - 1 ), random .randint (0 , SIZE - 1 ))
122+ while start in OBSTACLES :
123+ start = (random .randint (0 , SIZE - 1 ), random .randint (0 , SIZE - 1 ))
124+ end = start
125+ while end == start or end in OBSTACLES :
126+ end = (random .randint (0 , SIZE - 1 ), random .randint (0 , SIZE - 1 ))
93127
94128
95- customInProgress = False
129+ current_state = None
130+ start = None
131+ end = None
96132
97133root = Tk ()
98134grid = Canvas (root , width = SIZE * CELL_SIZE , height = SIZE * CELL_SIZE , bg = 'white' )
@@ -108,10 +144,11 @@ def click(event):
108144 OBSTACLES .append ((x , y ))
109145 grid .create_rectangle (x * CELL_SIZE , y * CELL_SIZE , (x + 1 )* CELL_SIZE , (y + 1 )* CELL_SIZE , fill = 'black' )
110146
147+
111148startButton = Button (root , text = 'start' , command = create_path )
112149startButton .pack ()
113150
114- customButton = Button (root , text = 'custom ' , command = lambda :custom (STATE_OBSTACLE ))
151+ customButton = Button (root , text = 'place obstacles ' , command = lambda :custom (STATE_OBSTACLE ))
115152customButton .pack ()
116153
117154root .mainloop ()
0 commit comments