-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaze.py
More file actions
83 lines (64 loc) · 2.61 KB
/
maze.py
File metadata and controls
83 lines (64 loc) · 2.61 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
from PIL import Image
from matplotlib import pyplot as plt
import numpy as np
import configparser
# Import config
configParser = configparser.RawConfigParser()
configFilePath = r'config.txt'
configParser.read(configFilePath)
gradient_range = int(configParser.get('your-config', 'gradient_range'))
start_y = int(configParser.get('your-config', 'start_y'))
start_x = int(configParser.get('your-config', 'start_x'))
mode = configParser.get('your-config', 'mode')
# Open image
img = Image.open('maze.png').convert('L')
# Image converts to binary matrix
binary_matrix = np.array(img)
binary_matrix = ~binary_matrix # invert B&W
binary_matrix [binary_matrix > 0] = 1
if (mode == "binary"):
binary_matrix = ~binary_matrix # invert B&W again for colour consistency
plt.imshow(binary_matrix, interpolation='nearest')
binary_matrix = ~binary_matrix
# Create output matrix
matrix_height = len(binary_matrix)
matrix_width = len(binary_matrix[0])
output_matrix = np.zeros([matrix_height, matrix_width], dtype=int)
output_matrix = ~output_matrix
start_tile = (start_y, start_x)
increment_move = (-1, -1)
# Set the moves to a high number based on the image size to ensure that the walls (at 0) are a much lower number than the first step of the path
current_moves = (matrix_height * matrix_width) / gradient_range
tiles_todo = [start_tile, increment_move]
check_for_end = False
# Breadth-first traversal of the maze, main loop
for tile in tiles_todo:
# Only increment the moves once all tiles of a certain depth have been checked. All tiles of a depth level are added to tiles_todo between increment_move items.
if (tile[0] == -1 and tile[1] == -1):
if (check_for_end):
break # If two increment_move tiles in a row, there are no more tiles left and the traversal is over
current_moves += 1
tiles_todo.append(increment_move)
check_for_end = True
continue
else:
check_for_end = False
right_tile = (tile[0], tile[1] + 1)
left_tile = (tile[0], tile[1] - 1)
down_tile = (tile[0] + 1, tile[1])
up_tile = (tile[0] - 1, tile[1])
possible_moves = [up_tile, down_tile, right_tile, left_tile]
for move in possible_moves:
y = move[0]
x = move[1]
if (y <= (matrix_height - 1) and y >= 0 and x <= (matrix_width - 1) and x >= 0):
if (binary_matrix[y][x] == 0): # If not a wall
if ((y, x) not in tiles_todo):
tiles_todo.append((y, x))
output_matrix[y][x] = current_moves
# Set the start point to the maximum moves colour for visibility
output_matrix[start_y][start_x] = current_moves
if (mode == "gradient"):
plt.imshow(output_matrix, interpolation='nearest')
plt.savefig('output.png', dpi=250, bbox_inches='tight')
plt.show()