-
Notifications
You must be signed in to change notification settings - Fork 4
/
maze.py
106 lines (90 loc) · 3.91 KB
/
maze.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
import random
from PIL import Image, ImageDraw
class Cell:
def __init__(self):
self.north = True
self.south = True
self.east = True
self.west = True
self.visited = False
class Maze:
def __init__(self, width=20, height=20, cell_width=20):
self.width = width
self.height = height
self.cell_width = cell_width
self.cells = [[Cell() for _ in range(height)] for _ in range(width)]
def generate(self):
x, y = random.choice(range(self.width)), random.choice(range(self.height))
self.cells[x][y].visited = True
path = [(x, y)]
while not all(all(c.visited for c in cell) for cell in self.cells):
x, y = path[len(path) - 1][0], path[len(path) - 1][1]
good_adj_cells = []
if self.exists(x, y - 1) and not self.cells[x][y - 1].visited:
good_adj_cells.append('north')
if self.exists(x, y + 1) and not self.cells[x][y + 1].visited:
good_adj_cells.append('south')
if self.exists(x + 1, y) and not self.cells[x + 1][y].visited:
good_adj_cells.append('east')
if self.exists(x - 1, y) and not self.cells[x - 1][y].visited:
good_adj_cells.append('west')
if good_adj_cells:
go = random.choice(good_adj_cells)
if go == 'north':
self.cells[x][y].north = False
self.cells[x][y - 1].south = False
self.cells[x][y - 1].visited = True
path.append((x, y - 1))
if go == 'south':
self.cells[x][y].south = False
self.cells[x][y + 1].north = False
self.cells[x][y + 1].visited = True
path.append((x, y + 1))
if go == 'east':
self.cells[x][y].east = False
self.cells[x + 1][y].west = False
self.cells[x + 1][y].visited = True
path.append((x + 1, y))
if go == 'west':
self.cells[x][y].west = False
self.cells[x - 1][y].east = False
self.cells[x - 1][y].visited = True
path.append((x - 1, y))
else:
path.pop()
def exists(self, x, y):
if x < 0 or x > self.width - 1 or y < 0 or y > self.height - 1:
return False
return True
def get_direction(self, direction, x, y):
if direction == 'north':
return x, y - 1
if direction == 'south':
return x, y + 1
if direction == 'east':
return x + 1, y
if direction == 'west':
return x - 1, y
def draw(self):
canvas_width, canvas_height = self.cell_width * self.width, self.cell_width * self.height
im = Image.new('RGB', (canvas_width, canvas_height))
draw = ImageDraw.Draw(im)
for x in range(self.width):
for y in range(self.height):
if self.cells[x][y].north:
draw.line(
(x * self.cell_width, y * self.cell_width, (x + 1) * self.cell_width, y * self.cell_width))
if self.cells[x][y].south:
draw.line((x * self.cell_width, (y + 1) * self.cell_width, (x + 1) * self.cell_width,
(y + 1) * self.cell_width))
if self.cells[x][y].east:
draw.line(((x + 1) * self.cell_width, y * self.cell_width, (x + 1) * self.cell_width,
(y + 1) * self.cell_width))
if self.cells[x][y].west:
draw.line(
(x * self.cell_width, y * self.cell_width, x * self.cell_width, (y + 1) * self.cell_width))
im.show()
if __name__ == '__main__':
maze = Maze()
maze.generate()
maze.draw()