line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Acme::GuessNumber - Number guessing game robot |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# Copyright (c) 2007-2008 imacat. All rights reserved. This program is free |
4
|
|
|
|
|
|
|
# software; you can redistribute it and/or modify it under the same terms |
5
|
|
|
|
|
|
|
# as Perl itself. |
6
|
|
|
|
|
|
|
# First written: 2007-05-19 |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
package Acme::GuessNumber; |
9
|
1
|
|
|
1
|
|
11636
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
10
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
11
|
1
|
|
|
1
|
|
9
|
use base qw(Exporter); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
432
|
|
12
|
1
|
|
|
1
|
|
7
|
use vars qw($VERSION @EXPORT @EXPORT_OK); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
103
|
|
13
|
|
|
|
|
|
|
$VERSION = 0.04; |
14
|
|
|
|
|
|
|
@EXPORT = qw(guess_number HURRY_UP); |
15
|
|
|
|
|
|
|
@EXPORT_OK = @EXPORT; |
16
|
|
|
|
|
|
|
# Prototype declaration |
17
|
|
|
|
|
|
|
sub guess_number($;$); |
18
|
|
|
|
|
|
|
|
19
|
1
|
|
|
1
|
|
6
|
use vars qw(@GUESSMSGS @RESTMSGS); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
192
|
|
20
|
|
|
|
|
|
|
@GUESSMSGS = split /\n/, << "EOT"; |
21
|
|
|
|
|
|
|
%d? |
22
|
|
|
|
|
|
|
Is it %d? |
23
|
|
|
|
|
|
|
It must be %d! |
24
|
|
|
|
|
|
|
EOT |
25
|
|
|
|
|
|
|
@RESTMSGS = split /\n/, << "EOT"; |
26
|
|
|
|
|
|
|
Any cigarette? |
27
|
|
|
|
|
|
|
I'm feeling lucky tonight |
28
|
|
|
|
|
|
|
I'm getting a little tired |
29
|
|
|
|
|
|
|
It's killing me |
30
|
|
|
|
|
|
|
I'm gonna faint |
31
|
|
|
|
|
|
|
EOT |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# HURRY_UP: Speed up the game |
34
|
1
|
|
|
1
|
|
5
|
use constant HURRY_UP => 1; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
687
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# guess_number: Play the game |
37
|
|
|
|
|
|
|
sub guess_number($;$) { |
38
|
1
|
|
|
1
|
1
|
8
|
local ($_, %_); |
39
|
1
|
|
|
|
|
3
|
my ($max, $hurry, $to_rest); |
40
|
1
|
|
|
|
|
3
|
($max, $hurry) = @_; |
41
|
1
|
50
|
|
|
|
4
|
$hurry = 0 if @_ < 2; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# Play the game |
44
|
1
|
50
|
|
|
|
4
|
$to_rest = 5 + int rand 7 unless $hurry; |
45
|
1
|
|
|
|
|
2
|
while (1) { |
46
|
18
|
|
|
|
|
20
|
my ($num, $guess); |
47
|
|
|
|
|
|
|
# Generate the number |
48
|
18
|
|
|
|
|
73
|
$num = 1 + int rand $max; |
49
|
|
|
|
|
|
|
# Generate the guess |
50
|
18
|
|
|
|
|
20
|
$guess = 1 + int rand $max; |
51
|
|
|
|
|
|
|
# Output our guess |
52
|
18
|
|
|
|
|
62
|
$_ = sprintf $GUESSMSGS[int rand scalar @GUESSMSGS], $guess; |
53
|
18
|
|
|
|
|
701
|
printf "%-40s", ": $_"; |
54
|
|
|
|
|
|
|
# Hit? |
55
|
18
|
100
|
|
|
|
79
|
if ($guess == $num) { |
56
|
1
|
|
|
|
|
5
|
print ": Jackpot!\n"; |
57
|
1
|
|
|
|
|
3
|
last; |
58
|
|
|
|
|
|
|
} |
59
|
17
|
|
|
|
|
1440
|
print ": Sorry, it's $num.\n"; |
60
|
|
|
|
|
|
|
# We are in a hurry |
61
|
17
|
50
|
|
|
|
48
|
next if $hurry; |
62
|
|
|
|
|
|
|
# Take a little rest |
63
|
0
|
0
|
|
|
|
0
|
if (--$to_rest == 0) { |
64
|
0
|
|
|
|
|
0
|
my ($flush, $rest); |
65
|
0
|
|
|
|
|
0
|
$flush = $|; |
66
|
0
|
|
|
|
|
0
|
$| = 1; |
67
|
|
|
|
|
|
|
# Yell something |
68
|
0
|
|
|
|
|
0
|
print " *" . $RESTMSGS[int rand scalar @RESTMSGS] . "* "; |
69
|
0
|
|
|
|
|
0
|
$rest = 3 + int rand 4; |
70
|
0
|
|
|
|
|
0
|
while ($rest-- > 0) { |
71
|
0
|
|
|
|
|
0
|
print "."; |
72
|
0
|
|
|
|
|
0
|
sleep 1; |
73
|
|
|
|
|
|
|
} |
74
|
0
|
|
|
|
|
0
|
print "\n"; |
75
|
0
|
|
|
|
|
0
|
$| = $flush; |
76
|
|
|
|
|
|
|
# Reset the rest counter |
77
|
0
|
|
|
|
|
0
|
$to_rest = 5 + int rand 7; |
78
|
|
|
|
|
|
|
# Take a breath |
79
|
|
|
|
|
|
|
} else { |
80
|
0
|
|
|
|
|
0
|
sleep 1; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
1
|
|
|
|
|
4
|
return; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
return 1; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
__END__ |