-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
238 lines (190 loc) · 8.17 KB
/
main.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import pygame
import sys
import math
from win32api import GetSystemMetrics
pygame.init()
pygame.display.set_caption("A* Mańczak")
myfont = pygame.font.SysFont('Arial', 30)
pygame.mouse.set_visible(1)
window_size = window_width, window_height = int(
GetSystemMetrics(0)/2), int(GetSystemMetrics(0)/2)
screen = pygame.display.set_mode(window_size)
gridDimension = cols, rows = 50, 50
gridSize = gridSizeWidth, girdSizeHeight = int(window_width /
cols), int(window_height/rows)
startNode = x, y = 0, 0
endNode = x, y = cols-1, rows-1
# endNode = x, y = 0, 0
##########################################################################
class Node:
def __init__(self, colNum, rowNum):
# A* variables
self.colNum = colNum
self.rowNum = rowNum
self.isStartNode = False
self.isEndNode = False
self.f = 0
self.g = 0
self.h = 0
self.neighborNodes = []
self.parentNode = None
# Drawing variables
self.posX = colNum*gridSizeWidth
self.posY = rowNum*girdSizeHeight
self.inFillColour = (255, 255, 255)
self.outlineColour = (0)
self.rectOutline = pygame.Rect(self.posX, self.posY,
gridSizeWidth, girdSizeHeight)
self.rectInner = pygame.Rect(self.posX+1, self.posY+1,
gridSizeWidth-2, girdSizeHeight-2)
def select(self):
if not self.isStartNode and not self.isEndNode:
self.inFillColour = (0)
self.outlineColour = (0, 0, 0)
def unselect(self):
if not self.isStartNode and not self.isEndNode:
self.inFillColour = (255, 255, 255)
self.outlineColour = (0)
def addNeighbours(self, grid):
if self.inFillColour != (0):
# left border
if self.colNum > 0 and grid[self.colNum-1][self.rowNum].inFillColour != (0):
self.neighborNodes.append(grid[self.colNum-1][self.rowNum])
# right border
if self.colNum < gridDimension[0]-1 and grid[self.colNum+1][self.rowNum].inFillColour != (0):
self.neighborNodes.append(grid[self.colNum+1][self.rowNum])
# celing
if self.rowNum > 0 and grid[self.colNum][self.rowNum-1].inFillColour != (0):
self.neighborNodes.append(grid[self.colNum][self.rowNum-1])
# floor
if self.rowNum < gridDimension[1]-1 and grid[self.colNum][self.rowNum+1].inFillColour != (0):
self.neighborNodes.append(grid[self.colNum][self.rowNum+1])
"""# left uo
if self.colNum > 0 and self.rowNum > 0 and grid[self.colNum-1][self.rowNum-1].inFillColour != (0):
self.neighborNodes.append(grid[self.colNum-1][self.rowNum-1])
# right up
if self.colNum < gridDimension[0]-1 and self.rowNum > 0 and grid[self.colNum+1][self.rowNum-1].inFillColour != (0):
self.neighborNodes.append(grid[self.colNum+1][self.rowNum-1])
# left down
if self.colNum > 0 and self.rowNum < gridDimension[1]-1 and grid[self.colNum-1][self.rowNum+1].inFillColour != (0):
self.neighborNodes.append(grid[self.colNum-1][self.rowNum+1])
if self.colNum < gridDimension[0]-1 and self.rowNum < gridDimension[1]-1 and grid[self.colNum+1][self.rowNum+1].inFillColour != (0):
self.neighborNodes.append(grid[self.colNum+1][self.rowNum+1])"""
def draw(self):
pygame.draw.rect(screen, self.outlineColour, self.rectOutline)
pygame.draw.rect(screen, self.inFillColour, self.rectInner)
def heuristic(start, stop):
d = abs(start.colNum-stop[0])+abs(start.rowNum-stop[1])
#d = math.sqrt(abs(start.colNum-stop[0])**2+abs(start.rowNum-stop[1])**2)
return d
grid = [[Node(i, j)
for j in range(rows)]for i in range(cols)]
grid[startNode[0]][startNode[1]].isStartNode = True
grid[endNode[0]][endNode[1]].isEndNode = True
grid[startNode[0]][startNode[1]].inFillColour = (0, 255, 255)
grid[endNode[0]][endNode[1]].inFillColour = (255, 255, 0)
lmbHolding = False
drawingPhase = True
isLookinForNeigbours = True
openSet = []
closedSet = []
openSet.append(grid[startNode[0]][startNode[1]])
path = []
######################################################################
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and drawingPhase:
for i in range(len(grid)):
for j in range(len(grid[0])):
grid[i][j].unselect()
if event.key == pygame.K_q:
drawingPhase = False
if event.type == pygame.MOUSEBUTTONDOWN:
lmbHolding = True
if event.type == pygame.MOUSEBUTTONUP:
lmbHolding = False
if lmbHolding and drawingPhase:
mousePos = pygame.mouse.get_pos()
try:
grid[int(mousePos[0]/girdSizeHeight)
][int(mousePos[1]/gridSizeWidth)].select()
except:
pass
###############################################################
if not drawingPhase:
if isLookinForNeigbours:
for i in range(len(grid)):
for j in range(len(grid[0])):
grid[i][j].addNeighbours(grid)
isLookinForNeigbours = False
print(grid[2][2].neighborNodes)
if len(openSet) > 0: # There are still spots to evaleute
winnerIndex = 0
for i in range(len(openSet)):
if openSet[i].f < openSet[winnerIndex].f:
winnerIndex = i
current = openSet[winnerIndex]
if current == grid[endNode[0]][endNode[1]]: # Found the Solution
print("Found a Path!")
break
openSet.remove(current)
closedSet.append(current)
neighbors = current.neighborNodes
for i in range(len(neighbors)):
curNeighbor = neighbors[i]
if not curNeighbor in closedSet:
tempG = current.g + 1
if curNeighbor in openSet:
if tempG < curNeighbor.g:
curNeighbor.g = tempG
newPath = True
else:
curNeighbor.g = tempG
openSet.append(curNeighbor)
newPath = True
if newPath:
curNeighbor.h = heuristic(curNeighbor, endNode)
curNeighbor.f = curNeighbor.g + curNeighbor.h
curNeighbor.parentNode = current
path = []
temp = current
path.append(temp)
while temp.parentNode:
path.append(temp.parentNode)
temp = temp.parentNode
else: # No solution
print("No path!")
break
######################################################################
for i in range(len(closedSet)):
closedSet[i].inFillColour = (255, 0, 0)
for i in range(len(openSet)):
openSet[i].inFillColour = (0, 255, 0)
for i in range(len(path)):
path[i].inFillColour = (0, 0, 255)
for i in range(len(grid)):
for j in range(len(grid[0])):
grid[i][j].draw()
pygame.display.flip()
screen.fill((255, 255, 255))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
for i in range(len(closedSet)):
closedSet[i].inFillColour = (255, 0, 0)
for i in range(len(openSet)):
openSet[i].inFillColour = (0, 255, 0)
for i in range(len(path)):
path[i].inFillColour = (0, 0, 255)
grid[startNode[0]][startNode[1]].inFillColour = (0, 255, 255)
grid[endNode[0]][endNode[1]].inFillColour = (255, 255, 0)
for i in range(len(grid)):
for j in range(len(grid[0])):
grid[i][j].draw()
pygame.display.flip()
screen.fill((255, 255, 255))
# Mańczak