Maze generation: Difference between revisions

→‎{{header|Python}}: correct solution, also python 2/3 compatible
(→‎{{header|C}}: cleanup)
(→‎{{header|Python}}: correct solution, also python 2/3 compatible)
Line 1,358:
 
=={{header|Python}}==
<lang python>from random import choice
{{incorrect|Python|Generated maze may contain sealed off sections, whereas a correct implementation shouldn't have that.}}
<lang Python>from random import randrange, choice
 
def make_maze(height, width):
conn = [ [[] for j in range(width)] for i in range(height) ]
def get_candidate_neighbors(cells, i):
visited = [ [ False for j in range(width) ] for i in range(height) ]
neighbors = []
 
def neighbors(x, y):
n = i - width
return [ (i, j) for (i, j) in ((y+1, x), (y-1, x), (y, x+1), (y, x-1))
s = i + width
if j >= 0 and j < width eand i >= 0 and i +< 1height
if not visited[i][j] ]
w = i - 1
 
def walk(y, x, avail=width*height):
if n >= 0 and not cells[n]:
visited[y][x] = True
neighbors.append(n)
avail -= 1
if (s < height * width) and (not cells[s]):
neighbors.append(s)
if (e // width == i // width) and (not cells[e]):
neighbors.append(e)
if (w // width == i // width) and (not cells[w]):
neighbors.append(w)
 
while avail:
return neighbors
n = neighbors(x, y)
if not len(n): return avail
 
(i,j) = choice(n)
# cells data structure is simply a list of lists. Each cell
conn[y][x].append((i,j))
# has a list of the neighbors where there is a connection
conn[i][j].append((y,x))
# so an empty list means that a cell has all walls remaining.
total_cells = height * width
cells = [[] for i in xrange(total_cells)]
cur_cell = randrange(total_cells)
visited = 1
cellstack = []
 
avail = walk(i, j, avail)
while visited < total_cells:
neighbors = get_candidate_neighbors(cells, cur_cell)
if len(neighbors):
new = choice(neighbors)
 
walk(0, 0)
# breakdown the wall
return conn
cells[cur_cell].append(new)
cells[new].append(cur_cell)
cellstack.append(cur_cell)
cur_cell = new
visited += 1
else:
cur_cell = cellstack.pop()
 
def return show_maze(cells):
w, h = len(cells[0]), len(cells)
for row in range(2 * h + 1):
i, s = row//2, ""
for j in range(w):
if row == 2*h:
s = "+--" * w
elif row & 1:
s += " " if (i, j-1) in cells[i][j] else "| "
else:
s += "+ " if (i-1, j) in cells[i][j] else "+--"
s += "|" if row & 1 else "+"
print(s)
 
show_maze(make_maze(8, 11))</lang>output<lang>+--+--+--+--+--+--+--+--+--+--+--+
 
| | | | |
def show_maze(cells, height, width):
+--+ + s1,+--+ s2 =+ "", ""+ +--+ +--+ +
| | | | | | | |
for i in xrange(height * width):
+ + + +--+--+ if+--+ i %+--+ width ==+ 0: +
| | | | print s1 | | | |
+ +--+ + + +--+ +--+--+ print+ s2 +
| | | s1 = "+" | | | |
+--+ +--+ +--+ +--+ + s2 = "|"+--+--+
| | | s1 +=| " +" if (i - width) in| cells[i] else| "--+" |
+ + + +--+ +--+--+ +--+--+ +
s2 += " " if (i + 1) in cells[i] else " |"
| | | | | | |
print "+" + "--+" * width
+ + +--+ +--+--+--+--+ + + +
 
| | | | | | |
 
+ +--+--+--+ +--+--+ + + + +
height, width = 8, 11
| | | |
show_maze(make_maze(height, width), height, width)</lang>
+--+--+--+--+--+--+--+--+--+--+--+</lang>
Sample Output:
<pre>
+--+--+--+--+--+--+--+--+--+--+--+
| | | |
+ +--+--+ +--+ + +--+ + + +
| | | | | | | |
+--+--+ + + +--+ + +--+--+ +
| | | | | |
+ + +--+ +--+--+--+ +--+--+--+
| | | | | |
+ + + +--+ + +--+--+--+--+ +
| | | | | |
+ +--+--+--+--+--+--+ + + +--+
| | | | |
+--+--+ +--+ + +--+--+--+--+ +
| | | | | | |
+--+--+--+--+--+--+--+--+--+--+--+
</pre>
 
=={{header|Tcl}}==
Anonymous user