-
Notifications
You must be signed in to change notification settings - Fork 53
/
maze
executable file
·186 lines (137 loc) · 4.05 KB
/
maze
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/perl
=begin metadata
Name: maze
Description: generate a maze problem
Author: Rocco Caputo, troc@netrus.net
License: perl
=end metadata
=cut
use strict;
sub R { 1 }
sub B { 2 }
sub L { 4 }
sub T { 8 }
my (@maze, @walk);
#
## Parse maze type options.
sub usage {
die "usage: $0 [-fl|-fi|-df|-sf] [width height]\n"
}
sub traverse_by_depth { -1 } # normal mazes (long walks)
sub traverse_by_breadth { 0 } # flood fills (short walks)
sub traverse_randomly { rand(@walk) } # fiendish mazez (random walks)
sub traverse_randomly_deep {-rand(@walk/2) } # longer random walks
sub traverse_randomly_shallow { rand(@walk/2) } # shorter random walks
my %alg = (
'-fl' => \&traverse_by_breadth,
'-fi' => \&traverse_randomly,
'-df' => \&traverse_randomly_deep,
'-sf' => \&traverse_randomly_shallow,
);
my $walk_function = \&traverse_by_depth;
while (@ARGV && $ARGV[0] =~ /^\-/) {
my $switch = shift;
$walk_function = $alg{$switch} or usage();
}
my $width = shift;
my $height = shift;
usage() if @ARGV;
sub get_number {
my ($prompt, $value) = @_;
while (!defined($value) or $value < 2) {
(defined $value) and ($value < 2) and print "$prompt too small.\n";
print "$prompt? ";
chomp($value = <STDIN>);
}
$value;
}
$width = &get_number('width', $width);
$height = &get_number('height', $height);
my $test_width = $width - 1;
my $test_height = $height - 1;
#
## initialize the maze
@maze = map { [ (0) x $width ] } (1 .. $height);
my $in = int(rand($width));
push @walk, [0, $in];
#
## random walk the maze, knocking down walls
##
## <purl> Cross over the cell bars, find a new maze, make the maze
## from its path, find the cell bars, cross over the bars, find a
## maze, make the maze from its path, eat the food, eat the path.
while (@walk) {
my $walk_index = &$walk_function();
my ($y, $x) = @{$walk[$walk_index]};
my @good_directions;
push(@good_directions, [ T, B, $y-1, $x ])
if ($y && !$maze[$y-1][$x]);
push(@good_directions, [ B, T, $y+1, $x ])
if (($y < $test_height) && !$maze[$y+1][$x]);
push(@good_directions, [ L, R, $y, $x-1 ])
if ($x && !$maze[$y][$x-1]);
push(@good_directions, [ R, L, $y, $x+1 ])
if (($x < $test_width) && !$maze[$y][$x+1]);
unless (@good_directions) {
splice(@walk, $walk_index, 1);
next;
}
my ($direction, $complementary_direction, $next_y, $next_x) =
@{$good_directions[rand @good_directions]};
$maze[$y][$x] |= $direction;
$maze[$next_y][$next_x] |= $complementary_direction;
splice(@walk, $walk_index, 1) if (@good_directions == 1);
push @walk, [ $next_y, $next_x ];
}
#
## display the maze
#
my @cellbits = ( [ '', ' ', ' +', '', '|', '', '', '', ' +' ],
[ '', ' |', '--+', '', '|', '', '', '', '--+' ],
);
my @wallbits = ( '', '|', '+' );
#
## input at top
#
print "+";
for (my $x=0; $x<$width; $x++) {
print $cellbits[$x!=$in]->[T];
}
print "\n";
#
## output at bottom of maze
#
$maze[-1]->[rand($width)] |= B;
#
## maze itself
#
foreach my $row (@maze) {
foreach my $wall (R, B) {
print $wallbits[$wall];
foreach my $cell (@$row) {
print $cellbits[!($cell & $wall)]->[$wall];
}
print "\n";
}
}
__END__
=head1 NAME
maze - generate a maze problem
=head1 SYNOPSIS
maze [ -fl | -fi | -df | -sf ] [width height]
=head1 DESCRIPTION
Without arguments, maze defaults to the standard behavior. It asks
for the desired width and height, then displays a maze on standard
output.
Maze contains five maze types: the normal one (no option), flood fills
(-fl), fiendish random mazes (-fi), fiendish favoring longer paths
("deep" fiendish: -df), and fiendish favoring shorter paths ("shallow"
fiendish: -sf).
Maze also accepts the width and height on the command line. If either
is too small, it will prompt for a replacement.
=head1 BUGS
Large mazes are slow.
=head1 AUTHOR and COPYRIGHT
Maze is Copyright 1999 Rocco Caputo <troc@netrus.net>. All rights
reserved. Maze is free software; you may redistribute it and/or
modify it under the same terms as Perl itself.