-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMaze.py
94 lines (78 loc) · 2.68 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
from random import randint
import numpy as np
def creat_map(start_node,end_node,mazeX,mazeY,sparsity):
# Set the size of the maze.
# These must be odd integers of 7 or above.
xSize = mazeX
ySize = mazeY
# Set the maximum wall length. This avoids overly long
# walls on larger mazes.
# The smaller the value, the greater possible solutions.
maxWall = 13
# Create a list to store the maze data.
# 0 denotes a walkway, 1 is a wall, start with no walls.
maze = (xSize*ySize)*[0]
# Set every other cell to 2, these are the starting points.
for i in range(0,xSize,sparsity):
for j in range(0,ySize,sparsity):
maze[(j*xSize)+i]=2
# Create a wall around the maze.
# Top and bottom.
for i in range(0,xSize):
maze[i]=1
maze[(xSize*(ySize-1))+i] = 1
# Left and right.
for i in range(1,ySize-1):
maze[xSize*i]=1
maze[(xSize*i)+(xSize-1)] = 1
# Create the maze.
while 2 in maze:
# Pick a random starting point.
x = randint(1,(xSize-1)/2)*2
y = randint(1,(ySize-1)/2)*2
c = maze[(y*xSize)+x]
# If the point hasn't already been used pick a random direction
# and move that way until we hit a wall.
if c == 2:
mwc = 0
r = randint(0,3)
if r == 0:
while c != 1:
maze[(y*xSize)+x] = 1
y -= 1
c = maze[(y*xSize)+x]
mwc += 1
if mwc == maxWall: break
if r == 1:
while c != 1:
maze[(y*xSize)+x] = 1
x += 1
c = maze[(y*xSize)+x]
mwc += 1
if mwc == maxWall: break
if r == 2:
while c != 1:
maze[(y*xSize)+x] = 1
y += 1
c = maze[(y*xSize)+x]
mwc += 1
if mwc == maxWall: break
if r == 3:
while c!= 1:
maze[(y*xSize)+x] = 1
x -= 1
c = maze[(y*xSize)+x]
mwc += 1
if mwc == maxWall: break
maze_map={}
arr=np.ones((xSize,ySize))
for i in range(0,ySize):
for j in range(0,xSize):
k = maze[(i*xSize)+j]
if k == 1:
if (i==start_node[0] and j==start_node[1]) or (j==end_node[1]and i==end_node[0]):
continue
else:
maze_map[(i,j)]=1
arr[j][i]=0
return maze_map,arr