Maze generation: Difference between revisions

Changed to handle solutions from other page
(Updated to work with Nim 1.4: added missing parameter type, import "random" instead of "math", changed "random" to "rand", changed ".. <" to "..<".)
(Changed to handle solutions from other page)
Line 2,047:
{$APPTYPE CONSOLE}
 
uses System.SysUtils, System.Types, System.Generics.Collections, System.IOUtils;
 
type
Line 2,055:
PassLeft : Boolean;
end;
TMaze = array of array of TMCell;
RouteTRoute := TStack<TPoint>;
 
const
mwidth = 3224;
mheight = 1214;
 
procedure ClearVisited(var AMaze: TMaze);
var
x, y: Integer;
Maze : array[0..mwidth - 1, 0..mheight - 1] of TMCell;
begin
for y := 0 to mheight - 1 do
for x := 0 to mwidth - 1 do
AMaze[x, y].Visited := False;
end;
 
procedure PrepareMaze(var AMaze: TMaze);
var
PositionRoute : TPointTRoute;
d Position : IntegerTPoint;
d : Integer;
Pool : array of TPoint; // Pool of directions to pick randomly from
Route : TStack<TPoint>;
begin
SetLength(AMaze, mwidth, mheight);
ClearVisited(AMaze);
Position := Point(Random(mwidth), Random(mheight));
Route := TStack<TPoint>.Create;
Line 2,078 ⟶ 2,088:
repeat
SetLength(Pool, 0);
if (y > 0) and not MazeAMaze[x, y-1].Visited then Pool := Pool + [Point(0, -1)];
if (x < mwidth-1) and not MazeAMaze[x+1, y].Visited then Pool := Pool + [Point(1, 0)];
if (y < mheight-1) and not MazeAMaze[x, y+1].Visited then Pool := Pool + [Point(0, 1)];
if (x > 0) and not MazeAMaze[x-1, y].Visited then Pool := Pool + [Point(-1, 0)];
 
if Length(Pool) = 0 then // no direction to draw from
Line 2,093 ⟶ 2,103:
Offset(Pool[d]);
 
MazeAMaze[x, y].Visited := True;
if Pool[d].y = -1 then MazeAMaze[x, y+1].PassTop := True; // comes from down to up ( ^ )
if Pool[d].x = 1 then MazeAMaze[x, y].PassLeft := True; // comes from left to right ( --> )
if Pool[d].y = 1 then MazeAMaze[x, y].PassTop := True; // comes from left to right ( v )
if Pool[d].x = -1 then MazeAMaze[x+1, y].PassLeft := True; // comes from right to left ( <-- )
Route.Push(Position);
end;
Line 2,105 ⟶ 2,115:
end;
 
function MazeToString(const AMaze: TMaze; const S, E: TPoint): String; overload;
procedure ShowMaze;
var
x, y: Integer;
v : Char;
begin
Result := '';
for y := 0 to mheight - 1 do
begin
for x := 0 to mwidth - 1 do
if MazeAMaze[x, y].PassTop then Write('+Result := Result + ')+'#32#32#32 else Write(Result := Result + '+---');
Result := Result + '+' + sLineBreak;
Writeln('+');
for x := 0 to mwidth - 1 do
begin
if Maze[x, y].PassLeft then Write(' ') else Write('| ');
if S = Point(x, y) then v := 'S' else
Writeln('|');
if E = Point(x, y) then v := 'E' else
v := #32'*'[Ord(AMaze[x, y].Visited) + 1];
 
Result := Result + '|'#32[Ord(AMaze[x, y].PassLeft) + 1] + #32 + v + #32;
end;
Result := Result + '|' + sLineBreak;
end;
for x := 0 to mwidth - 1 do Write(Result := Result + '+---');
Result := Result + '+' + sLineBreak;
Writeln('+');
end;
 
procedure ShowMazeMain;
var
Maze: TMaze;
begin
Randomize;
PrepareMaze(Maze);
ClearVisited(Maze); // show no route
ShowMaze;
Write(MazeToString(Maze, Point(-1, -1), Point(-1, -1)));
Readln;
ReadLn;
end;
 
begin
Main;
 
end.</lang>
{{out}}
Anonymous user