use strict;
use warnings;
use constant HIGH => 1024;
my $the_number = int(rand HIGH) + 1;
print "The number is $the_number\n";
my $high;
my $low;
my $answer;
my $guess;
ask_question();
sub ask_question {
sleep(1);
if (!$answer) {
$high = HIGH;
$low = 1;
} elsif ($answer == 0) {
return;
} elsif ($answer == 1) {
$low = $guess + 1;
} elsif ($answer == -1) {
$high = $guess - 1;
}
$guess = int(($low + $high) / 2);
print "Is your number $guess? ";
answer();
}
sub answer {
if ($guess < $the_number) {
print "Go higher ...\n";
$answer = 1;
ask_question();
} elsif ($guess == $the_number) {
print "You got it!\n";
$answer = 0;
} else {
print "Go lower ...\n";
$answer = -1;
ask_question();
}
}