-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisual.py
210 lines (180 loc) · 7.8 KB
/
visual.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
import pygame
import numpy as np
import copy
import colorsys
def prims(rowcol):
# Generate random matrix to use its values as weights
matrix = np.random.randint(1, rowcol*rowcol, size=(rowcol, rowcol))
# Select the first and last node as the start and end
matrix[0][0] = 0
matrix[matrix.shape[0]-1][matrix.shape[1]-1] = matrix.shape[0]*matrix.shape[1]+100
# Implementation of prims algorithm
# Steps:
# 1. Select the start node and added to the visited list
# 2. Look for the smallest weight node that surrounds any of the visited nodes list, without it being part of the visited list
# 3. Repeat step 2 until all nodes are visited
visited = [(0, 0)]
paths = [[(0, 0)]]
while len(visited) != matrix.size:
min = 10000000
father = None
for x in visited:
# Check the surrounding nodes
# Check right
if x[0]+1 < matrix.shape[0]:
if (x[0]+1, x[1]) not in visited and matrix[x[0]+1][x[1]] < min:
pos = (x[0]+1, x[1])
min = matrix[x[0]+1][x[1]]
father = x
# Check left
if x[0]-1 >= 0:
if (x[0]-1, x[1]) not in visited and matrix[x[0]-1][x[1]] < min:
pos = (x[0]-1, x[1])
min = matrix[x[0]-1][x[1]]
father = x
# Check up
if x[1]+1 < matrix.shape[1]:
if (x[0], x[1]+1) not in visited and matrix[x[0]][x[1]+1] < min:
pos = (x[0], x[1]+1)
min = matrix[x[0]][x[1]+1]
father = x
# Check down
if x[1]-1 >= 0:
if (x[0], x[1]-1) not in visited and matrix[x[0]][x[1]-1] < min:
pos = (x[0], x[1]-1)
min = matrix[x[0]][x[1]-1]
father = x
visited.append(pos)
#Locate father and save new path
for x in paths:
if x[-1] == father:
paths.append(x + [pos])
break
finalpaths = copy.deepcopy(paths)
for i in range(len(finalpaths) - 1, -1, -1):
current_path = finalpaths[i]
# Check if the current path is a subset of any following paths
for j in range(i + 1, len(finalpaths)):
following_path = finalpaths[j]
if set(current_path).issubset(set(following_path)):
finalpaths.pop(i)
break
return matrix, finalpaths
def generate_distinct_colors(num_colors):
colors = []
golden_ratio_conjugate = (5 ** 0.5 - 1) / 2
for i in range(num_colors - 1):
hue = (i * golden_ratio_conjugate) % 1
r, g, b = colorsys.hsv_to_rgb(hue, 0.5, 0.95)
colors.append((int(r * 255), int(g * 255), int(b * 255)))
colors.append((0,255,0))
return colors
def viewMaze(matrix, paths):
# Colour all white
for j in range(matrix.shape[0]):
for i in range(matrix.shape[1]):
matrix[i][j] = 0
# Colour the start and end in green
matrix[0][0] = len(paths) + 1
matrix[matrix.shape[0]-1][matrix.shape[1]-1] = len(paths) + 1
def viewAnswer(matrix, paths):
# Colour each path
for j in range(matrix.shape[0]):
for i in range(matrix.shape[1]):
for x in paths:
if (i, j) in x:
matrix[i][j] = paths.index(x) + 1
# Colour the correct path in green
for x in paths:
if (matrix.shape[0]-1, matrix.shape[1]-1) in x:
for y in x:
matrix[y[0]][y[1]] = len(paths) + 1
def main():
# Initialize pygame
pygame.init()
# Define the screen dimensions
WIDTH = 400
HEIGHT = 400
# Define the matrix, you can edit the size of the matrix it will work fine till 30x30
rowcol = 15
matrix, paths = prims(rowcol)
#Comment one of the viewers, one shows you the answer and the other one shows you the maze
viewAnswer(matrix, paths)
# viewMaze(matrix, paths)
x = len(paths) + 1 # Number of colors
color_list = generate_distinct_colors(x)
color_mapping = {i + 1: color_list[i] for i in range(x)}
# Calculate the size of each cell based on the screen dimensions and matrix size
cell_size = min(WIDTH // len(matrix[0]), HEIGHT // len(matrix))
# Calculate the offset to center the matrix on the screen
x_offset = (WIDTH - len(matrix[0]) * cell_size) // 2
y_offset = (HEIGHT - len(matrix) * cell_size) // 2
# Create the screen surface
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# Game loop
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Clear the screen
screen.fill((0, 0, 0))
# Draw the matrix cells
for i, row in enumerate(matrix):
for j, value in enumerate(row):
# Calculate the cell position
x = j * cell_size + x_offset
y = i * cell_size + y_offset
# Get the color for the cell value
color = color_mapping.get(value, (255, 255, 255)) # Default to white if value not found in mapping
pygame.draw.rect(screen, color, (x, y, cell_size, cell_size))
# Draw the walls of each cell
for i, row in enumerate(matrix):
for j, value in enumerate(row):
# Calculate the cell position
x = j * cell_size + x_offset
y = i * cell_size + y_offset
futuredirections = ["up", "down", "left", "right"]
for z in paths:
if (j, i) in z:
index=z.index((j, i))+1
indexd=z.index((j, i))-1
if(index<len(z)):
xx,yy=z[index]
xx=xx-j
yy=yy-i
if yy==1:
futuredirections[3]="NULL"
elif yy==-1:
futuredirections[2]="NULL"
if xx==1:
futuredirections[1]="NULL"
elif xx==-1:
futuredirections[0]="NULL"
if(indexd>=0):
xx,yy=z[indexd]
xx=xx-j
yy=yy-i
if yy==1:
futuredirections[3]="NULL"
elif yy==-1:
futuredirections[2]="NULL"
if xx==1:
futuredirections[1]="NULL"
elif xx==-1:
futuredirections[0]="NULL"
# Draw the walls
if(futuredirections[0]!="NULL"):#up
pygame.draw.line(screen, (0, 0, 0), (y, x), (y + cell_size, x), 5)
if(futuredirections[1]!="NULL"): #down
pygame.draw.line(screen, (0, 0, 0), (y, x + cell_size), (y + cell_size, x + cell_size), 5)
if(futuredirections[2]!="NULL"): #left
pygame.draw.line(screen, (0, 0, 0), (y, x), (y, x + cell_size), 5)
if(futuredirections[3]!="NULL"): #right
pygame.draw.line(screen, (0, 0, 0), (y + cell_size, x), (y + cell_size, x + cell_size), 5)
# Update the display
pygame.display.flip()
# Quit the game
pygame.quit()
main()