forked from nas-programmer/path-finding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
djikstra.py
153 lines (127 loc) · 4.44 KB
/
djikstra.py
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
"""Djikstra's Path Finding"""
import pygame, sys, random, math
from collections import deque
from tkinter import messagebox, Tk
size = (width, height) = 640, 480
pygame.init()
win = pygame.display.set_mode(size)
pygame.display.set_caption("Dijktdtra's Path Finding")
clock = pygame.time.Clock()
cols, rows = 64, 48
w = width//cols
h = height//rows
grid = []
queue, visited = deque(), []
path = []
class Spot:
def __init__(self, i, j):
self.x, self.y = i, j
self.f, self.g, self.h = 0, 0, 0
self.neighbors = []
self.prev = None
self.wall = False
self.visited = False
# if random.randint(0, 100) < 20:
# self.wall = True
def show(self, win, col, shape= 1):
if self.wall == True:
col = (0, 0, 0)
if shape == 1:
pygame.draw.rect(win, col, (self.x*w, self.y*h, w-1, h-1))
else:
pygame.draw.circle(win, col, (self.x*w+w//2, self.y*h+h//2), w//3)
def add_neighbors(self, grid):
if self.x < cols - 1:
self.neighbors.append(grid[self.x+1][self.y])
if self.x > 0:
self.neighbors.append(grid[self.x-1][self.y])
if self.y < rows - 1:
self.neighbors.append(grid[self.x][self.y+1])
if self.y > 0:
self.neighbors.append(grid[self.x][self.y-1])
def clickWall(pos, state):
i = pos[0] // w
j = pos[1] // h
grid[i][j].wall = state
def place(pos):
i = pos[0] // w
j = pos[1] // h
return w, h
for i in range(cols):
arr = []
for j in range(rows):
arr.append(Spot(i, j))
grid.append(arr)
for i in range(cols):
for j in range(rows):
grid[i][j].add_neighbors(grid)
start = grid[cols//2][rows//2]
end = grid[cols-50][rows - cols//2]
start.wall = False
end.wall = False
queue.append(start)
start.visited = True
def main():
flag = False
noflag = True
startflag = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONUP:
if pygame.mouse.get_pressed(0):
clickWall(pygame.mouse.get_pos(), True)
if pygame.mouse.get_pressed(2):
clickWall(pygame.mouse.get_pos(), False)
if event.type == pygame.MOUSEMOTION:
if pygame.mouse.get_pressed()[0]:
clickWall(pygame.mouse.get_pos(), True)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
startflag = True
if startflag:
if len(queue) > 0:
current = queue.popleft()
if current == end:
temp = current
while temp.prev:
path.append(temp.prev)
temp = temp.prev
if not flag:
flag = True
print("Done")
elif flag:
continue
if flag == False:
for i in current.neighbors:
if not i.visited and not i.wall:
i.visited = True
i.prev = current
queue.append(i)
else:
if noflag and not flag:
Tk().wm_withdraw()
messagebox.showinfo("No Solution", "There was no solution" )
noflag = False
else:
continue
win.fill((0, 20, 20))
for i in range(cols):
for j in range(rows):
spot = grid[i][j]
spot.show(win, (44, 62, 80))
if spot in path:
spot.show(win, (192, 57, 43))
elif spot.visited:
spot.show(win, (39, 174, 96))
if spot in queue:
spot.show(win, (44, 62, 80))
spot.show(win, (39, 174, 96), 0)
if spot == start:
spot.show(win, (0, 255, 200))
if spot == end:
spot.show(win, (0, 120, 255))
pygame.display.flip()
main()