-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmaze.py
More file actions
72 lines (63 loc) · 1.41 KB
/
maze.py
File metadata and controls
72 lines (63 loc) · 1.41 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
#!/usr/bin/python
# Maze generator in Python
# Joe Wingbermuehle
# 2010-10-06
import sys
import random
# The size of the maze (must be odd).
width = 39
height = 23
# The maze.
maze = dict()
# Display the maze.
def display_maze():
for y in range(0, height):
for x in range(0, width):
if maze[x][y] == 0:
sys.stdout.write(" ")
else:
sys.stdout.write("[]")
sys.stdout.write("\n")
# Initialize the maze.
def init_maze():
for x in range(0, width):
maze[x] = dict()
for y in range(0, height):
maze[x][y] = 1
# Carve the maze starting at x, y.
def carve_maze(x, y):
dir = random.randint(0, 3)
count = 0
while count < 4:
dx = 0
dy = 0
if dir == 0:
dx = 1
elif dir == 1:
dy = 1
elif dir == 2:
dx = -1
else:
dy = -1
x1 = x + dx
y1 = y + dy
x2 = x1 + dx
y2 = y1 + dy
if x2 > 0 and x2 < width and y2 > 0 and y2 < height:
if maze[x1][y1] == 1 and maze[x2][y2] == 1:
maze[x1][y1] = 0
maze[x2][y2] = 0
carve_maze(x2, y2)
count = count + 1
dir = (dir + 1) % 4
# Generate the maze.
def generate_maze():
random.seed()
maze[1][1] = 0
carve_maze(1, 1)
maze[1][0] = 0
maze[width - 2][height - 1] = 0
# Generate and display a random maze.
init_maze()
generate_maze()
display_maze()