line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Games::Wumpus; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
40912
|
use 5.010; |
|
2
|
|
|
|
|
81
|
|
|
2
|
|
|
|
|
97
|
|
4
|
2
|
|
|
2
|
|
10
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
81
|
|
5
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
93
|
|
6
|
2
|
|
|
2
|
|
10
|
no warnings 'syntax'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
264
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '2009112401'; |
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
1349
|
use Hash::Util::FieldHash qw [fieldhash]; |
|
2
|
|
|
|
|
1470
|
|
|
2
|
|
|
|
|
134
|
|
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
791
|
use Games::Wumpus::Constants; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
394
|
|
13
|
2
|
|
|
2
|
|
781
|
use Games::Wumpus::Cave; |
|
2
|
|
|
|
|
19
|
|
|
2
|
|
|
|
|
62
|
|
14
|
2
|
|
|
2
|
|
127
|
use Games::Wumpus::Room; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
6542
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
fieldhash my %cave; |
17
|
|
|
|
|
|
|
fieldhash my %arrows; |
18
|
|
|
|
|
|
|
fieldhash my %finished; |
19
|
|
|
|
|
|
|
|
20
|
0
|
|
|
0
|
1
|
|
sub new {bless \do {my $var} => shift} |
|
0
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub init { |
22
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
23
|
0
|
|
|
|
|
|
my %args = @_; |
24
|
|
|
|
|
|
|
|
25
|
0
|
|
|
|
|
|
$cave {$self} = Games::Wumpus::Cave -> new -> init (%args); |
26
|
0
|
|
|
|
|
|
$arrows {$self} = $NR_OF_ARROWS; |
27
|
|
|
|
|
|
|
|
28
|
0
|
|
|
|
|
|
$cave {$self} -> set_location ($cave {$self} -> start); |
29
|
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
|
$self; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# |
34
|
|
|
|
|
|
|
# Accessors |
35
|
|
|
|
|
|
|
# |
36
|
0
|
|
|
0
|
1
|
|
sub cave { $cave {$_ [0]}} |
37
|
0
|
|
|
0
|
0
|
|
sub arrows { $arrows {$_ [0]}} |
38
|
0
|
|
|
0
|
1
|
|
sub lose_arrow {-- $arrows {$_ [0]}} |
39
|
0
|
|
|
0
|
1
|
|
sub finished { $finished {$_ [0]}} |
40
|
0
|
|
|
0
|
1
|
|
sub win { $finished {$_ [0]} = 1} |
41
|
0
|
|
|
0
|
1
|
|
sub lose { $finished {$_ [0]} = 0} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# |
45
|
|
|
|
|
|
|
# Describe the current situation |
46
|
|
|
|
|
|
|
# |
47
|
|
|
|
|
|
|
sub describe { |
48
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
my $text = $self -> cave -> describe; |
51
|
0
|
|
|
|
|
|
$text .= "You have " . $self -> arrows . " arrows left.\n"; |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
$text; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# |
57
|
|
|
|
|
|
|
# Try to move a different room. The argument is well formatted, |
58
|
|
|
|
|
|
|
# but not necessarely valid. |
59
|
|
|
|
|
|
|
# |
60
|
|
|
|
|
|
|
sub move { |
61
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
62
|
0
|
|
|
|
|
|
my $new = shift; |
63
|
|
|
|
|
|
|
|
64
|
0
|
0
|
|
|
|
|
unless ($self -> cave -> can_move_to ($new)) { |
65
|
0
|
|
|
|
|
|
return (0, "There's no tunnel to $new."); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
my @hazards = $self -> cave -> move ($new); |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
my @messages; |
71
|
0
|
|
|
|
|
|
foreach (@hazards) { |
72
|
0
|
|
|
|
|
|
when ($WUMPUS) { |
73
|
0
|
|
|
|
|
|
$self -> lose; |
74
|
0
|
|
|
|
|
|
push @messages => "Oops! Bumped into a Wumpus!"; |
75
|
|
|
|
|
|
|
} |
76
|
0
|
|
|
|
|
|
when ($PIT) { |
77
|
0
|
|
|
|
|
|
$self -> lose; |
78
|
0
|
|
|
|
|
|
push @messages => "YYYIIIIEEEE! Fell in a pit!"; |
79
|
|
|
|
|
|
|
} |
80
|
0
|
|
|
|
|
|
when ($BAT) { |
81
|
0
|
|
|
|
|
|
push @messages => "ZAP! Super bat snatch! Elsewhereville for you!"; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
} |
84
|
0
|
|
|
|
|
|
return 1, @messages; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# |
90
|
|
|
|
|
|
|
# Try to move a different room. The argument is well formatted, |
91
|
|
|
|
|
|
|
# but not necessarely valid. |
92
|
|
|
|
|
|
|
# |
93
|
|
|
|
|
|
|
sub shoot { |
94
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
95
|
0
|
|
|
|
|
|
my @rooms = @_; |
96
|
|
|
|
|
|
|
|
97
|
0
|
0
|
|
|
|
|
if ($self -> arrows < 1) { |
98
|
|
|
|
|
|
|
# |
99
|
|
|
|
|
|
|
# This shouldn't be able to happen. |
100
|
|
|
|
|
|
|
# |
101
|
0
|
|
|
|
|
|
$self -> lose; |
102
|
0
|
|
|
|
|
|
return 0, "You are out of arrows"; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
0
|
0
|
|
|
|
|
unless ($self -> cave -> can_move_to ($rooms [0])) { |
106
|
0
|
|
|
|
|
|
return (0, "There's no tunnel to " . $rooms [0]); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
for (my $i = 2; $i < @rooms; $i ++) { |
110
|
0
|
0
|
|
|
|
|
if ($rooms [$i] eq $rooms [$i - 2]) { |
111
|
0
|
|
|
|
|
|
return 0, "Arrows aren't that crooked - try another path"; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
my $hit = $self -> cave -> shoot (@rooms); |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
my @mess; |
118
|
0
|
|
|
|
|
|
given ($hit) { |
119
|
0
|
|
|
|
|
|
when ($WUMPUS) { |
120
|
0
|
|
|
|
|
|
$self -> win; |
121
|
0
|
|
|
|
|
|
return 1, "Ha! You got the Wumpus!"; |
122
|
|
|
|
|
|
|
} |
123
|
0
|
|
|
|
|
|
when ($PLAYER) { |
124
|
0
|
|
|
|
|
|
$self -> lose; |
125
|
0
|
|
|
|
|
|
return 1, "Ouch! Arrow got you!"; |
126
|
|
|
|
|
|
|
} |
127
|
0
|
|
|
|
|
|
default { |
128
|
0
|
|
|
|
|
|
push @mess => "Missed!"; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
$self -> cave -> stir_wumpus; |
133
|
|
|
|
|
|
|
|
134
|
0
|
0
|
|
|
|
|
if ($self -> cave -> wumpus == $self -> cave -> location) { |
135
|
0
|
|
|
|
|
|
$self -> lose; |
136
|
0
|
|
|
|
|
|
return 1, @mess, "Tsk Tsk Tsk - Wumpus got you!"; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
0
|
0
|
|
|
|
|
if ($self -> lose_arrow < 1) { |
140
|
0
|
|
|
|
|
|
$self -> lose; |
141
|
0
|
|
|
|
|
|
return 1, @mess, |
142
|
|
|
|
|
|
|
"You ran out of arrows. Wumpus will eventually eat you." |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
0
|
|
|
|
|
|
return 1, @mess; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
1; |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
__END__ |