-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvent.pl
107 lines (83 loc) · 1.98 KB
/
advent.pl
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
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
use Data::Dumper;
use lib './lib';
use Game::Player::Wizard;
my $player = Game::Player::Wizard->new(
{
location => 'entrance',
alive => 1,
}
);
my @objects = (
{
name => 'dragon',
location => 'entrance',
alive => 1,
},
{
name => 'sword',
location => 'entrance',
},
{
name => 'stone',
location => 'museum',
},
);
my %object_by_name = map { $_->{name} => $_ } @objects;
while ( $player->alive ) {
say "You are here: ", $player->location;
print "What do you want to do? ";
my $sentence = readline();
my ( $verb, $obj_name ) = split( /\s+/, $sentence );
if ( $verb eq 'quit' ) {
say "Bye!";
exit;
} elsif ( $verb eq 'look' ) {
look();
} elsif ( $verb eq 'take' ) {
take($obj_name);
} elsif ( $verb eq 'slay' ) {
slay($obj_name);
} elsif ( $verb eq 'magic' ) {
$player->magic();
} else {
say "I don't understand what you want to do!";
}
say "";
}
sub look {
say "I see here:";
my @obj_here =
map { $_->{name} }
grep { $_->{location} eq $player->location } @objects;
if (@obj_here) {
say join( "; ", @obj_here );
} else {
say "Nothing special.";
}
}
sub take {
my ($obj_name) = @_;
my $object = $object_by_name{$obj_name};
return if !$object;
return if $object->{location} ne $player->location;
return if $object->{alive};
$object->{location} = 'PLAYER';
say "Taken!";
}
sub slay {
my ($obj_name) = @_;
my $object = $object_by_name{$obj_name};
return if !$object;
return if $object->{location} ne $player->location;
return if !$object->{alive};
if ( $object_by_name{sword}->{location} ne 'PLAYER' ) {
say "With your bare hands???";
return;
}
$object->{alive} = 0;
say "You killed it!";
}