FrostFyr
2010-05-30 09:18:49
I've made a script to create a random maze using DepthFirstSearch
but it will not create them properly
any assistance will be appreciated
This post was edited after the next 2 posts were posted btw
if you need a place to put the code the script outputs look at the 3rd post
but it will not create them properly
any assistance will be appreciated
This post was edited after the next 2 posts were posted btw
if you need a place to put the code the script outputs look at the 3rd post
program New;
const
W = 15; //Max Width of Maze
H = 15; //Max Height of Maze
type
Cells = record
x, y : Integer;
Start, Finish, Visited, WallN, WallE, WallS, WallW : Boolean;
end;
var
Cell : array of array of Cells;
Stack : TPointArray;
r : Integer;
procedure CreateMazeBase;
Var i,j:Integer;
begin
SetArrayLength(Cell,w);
for i:=0 to W-1 do
begin
SetArrayLength(Cell[i],H);
for j:=0 to H-1 do
begin
with Cell[i][j] do
begin
x:=i;
y:=j;
Start:=False;
Finish:=False;
Visited:=False;
WallN:=True;
WallE:=True;
WallS:=True;
WallW:=True;
end;
end;
end;
Cell[0][0].Start:=True;
Cell[0][0].WallN:=False;
Cell[W-1][H-1].Finish:=True;
Cell[W-1][H-1].WallS:=False;
end;
Procedure RecurseMaze;
var q, x, y, z:Integer; N:TPointArray;
begin
x:=Random(W);
y:=Random(H);
SetArrayLength(Stack,r+2);
r:=r+1;
Stack[r].x:=x;
Stack[r].y:=y;
Cell[x][y].Visited:=True;
SetArrayLength(N,4);
repeat
z:=0;
//Check Stack x,y to neighbours with Unvisited staus and load those into N
//Dbug
{Writeln(x);
Writeln(y);}
//Dbug
if(not(x=0))then
if(Cell[x-1][y].Visited=False)then
begin
N[z].x:=x-1;
N[z].y:=y;
z:=z+1;
end;
if(not(x=w-1))then
if(Cell[x+1][y].Visited=False)then
begin
N[z].x:=x+1;
N[z].y:=y;
z:=z+1;
end;
if(not(y=0))then
if(Cell[x][y-1].Visited=False)then
begin
N[z].x:=x;
N[z].y:=y-1;
z:=z+1;
end;
if(not(y=H-1))then
if(Cell[x][y+1].Visited=False)then
begin
N[z].x:=x;
N[z].y:=y+1;
z:=z+1;
end;
//IF NOT unvisited, End/Start Square
if(not((Cell[x][y].Finish)or(Cell[x][y].Start)or(z=0)))then
begin
//Random(Unvisited)
q:=Random(z-1);
x:=n[q].x;
y:=n[q].y;
Cell[x][y].Visited:=True;
//Knock walls between the two
if(x=Stack[r].x)then
begin
if(y<Stack[r].y)then
begin
Cell[x][y].WallS:=False;
Cell[Stack[r].x][Stack[r].y].WallN:=False;
end;
if(y>Stack[r].y)then
begin
Cell[x][y].WallN:=False;
Cell[Stack[r].x][Stack[r].y].WallS:=False;
end;
end else
if(y=Stack[r].y)then
begin
if(x<Stack[r].x)then
begin
Cell[x][y].WallE:=False;
Cell[Stack[r].x][Stack[r].y].WallW:=False;
end;
if(x>Stack[r].x)then
begin
Cell[x][y].WallW:=False;
Cell[Stack[r].x][Stack[r].y].WallE:=False;
end;
end;
//AddStack(x,y);
SetArrayLength(Stack,r+2);
r:=r+1;
Stack[r].x:=x;
Stack[r].y:=y;
end else
begin
SetArrayLength(Stack,r);
r:=r-1;
if(r>-1)then
begin
x:=Stack[r].x;
y:=Stack[r].y;
end;
end;
until(r=-1)
end;
procedure PrintMaze(Bool:Boolean);
var a,b:integer; Str,Temp,Hex:String;
begin
for a:=0 to W-1 do
for b:=0 to H-1 do
begin
if(Cell[a][b].Visited=False)then Writeln('Cell['+IntToStr(a)+']['+IntToStr(b)+'] Is unvisited.');
if(Cell[a][b].WallW=True)then
begin
Str:=Str+'1';
end else
begin
Str:=Str+'0';
end;
if(Cell[a][b].WallS=True)then
begin
Str:=Str+'1';
end else
begin
Str:=Str+'0';
end;
if(Cell[a][b].WallE=True)then
begin
Str:=Str+'1';
end else
begin
Str:=Str+'0';
end;
if(Cell[a][b].WallN=True)then
begin
Str:=Str+'1';
end else
begin
Str:=Str+'0';
end;
end;
Writeln(Str);
if(Bool)then
begin
Temp:=Str;
Str:='';
Repeat
a:=0;
Hex:=Copy(Temp,1,4);
Delete(Temp,1,4);
If(Copy(Hex,1,1)='1')then a:=a+8;
If(Copy(Hex,2,1)='1')then a:=a+4;
If(Copy(Hex,3,1)='1')then a:=a+2;
If(Copy(Hex,4,1)='1')then a:=a+1;
Hex:=IntToStr(a);
case Hex of
'10': Hex:='A';
'11': Hex:='B';
'12': Hex:='C';
'13': Hex:='D';
'14': Hex:='E';
'15': Hex:='F';
end;
Str:=Str+Hex;
until(Length(Temp)<4)
end;
Writeln(Str);
end;
begin
r:=-1;
CreateMazeBase;
RecurseMaze;
PrintMaze(True);
end.