in reply to Re: Number Guess
in thread Number Guess
#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";
|
---|