-
Notifications
You must be signed in to change notification settings - Fork 0
/
mazemakerIMG.py
executable file
·151 lines (113 loc) · 3.24 KB
/
mazemakerIMG.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
#!/usr/bin/env python
# import turtle
import random
from PIL import Image, ImageDraw
# variable
me = 50**2
seed = "banana"
side = 100
board = range(1, me+1)
boardImage = Image.new("RGB", ((side*int(me**0.5))+(3*side), )*2)
boardDraw = ImageDraw.Draw(boardImage)
boardDraw.rectangle((0, 0, boardImage.size[0], boardImage.size[1]), (255, 255, 255))
# a quick stack implementation
class stack:
def __init__(self):
self.l = []
def size(self):
return len(self.l)
def push(self, e):
self.l.append(e)
def pop(self):
return self.l.pop(-1)
def exists(self, e):
return e in self.l
def top(self):
return self.l[-1]
# helper functions
transpose = lambda m: zip(*m)
order = lambda m: (len(m), len(m[0]))
toMatrix = lambda f: [f[i:i+int(me**0.5)] for i in range(len(f)) if i%int(me**0.5)==0]
def boardCoords(n):
"""converts the cell address to x,y coordinates"""
h = toMatrix(board)
c = board.index(n)/int(me**0.5)
ha = h[c].index(n)
va = transpose(h)[ha].index(n)
return (ha, va)
def nneighbors(n, ff=False):
"""computes the nearest neighbors, (up, down, left, right) only"""
coord = boardCoords(n)
h = toMatrix(board)
neighbors = []
hn = filter(lambda x: 0<=x<me**0.5, [coord[1]-1, coord[1]+1])
vn = filter(lambda x: 0<=x<me**0.5, [coord[0]-1, coord[0]+1])
neighbors.extend([h[coord[1]][i] for i in vn])
neighbors.extend([transpose(h)[coord[0]][i] for i in hn])
return neighbors
# plotting using PIL
def mkBox(coord, cons):
x, y = coord
s = int(side/2)
box = [
(x+s, y-s, x+s, y+s),
(x-s, y-s, x+s, y-s),
(x-s, y-s, x-s, y+s),
(x-s, y+s, x+s, y+s)
]
for i in zip(cons, box):
if i[0]:
boardDraw.line(i[1], fill=0)
def plotonImage():
"""generates the maze and plots it"""
# initializing variables
start, end = 1, me
visited = []
boxD = {1:[0, 1, 1, 0]}
directs = {-1:[0, 1, 1, 1],
1:[1, 1, 0, 1],
-me**0.5:[1, 1, 1, 0],
me**0.5:[1, 0, 1, 1],
0:[0, 1, 1, 0]}
s = stack()
s.push(start)
prev = start
ss = 1
# main loop
while len(s.l)>0:
c = s.top()
cr = boardCoords(c)
xc, yc = ((cr[0]*int(side)+side*2), (cr[1]*int(side)+side*2))
# generating constraints for the walls
nb = filter(lambda x: x not in visited, nneighbors(c))
pd = directs[c - prev]
nvnb = map(lambda y: c-y, filter(lambda x: x not in visited, nneighbors(c)))
pds = [directs[i] for i in nvnb]
boxD[c] = [all((pd[i], boxD[prev][i]))for i in range(4)] if boxD.has_key(c) else pd
for i in pds:
boxD[c] = [all((i[k], boxD[c][k]))for k in range(4)] if boxD.has_key(c) else i
if c not in visited:
mkBox((xc, yc), boxD[c])
# seeking next cell
if len(nb)>0:
nx = random.choice(nb)
s.push(nx)
print "[+] Moving to : ", nx
else:
# backtracking
q = s.pop()
print "[-] Backtracking from : ", q
# updating the end pos, based on size of stack
ss = s.size() if s.size()>=ss else ss
end = c if ss==s.size() else end
visited.append(c)
prev = c
# plotting the start and end
for points in [start, end]:
coord = boardCoords(points)
cw = lambda i, r: coord[i]*int(side)+(side*2)+r
boardDraw.ellipse((cw(0, -2), cw(1, -2), cw(0, 2), cw(1, 2)), fill=255)
if __name__ == "__main__":
plotonImage()
print "[*] DONE!"
boardImage.save("maze.png")