File Coverage

blib/lib/Acme/GuessNumber.pm
Criterion Covered Total %
statement 29 42 69.0
branch 5 10 50.0
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 40 58 68.9


line stmt bran cond sub pod time code
1             # Acme::GuessNumber - Number guessing game robot
2              
3             # Copyright (c) 2007-2021 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/5/19
7              
8             package Acme::GuessNumber;
9 1     1   6805 use strict;
  1         12  
  1         37  
10 1     1   5 use warnings;
  1         2  
  1         53  
11 1     1   6 use base qw(Exporter);
  1         2  
  1         371  
12             our ($VERSION, @EXPORT, @EXPORT_OK);
13             $VERSION = 0.05;
14             @EXPORT = qw(guess_number HURRY_UP);
15             @EXPORT_OK = @EXPORT;
16             # Prototype declaration
17             sub guess_number($;$);
18              
19             our (@GUESS_MSGS, @RESULT_MSGS);
20             @GUESS_MSGS = split /\n/, << "EOT";
21             %d?
22             Is it %d?
23             It must be %d!
24             EOT
25             @RESULT_MSGS = 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   8 use constant HURRY_UP => 1;
  1         3  
  1         523  
35              
36             # guess_number: Play the game
37             sub guess_number($;$) {
38 1     1 1 12 local ($_, %_);
39 1         2 my ($max, $hurry, $to_rest);
40 1         4 ($max, $hurry) = @_;
41 1 50       4 $hurry = 0 if @_ < 2;
42            
43             # Play the game
44 1 50       3 $to_rest = 5 + int rand 7 unless $hurry;
45 1         1 while (1) {
46 51         74 my ($num, $guess);
47             # Generate the number
48 51         125 $num = 1 + int rand $max;
49             # Generate the guess
50 51         72 $guess = 1 + int rand $max;
51             # Output our guess
52 51         144 $_ = sprintf $GUESS_MSGS[int rand scalar @GUESS_MSGS], $guess;
53 51         412 printf "%-40s", ": $_";
54             # Hit?
55 51 100       164 if ($guess == $num) {
56 1         7 print ": Jackpot!\n";
57 1         3 last;
58             }
59 50         305 print ": Sorry, it's $num.\n";
60             # We are in a hurry
61 50 50       126 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 " *" . $RESULT_MSGS[int rand scalar @RESULT_MSGS] . "* ";
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         5 return;
85             }
86              
87             return 1;
88              
89             __END__