Maze generation: Difference between revisions

m
Delphi small fix
No edit summary
m (Delphi small fix)
Line 2,072:
Position := Point(Random(mwidth), Random(mheight));
Route := TStack<TPoint>.Create;
try
with Position do
while True do
begin
repeat
SetLength(Pool, 0);
if (y > 0) and not Maze[x, y-1].Visited then Pool := Pool + [Point(0, -1)];
if (x < mwidth-1) and not Maze[x+1, y].Visited then Pool := Pool + [Point(1, 0)];
if (y < mheight-1) and not Maze[x, y+1].Visited then Pool := Pool + [Point(0, 1)];
if (x > 0) and not Maze[x-1, y].Visited then Pool := Pool + [Point(-1, 0)];
 
if Length(Pool) = 0 then // no direction to draw from
with Position do
begin
while True do
if Route.Count = 0 then Exit; // and we are back at start so this is the end
begin
Position := Route.Pop;
repeat
SetLength(Pool, 0) end;
until Length(Pool) > 0;
if (y > 0) and not Maze[x, y-1].Visited then Pool := Pool + [Point(0, -1)];
if (x < mwidth-1) and not Maze[x+1, y].Visited then Pool := Pool + [Point(1, 0)];
if (y < mheight-1) and not Maze[x, y+1].Visited then Pool := Pool + [Point(0, 1)];
if (x > 0) and not Maze[x-1, y].Visited then Pool := Pool + [Point(-1, 0)];
 
if Length(Pool) = 0 then // no direction to draw from
begin
if Route.Count = 0 then Exit; // and we are back at start so this is the end
Position := Route.Pop;
end;
until Length(Pool) > 0;
 
d := Random(Length(Pool));
Offset(Pool[d]);
 
if Maze[x, y].Visited then Continue;
Maze[x, y].Visited := True;
 
if Pool[d].y = -1 then Maze[x, y+1].PassTop := True; // comes from down to up ( ^ )
if Pool[d].x = 1 then Maze[x, y].PassLeft := True; // comes from left to right ( --> )
if Pool[d].y = 1 then Maze[x, y].PassTop := True; // comes from left to right ( v )
if Pool[d].x = -1 then Maze[x+1, y].PassLeft := True; // comes from right to left ( <-- )
Route.Push(Position);
end;
finally
Route.Free;
end;
Route.Free;
end;
 
Line 2,112 ⟶ 2,114:
begin
for x := 0 to mwidth - 1 do
if Maze[x, y].PassTop then Write('+ ') else Write('+---');
Writeln('+');
for x := 0 to mwidth - 1 do
if Maze[x, y].PassLeft then Write(' ') else Write('| ');
Writeln('|');
end;
for x := 0 to mwidth - 1 do Write('+---');
Writeln('+');
end;
Line 2,129 ⟶ 2,131:
end.</lang>
{{out}}
<pre>+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| | | | | | | | | | |
+ + +--+--+---+ + + +---+---+---+---+---+ + + +---+---+---+ +---+ + +--+--+--+--+--+-- + + +--+--+ +---+---+ +
| | | | | | | | | | | | | | | | | |
+-- +---+ + +---+ ---+---+---+---+---+---+---+ ---+ ---+---+ + +---+---+---+ +---+---+ +---+---+ + + + + ---+---+ +---+---+
| | | | | | | | | | | | | | | | | |
+ +---+---+ + + +-- + +--+--+--+ +--+--+-- + + + +---+---+ +---+---+ + + + +---+---+---+ +---+ + + + +
| | | | | | | | | | | | | | | | | | | | | | |
+ ---+ +---+ + +---+ + +---+--+--+ +---+---+---+ + +---+ ---+ + +---+---+ +---+---+ +---+ +---+ +---+---+ +-- + + +
| | | | | | | | | | | | | | | | | | | | | |
+ +---+ +---+---+ +---+---+ + +--+--+ + + +-- +--+--+-- +--+--+--+--+ + +-- +-- +---+ + +--+--+ + + +---+---+ +
| | | | | | | | | | | | | | | | | | | | |
+ ---+---+ ---+ + +---+ ---+--+ -+---+---+ + +--+--+--+--+-- +--+--+ +-- +---+ +---+ +--+--+---+ +---+---+---+ + + + + +
| | | | | | | | | | | | | | | | | | | | |
+ +---+ +---+---+ + +---+---+ + + + +---+ +---+ + +--+--+ +--+--+ +--+--+---+ + + +---+ ---+---+ +--+--+--+ + -+
| | | | | | | | | | | | | | | | | | | | |
+ + + +---+---+ + + + +---+---+---+ + +---+ ---+---+ +---+--+--+ +-- + +---+---+---+ +---+ ---+ + +---+---+---+ +---+
| | | | | | | | | | | | | | | | | | | |
+---+ + +-- +--+--+ + + + +---+ +---+ +---+---+ +---+---+ + + +--+ -+---+ ---+---+---+ + +---+---+ + +--+--+ +
| | | | | | | | | | | | | | | | | | | |
+ + +---+ + + +---+---+ + +-- + +---+ + +---+---+ ---+---+---+---+ ---+ + + ---+---+---+ +---+---+ ---+ ---+ ---+ + ---+---+ +
| | | | | | | | | | | | | | | | | | | | |
+ +-- + +-- + + +-- + + +---+ +--+--+--+--+-- + +---+ +-- +-- +-- +---+ + + +---+---+---+ + +---+---+ +---+---+
| | | | | | | | | | | |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+</pre>
=={{header|EasyLang}}==
 
Anonymous user