-
Notifications
You must be signed in to change notification settings - Fork 20
/
maze.pas
102 lines (87 loc) · 1.96 KB
/
maze.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
(* Maze generator in Pascal.
* Joe Wingbermuehle
* 2010-09-29
*)
program maze;
const
(* Size of the maze, must be odd. *)
width = 39;
height = 23;
type
CellType = (Space, Wall);
MazeType = array [0 .. width - 1, 0 .. height - 1] of CellType;
var
maze : MazeType;
(* Initialize the maze matrix. *)
procedure init_maze;
var
x, y : integer;
begin
for y := 0 to height - 1 do
for x := 0 to width - 1 do
maze[x, y] := Wall;
end;
(* Carve the maze starting at x, y. *)
procedure carve_maze(x, y : integer);
var
x1, y1 : integer;
x2, y2 : integer;
dx, dy : integer;
dir, cnt : integer;
begin
dir := random(4);
cnt := 0;
while cnt < 4 do
begin
dx := 0;
dy := 0;
case dir of
0: dx := 1;
1: dy := 1;
2: dx := -1;
else dy := -1;
end;
x1 := x + dx;
y1 := y + dy;
x2 := x1 + dx;
y2 := y1 + dy;
if (x2 > 0) and (x2 < width) and (y2 > 0) and (y2 < height)
and (maze[x1, y1] = Wall) and (maze[x2, y2] = Wall) then
begin
maze[x1, y1] := Space;
maze[x2, y2] := Space;
carve_maze(x2, y2);
end;
dir := (dir + 1) mod 4;
cnt := cnt + 1;
end;
end;
(* Generate the maze. *)
procedure generate_maze;
begin
maze[1, 1] := Space;
carve_maze(1, 1);
maze[1, 0] := Space;
maze[width - 2, height - 1] := Space;
end;
(* Show the maze. *)
procedure show_maze;
var
x, y : integer;
begin
for y := 0 to height - 1 do
begin
for x := 0 to width - 1 do
if maze[x, y] = Space then
write(' ')
else
write('[]');
writeln('');
end;
end;
begin
randomize;
init_maze;
generate_maze;
show_maze;
end.