Anonymous user
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;
const
mwidth =
mheight =
procedure ClearVisited(var AMaze: TMaze);
var
x, y: Integer;
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
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
if (x < mwidth-1) and not
if (y < mheight-1) and not
if (x > 0) and not
if Length(Pool) = 0 then // no direction to draw from
Line 2,093 ⟶ 2,103:
Offset(Pool[d]);
if Pool[d].y = -1 then
if Pool[d].x = 1 then
if Pool[d].y = 1 then
if Pool[d].x = -1 then
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
Result := Result + '+' + sLineBreak;
for x := 0 to mwidth - 1 do
begin
if S = Point(x, y) then v := 'S' else
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
Result := Result + '+' + sLineBreak;
end;
var
Maze: TMaze;
begin
Randomize;
PrepareMaze(Maze);
ClearVisited(Maze); // show no route
Write(MazeToString(Maze, Point(-1, -1), Point(-1, -1)));
ReadLn;
end;
begin
Main;
end.</lang>
{{out}}
|