Skip to content

Instantly share code, notes, and snippets.

@masak
Created July 18, 2011 20:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masak/1090618 to your computer and use it in GitHub Desktop.
Save masak/1090618 to your computer and use it in GitHub Desktop.
The adventure game with just the rooms
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.";
}
}
}
== 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment