Repository navigation
Expand file tree
/
Copy pathmaze_shortest_path.py
More file actions
175 lines (115 loc) · 4.88 KB
/
Copy pathmaze_shortest_path.py
File metadata and controls
175 lines (115 loc) · 4.88 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from algorithms.data_struct.queue import queue
from graph.Maze import labirinth as maze
import pygame,time
from graph.color import Green,Cyan,Blue,Lime,Yellow
pygame.init()
screen_height = 700
screen_width = 1000
screen = pygame.display.set_mode((screen_width,screen_height))
screen.fill((0,0,0))
ROWS = len(maze)
COLS = len(maze[0])
predecessor = list( list( ( -1 , -1 ) for _ in range(COLS) ) for _ in range(ROWS) )
White = (255,255,255)
Black = ( 0 , 0 , 0 )
Q = queue()
def remove_queue(i,j):
maze[i][j] = Cyan
pygame.draw.rect(screen , Cyan , ( 50 + 5*i , 50 + 5*j , 5 , 5 ) )
def insert_queue(i,j):
if i < 0 or j < 0 or i >99 or j > 99: return
if maze[i][j] != Green:
maze[i][j] = Blue
pygame.draw.rect(screen , Blue , ( 50 + 5*i , 50 + 5*j , 5 , 5 ) )
def display_maze():
N = len(maze)
print(N)
for x in range(N):
time.sleep(0.01)
for y in range(N):
pygame.draw.rect(screen , maze[x][y] , ( 50 + 5*x , 50 + 5*y , 5 , 5 ) )
pygame.display.update()
#maze[0][1] = maze[0][2] = maze[0][3] = maze[0][4] = Yellow
maze[99][95] = maze[99][96] = maze[99][97] = maze[99][98] = maze[99][99] = Green
time.sleep(1)
font = pygame.font.Font('freesansbold.ttf',20)
text = font.render("Entry (Entrada).",True, Cyan)
screen.blit(text,text.get_rect(center = (100,30)))
font = pygame.font.Font('freesansbold.ttf',20)
text = font.render("Exit (Saida)" ,True, Green)
screen.blit(text,text.get_rect(center = (560,570)))
font = pygame.font.Font('freesansbold.ttf',40)
text = font.render("in queue",True, Blue)
screen.blit(text,text.get_rect(center = (800,250)))
font = pygame.font.Font('freesansbold.ttf',40)
text = font.render("seen",True, Cyan)
screen.blit(text,text.get_rect(center = (800,350)))
pygame.display.update()
display_maze()
N_layer = 1
missing = 0
found = False
source = (0,0)
current = source
Q.insert(source)
see_neighbours = False
delay = 0.025
pause = True
running = True
while running :
# pygame stuff:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit() #exit pygame,
running = False #exit() program
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE: #press breakspace to pause or play
pause = not pause
time.sleep(0.2)
if pause:
continue
if not see_neighbours and not found:
if missing == 0:
missing = N_layer
N_layer = 0
pygame.display.update()
time.sleep(delay)
if Q.not_empty():
missing -= 1
current = Q.pop()
else:
pause = True
continue
i,j = current
if maze[i][j] == Green:
font = pygame.font.Font('freesansbold.ttf',40)
text = font.render("Found",True,(0,235,0)) # informative node
screen.blit(text,text.get_rect(center = (860,50)))
pygame.display.update()
found = True
continue
remove_queue(i,j)
see_neighbours = True
if see_neighbours and not found:
i,j = current
if( (i+1 < ROWS) and (maze[i+1][ j ] == Black or maze[i+1][ j ] == Green) ): insert_queue(i+1 , j ); Q.insert((i+1 , j )); N_layer += 1 ; predecessor[i+1][ j ] = ( i , j )
if( (0 < i ) and (maze[i-1][ j ] == Black or maze[i-1][ j ] == Green) ): insert_queue(i-1 , j ); Q.insert((i-1 , j )); N_layer += 1 ; predecessor[i-1][ j ] = ( i , j )
if( (j+1 < COLS) and (maze[ i ][j+1] == Black or maze[ i ][j+1] == Green) ): insert_queue( i , j+1); Q.insert(( i , j+1)); N_layer += 1 ; predecessor[ i ][j+1] = ( i , j )
if( (j > 0 ) and (maze[ i ][j-1] == Black or maze[ i ][j-1] == Green) ): insert_queue( i , j-1); Q.insert(( i , j-1)); N_layer += 1 ; predecessor[ i ][j-1] = ( i , j )
see_neighbours = False
time.sleep(0.001)
if found:
time.sleep(0.025)
(x,y) = current
if( current != source ):
pygame.draw.rect(screen , Green , ( 50 + 5*x , 50 + 5*y , 5 , 5 ) )
x,y = predecessor[x][y]
pygame.draw.rect(screen , Lime , ( 50 + 5*x , 50 + 5*y , 5 , 5 ) )
pygame.display.update()
current = (x,y)
if current == source:
pygame.draw.rect(screen , Green , ( 50 + 5*x , 50 + 5*y , 5 , 5 ) )
pygame.display.update()
pause = True
found = False
continue