public
Created

The adventure game with just the rooms

  • Download Gist
crypt-rooms.pl
Perl
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
use v6;
 
say "CRYPT";
say "=====";
say "";
 
say "You've heard there's supposed to be an ancient hidden crypt in these";
say "woods. One containing a priceless treasure. Well, there's only one way";
say "to find out...";
say "";
 
my %descriptions;
for slurp("room-descriptions").split(/\n\n/) {
/^^ '== ' (\N+) \n (.*)/
or die "Could not parse 'room-descriptions' file: $_";
%descriptions{$0} = ~$1;
}
 
my @directions = <
north south east west
northeast northwest southeast southwest
up down in out
>;
 
my %abbr_directions = <
n north
s south
e east
w west
ne northeast
nw northwest
se southeast
sw southwest
u up
d down
>;
 
subset Direction of Str where any(@directions);
 
sub opposite_direction(Direction $d) {
my %opposites =
'north' => 'south',
'east' => 'west',
'northeast' => 'southwest',
'northwest' => 'southeast',
'up' => 'down',
'in' => 'out',
;
 
%opposites.push( %opposites.invert );
 
%opposites{$d};
}
 
role Thing {
has Str $.name;
has Str $!description = %descriptions{$.name};
 
method examine {
say $!description;
self.?on_examine;
}
}
 
my $room;
role Room does Thing {
has Direction %.exits is rw;
has Direction $.in;
has Direction $.out;
has Bool $!visited = False;
 
method connect(Direction $direction, Room $other_room) {
my $opposite = opposite_direction($direction);
self.exits{$direction} = $other_room;
$other_room.exits{$opposite} = self;
}
 
method disconnect(Direction $direction) {
my $opposite = opposite_direction($direction);
my $other_room = self.exits.delete($direction);
if $other_room {
$other_room.exits.delete($opposite);
}
}
 
method list_exits {
given %.exits {
when 0 {
say "There are no obvious exits from here.";
}
when 1 {
say "You can go {.keys}.";
}
when 2 {
say "You can go {.keys.join(" and ")}.";
}
default {
say "You can go {.keys[0..*-2].join(", ")} and {.keys[*-1]}.";
}
}
}
 
method look {
if there_is_light() {
self.examine;
self.list_exits;
}
else {
say "It is pitch black.";
}
}
 
method enter {
say $!name;
$room = self;
 
unless $!visited {
say "";
self.look;
}
 
$!visited = True;
self.?on_enter;
}
}
 
class Cave does Room {
method on_try_exit($direction) {
if $direction eq "northwest" {
say "You try to walk past the fire, but it's too hot!";
return False;
}
return True;
}
}
 
sub there_is_light() {
return True; # will be more complex in the complete game
}
 
my %rooms =
clearing => Room.new( :name<Clearing>, ),
hill => Room.new( :name<Hill>, :in<south> ),
chamber => Room.new( :name(<Chamber>), :out<north> ),
hall => Room.new( :name(<Hall>) ),
cave => Cave.new( :name(<Cave>) ),
crypt => Room.new( :name(<Crypt>) ),
;
 
%rooms<clearing>.connect('east', %rooms<hill>);
%rooms<hill>.connect('south', %rooms<chamber>);
%rooms<chamber>.connect('south', %rooms<hall>);
%rooms<hall>.connect('down', %rooms<cave>);
%rooms<cave>.connect('northwest', %rooms<crypt>);
 
%rooms<clearing>.enter;
 
loop {
say "";
my $command = prompt("> ");
 
given $command {
when !.defined || .lc eq "q" | "quit" {
say "";
my $really = prompt "Really quit (y/N)? ";
if !defined $really || "y"|"yes" eq lc $really {
say "Thanks for playing.";
exit;
}
}
 
$command .= trim;
$command .= lc;
 
when "" {
succeed;
}
 
when /^ :s go (\w+) $/
&& $0 eq any @directions, %abbr_directions.keys, <in out> {
 
$command = ~$0;
proceed;
}
 
when any(%abbr_directions.keys) {
$command = %abbr_directions{$command};
proceed;
}
 
when 'in' {
if $room.in -> $real_direction {
$command = $real_direction;
}
proceed;
}
 
when 'out' {
if $room.out -> $real_direction {
$command = $real_direction;
}
proceed;
}
 
when any(@directions) {
my $direction = $command;
if $room.exits{$direction} -> $new_room {
my $succeeded = $room.?on_try_exit($direction) // True;
if $succeeded {
$new_room.enter;
}
}
else {
say "Sorry, you can't go $direction from here.";
}
}
 
when "look"|"l" {
$room.look;
}
 
default {
say "Sorry, I did not understand that.";
say "Type 'help' for suggestions.";
}
}
}
room-descriptions
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
== Clearing
The forest road stops here, and the gaps between the trees widen into a
patch of un-forest. The sky above is clear apart from a few harmless clouds.
 
== Hill
A flat, broad hill interrupts the dense-ish forest here. Only grass and
small bushes grow on the hill.
 
== Chamber
This is a cramped space just inside the hidden opening in the hill. The
sun gets in enough to illuminate the place. There are some scribblings on
the wall.
 
== Hall
It's hard to believe that this kind of room can fit under a hill. It's tall,
long, and quite spacious. Hieroglyphs adorn all four of the walls. The
floor slants a bit.
 
== Cave
This is a perfectly cylindrical chamber. An oversized fire-pit takes up most
of the floor space in the middle of the room. Ancient runes run along the
circumference of the wall. It's hot and stuffy.
 
== Crypt
The air is stale and smells a bit metallic. Three impenetrable sarcophagi
sit next to each other on the floor. On the walls are numerous finely
inscribed ideograms.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.