Maze generation: Difference between revisions

Content added Content deleted
(Undo revision 305527 by Spie812 (talk) undid massive changes to the whole Rosetta Code task entries.)
(→‎{{header|Ada}}: Simplified way of detecting unvisited neighbours.)
Line 65: Line 65:
Row : Height_Type;
Row : Height_Type;
Column : Width_Type);
Column : Width_Type);

function Has_Unvisited_Neighbours
(Maze : Maze_Grid;
Row : Height_Type;
Column : Width_Type)
return Boolean;


procedure Move
procedure Move
Line 101: Line 95:
Next_Direction : Directions;
Next_Direction : Directions;
Valid_Direction : Boolean;
Valid_Direction : Boolean;
Tested_Wall : array (Directions) of Boolean := (others => False);
All_Tested : Boolean;
begin
begin
-- mark as visited
-- mark as visited
Maze (Row, Column).Visited := True;
Maze (Row, Column).Visited := True;
-- continue as long as there are unvisited neighbours left
-- continue as long as there are unvisited neighbours left
loop
while Has_Unvisited_Neighbours (Maze, Row, Column) loop
All_Tested := True;
for D in Directions loop
All_Tested := All_Tested and Tested_Wall (D);
end loop;
-- all directions are either visited from here,
-- or previously visited, or invalid.
exit when All_Tested;
-- use random direction
-- use random direction
Next_Direction := Random_Direction.Random (Dir_Generator);
Next_Direction := Random_Direction.Random (Dir_Generator);
Line 121: Line 124:
end if;
end if;
end if;
end if;
Tested_Wall (Next_Direction) := True;
end loop;
end loop;
end Depth_First_Algorithm;
end Depth_First_Algorithm;

function Has_Unvisited_Neighbours
(Maze : Maze_Grid;
Row : Height_Type;
Column : Width_Type)
return Boolean
is
Neighbour_Row : Height_Type;
Neighbour_Column : Width_Type;
Is_Valid : Boolean;
begin
for Dir in Directions loop
Neighbour_Row := Row;
Neighbour_Column := Column;
Move
(Row => Neighbour_Row,
Column => Neighbour_Column,
Direction => Dir,
Valid_Move => Is_Valid);
if Is_Valid
and then not Maze (Neighbour_Row, Neighbour_Column).Visited
then
return True;
end if;
end loop;
return False;
end Has_Unvisited_Neighbours;


procedure Initialize (Maze : in out Maze_Grid) is
procedure Initialize (Maze : in out Maze_Grid) is