Just for kicks, here's my corrolary, where the computer guesses. It turned out a lot simpler. I'm trying harder to make it pretty, too.
#The computer will try to guess your number.
#Watch out! It knows if you're cheating.
use strict;
use warnings;
sub game{
my $min=0;
my $max=200;
my $guess;
my $response="h";
print "Think of a number between $min and $max, and I will guess wha
+t it is. Type h if your number is higher than my guess, and type l i
+f it is lower. Type = if I have guessed your number.\n";
while ($response ne "="){
if ((($max-$min)/2)<1){
print "You're not playing fair! I quit.\n";
return;
}
else{
$guess=int($max-(($max-$min)/2));
print "My guess is ",$guess,".\n";
$response = <STDIN>;
chomp($response);
if ($response eq "h"){
$min=$guess;
}
elsif ($response eq "l"){
$max=$guess;
}
}
}
}
my $again = 'y';
while ($again eq 'y'){
game();
print "That was fun--no really, it was. Play again? (y/n) \n";
$again = <STDIN>;
chomp ($again);
}
print "Goodbye!\n";
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.