Jump to content

Maze generation: Difference between revisions

no edit summary
(Added a Ruby implementation)
No edit summary
Line 1,842:
<a id='solve' style='display:none' href='javascript:solve(); void(0)'>Solve</a>
</fieldset></form><table id='maze'/></body></html></lang>
 
 
=={{header|MATLAB}}==
<lang Matlab>function M = makeMaze(n)
showProgress = false;
 
colormap([1,1,1;1,1,1;0,0,0]);
set(gcf,'color','w');
 
NoWALL = 0;
WALL = 2;
NotVISITED = -1;
VISITED = -2;
 
m = 2*n+3;
M = NotVISITED(ones(m));
offsets = [-1, m, 1, -m];
 
M([1 2:2:end end],:) = WALL;
M(:,[1 2:2:end end]) = WALL;
 
currentCell = sub2ind(size(M),3,3);
M(currentCell) = VISITED;
S = currentCell;
while (~isempty(S))
moves = currentCell + 2*offsets;
unvistedNeigbors = find(M(moves)==NotVISITED);
 
if (~isempty(unvistedNeigbors))
next = unvistedNeigbors(randi(length(unvistedNeigbors),1));
M(currentCell + offsets(next)) = NoWALL;
 
newCell = currentCell + 2*offsets(next);
if (any(M(newCell+2*offsets)==NotVISITED))
S = [S newCell];
end
currentCell = newCell;
M(currentCell) = VISITED;
else
currentCell = S(1);
S = S(2:end);
end
 
if (showProgress)
image(M-VISITED);
axis equal off;
drawnow;
pause(.01);
end
end
 
image(M-VISITED);
axis equal off;
</lang>
 
=={{header|Perl}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.