| 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 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use base qw(Exporter); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use vars qw($VERSION @EXPORT @EXPORT_OK); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | $VERSION = 0.04; | |||||||||
| 14 | 1 | @EXPORT = qw(guess_number HURRY_UP); | |||||||||
| 15 | 1 | @EXPORT_OK = @EXPORT; | |||||||||
| 16 | # Prototype declaration | ||||||||||
| 17 | sub guess_number($;$); | ||||||||||
| 18 | |||||||||||
| 19 | 1 | ▶ | use vars qw(@GUESSMSGS @RESTMSGS); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 1 | @GUESSMSGS = split /\n/, << "EOT"; | |||||||||
| 21 | %d? | ||||||||||
| 22 | Is it %d? | ||||||||||
| 23 | It must be %d! | ||||||||||
| 24 | EOT | ||||||||||
| 25 | 1 | @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 | ▶ | use constant HURRY_UP => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | |||||||||||
| 36 | # guess_number: Play the game | ||||||||||
| 37 | sub guess_number($;$) { | ||||||||||
| 38 | 1 | ▶ | local ($_, %_); | ||||||||
|
Subroutine
guess_number: called CC=7 SCAR=25.1
Pod
documented
| |||||||||||
| 39 | 1 | my ($max, $hurry, $to_rest); | |||||||||
| 40 | 1 | ($max, $hurry) = @_; | |||||||||
| 41 | 1 | ▶ | $hurry = 0 if @_ < 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | |||||||||||
| 43 | # Play the game | ||||||||||
| 44 | 1 | ▶ | $to_rest = 5 + int rand 7 unless $hurry; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 1 | while (1) { | |||||||||
| 46 | 26 | my ($num, $guess); | |||||||||
| 47 | # Generate the number | ||||||||||
| 48 | 26 | $num = 1 + int rand $max; | |||||||||
| 49 | # Generate the guess | ||||||||||
| 50 | 26 | $guess = 1 + int rand $max; | |||||||||
| 51 | # Output our guess | ||||||||||
| 52 | 26 | $_ = sprintf $GUESSMSGS[int rand scalar @GUESSMSGS], $guess; | |||||||||
| 53 | 26 | printf "%-40s", "<Player>: $_"; | |||||||||
| 54 | # Hit? | ||||||||||
| 55 | 26 | ▶ | if ($guess == $num) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 1 | print "<Banker>: Jackpot!\n"; | |||||||||
| 57 | 1 | last; | |||||||||
| 58 | } | ||||||||||
| 59 | 25 | print "<Banker>: Sorry, it's $num.\n"; | |||||||||
| 60 | # We are in a hurry | ||||||||||
| 61 | 25 | ▶ | next if $hurry; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | # Take a little rest | ||||||||||
| 63 | 0 | ▶ | if (--$to_rest == 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 0 | my ($flush, $rest); | |||||||||
| 65 | 0 | $flush = $|; | |||||||||
| 66 | 0 | $| = 1; | |||||||||
| 67 | # Yell something | ||||||||||
| 68 | 0 | print " *" . $RESTMSGS[int rand scalar @RESTMSGS] . "* "; | |||||||||
| 69 | 0 | $rest = 3 + int rand 4; | |||||||||
| 70 | 0 | ▶ | while ($rest-- > 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 0 | print "."; | |||||||||
| 72 | 0 | sleep 1; | |||||||||
| 73 | } | ||||||||||
| 74 | 0 | print "\n"; | |||||||||
| 75 | 0 | $| = $flush; | |||||||||
| 76 | # Reset the rest counter | ||||||||||
| 77 | 0 | $to_rest = 5 + int rand 7; | |||||||||
| 78 | # Take a breath | ||||||||||
| 79 | } else { | ||||||||||
| 80 | 0 | sleep 1; | |||||||||
| 81 | } | ||||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 1 | return; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 1 | return 1; | |||||||||
| 88 | |||||||||||
| 89 | __END__ | ||||||||||