10 token TOP
{ <command
> }
12 | <verb
> <ws
> <article
> <ws
> <object
>
13 | <verb
> <ws
> <object
>
18 token verb
{ look
|take
|drop
|inventory
|open|score
|help
|quit
}
19 token object
{ sign
|coin
|key
|door
|vampire
|cross
}
20 token article
{ a
|an
|the
|at
|in|on
|to
}
21 token direction
{ north
|south
|east
|west
}
34 north
=> 'throne room',
43 BLOCKED_south
=> 'cell' # there is no "south"... yet
56 description
=> "Sign says: bring treasures here, then say SCORE"
66 description
=> "The door is closed"
72 place
=> 'throne room'
79 sub do_action
( $verb, $object ) {
80 say "[$verb] [$object]";
83 'look' => sub { look
($object) },
84 'take' => sub { take
($object) },
85 'drop' => sub { drop
($object) },
86 'open' => sub { open($object) },
87 'inventory' => sub { inventory
() },
88 'score' => sub { score
() },
89 'help' => sub { help
() },
90 'quit' => sub { quit
() },
99 say "look, take, drop, inventory, open, score, help, quit";
100 say "north, south, east, west";
108 sub walk_to
( $direction ) {
109 my $new_place = %map{%player{'place'}}{$direction};
111 if $new_place { # can go to that direction?
112 say "You entered the $new_place";
113 %player{'place'} = $new_place;
115 say "You can't go $direction";
119 sub look
( $object ) {
120 if i_see
( $object ) {
121 my $description = %object{$object}{'description'};
125 say "It's a regular $object";
134 say "You are in the " ~ %player{'place'};
136 my @objects = objects_in
( %player{'place'} );
137 my $list = join(", ", @objects);
139 say "I see here: $list" if $list;
143 my @objects = objects_in
('player');
144 my $list = join(", ", @objects);
146 $list = "nothing" if !$list;
148 say "You're carrying: $list.";
151 sub take
( $object ) {
152 if is_here
( $object ) {
153 say "You took the $object";
154 %object{$object}{'place'} = 'player';
156 say "I don't see that here";
160 sub drop
( $object ) {
161 if i_have
( $object ) {
162 say "You dropped the $object on the floor";
163 %object{$object}{'place'} = %player{'place'};
165 say "You don't have that $object";
169 sub open( $object ) {
170 if $object ne 'door' {
171 say "You can't open that!";
175 if is_here
( $object ) {
177 say "You opened the door!";
178 %object{'door'}{'description'} = 'The door is open';
179 %map{ %player{'place'} }{'south'} = 'cell';
181 say "The door is locked";
184 say "I don't see a $object";
190 say "You got the treasure. Congratulations!";
192 say "You have scored " ~ %player{'score'} ~ " points";
195 ### AUXILIAR FUNCTIONS ###
197 sub is_here
($object) {
198 if $object ~~ %object {
199 return ( %object{$object}{'place'} eq %player{'place'} );
205 sub i_have
($object) {
206 if $object ~~ %object {
207 return ( %object{$object}{'place'} eq 'player' );
213 sub i_see
($object) {
214 return ( is_here
( $object) or i_have
( $object ) );
217 sub objects_in
($where) {
219 for %object.kv
-> $name, $obj {
220 if $obj{'place'} eq $where {
221 @objects.push: $name;
231 my $input = $*IN
.get
();
232 my $response = Adventure
::command
($input);
234 if my $direction = $response<direction
> {
235 walk_to
( ~$direction );
237 elsif my $verb = $response<verb
> {
238 do_action
( ~$verb, ~($response<object
> or "") )