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();
}
}
##```
##
The number is 1001
Is your number 512? Go higher ...
Is your number 768? Go higher ...
Is your number 896? Go higher ...
Is your number 960? Go higher ...
Is your number 992? Go higher ...
Is your number 1008? Go lower ...
Is your number 1000? Go higher ...
Is your number 1004? Go lower ...
Is your number 1002? Go lower ...
Is your number 1001? You got it!
```