Chapter 14 – Your First Perl Game

If you’ve been with us this far, you’ve done quite a bit of learning. So let’s take a break and create a Perl game with all that we’ve learned so far!

This is a Guess a Number (GAN) where the computer makes a random number and you have to guess what the number is within a specified number of guesses. This game will record statistics and report back on whether the guess was too high or too low.

This script assumes you have been learning Perl following our class or tutorial and haven’t learned about databases yet. This is why we are saving our data to textfiles instead of a Perl built-in database. Even though this is a complete script, there are some things missing that would make this more stable (such as $guess checking) which we haven’t gone over yet so these features have been excluded.

For this to work, you need to create two text files before running the script. win.txt and lose.txt.

The code (with line numbers):

01: #!/usr/bin/perl
02:
03: use warnings;
04: use strict;
05:
06: my $guess;
07: my $tries = 0;
08: my $allowed = 5;
09:
10: print “what is the max number you want to try for? “;
11: my $max = <STDIN>;
12: chomp($max);
13: my $answer = int(rand($max))+1;
14:
15: while ($tries < $allowed)
16: {
17: print “Your guess: “;
18: $guess = <STDIN>;
19: chomp($guess);
20: $tries++;
21:
22: if ($guess eq $answer)
23: {
24: last;
25: }
26: elsif ($guess > $answer)
27: {
28: print “\t$guess is too high\n”;
29: }
30: elsif ($guess < $answer)
31: {
32: print “\t$guess is too low\n”;
33: }
34: }
35:
36: if ($guess eq $answer)
37: {
38: print “\nYou got it right!\n”;
39: print “It only took you $tries tries to do it!\n”;
40:
41: open(FILE, “win.txt”) or die “error opening win.txt: $!”;
42: my $stats = <FILE>;
43: close(FILE);
44:
45: $stats++;
46:
47: open(FILE, “> win.txt”) or die “error opening win.txt: $!”;
48: print FILE “$stats”;
49: close(FILE);
50: }
51: else
52: {
53: print “\nYou lose! You’re only allowed $allowed guesses :( \n”;
54: print “Answer was $answer\n”;
55:
56: open(FILE, “lose.txt”) or die “error opening lose.txt: $!”;
57: my $stats = <FILE>;
58: close(FILE);
59:
60: $stats++;
61:
62: open(FILE, “> lose.txt”) or die “error opening lose.txt: $!”;
63: print FILE “$stats”;
64: close(FILE);
65: }
66:
67: open(FILE, “win.txt”) or die “error opening win.txt: $!”;
68: my $wins = <FILE>;
69: close(FILE);
70:
71: open(FILE, “lose.txt”) or die “error opening lose.txt: $!”;
72: my $losses = <FILE>;
73: close(FILE);
74:
75: print “You won $wins and lost $losses”;

Line by line Explanation

line (1-4)
Setting up our script and turning on warnings and strict to help us debug in the off-chance something goes terribly wrong.
line (6-8)
The script we make requires a few variables to be set up. $guess is the guess that you or the user will type in to try to guess the number right. $tries is set at 0 as this variable will count how many “guesses” you took. $allowed is the max number of guesses you want the script to allow before the player loses.
line (10-12)
We print a question to the player on the max number the computer should create a random number for. Ie, if it’s 50 the computer will make a random number no greater than 50 that the player has to guess. You are storing the max number in $max and then chomping it.
line (13)
This is something new to you. We are creating a random integer (number) given the max number the player specified. If you don’t understand this, that’s fine. Just copy and paste the line for now.
line (15-16)
Our main while loop is initiated. This loop will continue to prompt for more guesses UNTIL the condition is no longer true. This means once the guess count equals the max allowed it’ll exit the loop.
line (17-19)
We are inside our loop now so we must ask the user for input and save it to our $guess variable. With all input from the command line, we must chomp() it for the data to be exactly what we need.
line (20)
While in the while() loop we need to auto increment the $tries variable we set to 0 earlier. This way we know exactly how many guesses they took and we can make the program hault when they guesses too many. Each time through the loop that they guess incorrectly, we add 1 to the $tries number.
line (22-33)
After each guess we have to use an if/elsif/else iteration to see whether or not the guess was right AND if it was wrong, check to see if the guess was too high or too low. If the number guessed was correct, we apply a last; to tell the script to exit the while() loop we created earlier because there’s no point to loop again if they have it right. If the answer is wrong, it’ll print to the screen whether the guess was too high or too low and will continue back to the top of the loop.
line (34)
All of our loop is done. They either guessed it right or they ran out of guesses so all we do here is close the bracket } on our while() loop we created earlier.
line (36-39)
We are out of our while loop and now we must check one last time whether they guessed the answer right or they ran out of guesses. If this if() is activated, they guessed it right and it prints out a little message saying they won!
line (41-49)
Because we are going to be storing our statistics to file (since we haven’t learned databases at this point), we need to open our win.txt file for reading and save the contents to our variable. After we get the count that we had saved, we need to auto increment our variable $stats++ because the player beat the game. We then close the filehandle and open the file again for writing. After we open the win.txt file the second time, all we do is print our new statistics to file and close the filehandle FILE.
line (51-65)
This is identical to the last section except this will be executed only if the player ran out of guessed and didn’t get the answer right. We print out a message saying they lost and then we have to open the lose.txt file for reading and then open it for writing. Just like we did for lines 41-49.
line (67-75)
The game is now over. The player either one or lost by this point. We are going to open both the win.txt and the lose.txt file and save the current number for each of them to file. We are doing this so we can print out how many times, in total, the player won or lost this GAN game you just made.

Complete script (without lines) for copying and pasting

#!/usr/bin/perl

use warnings;
use strict;

my $guess;
my $tries = 0;
my $allowed = 5;

print “what is the max number you want to try for? “;
my $max = <STDIN>;
chomp($max);
my $answer = int(rand($max))+1;

while ($tries < $allowed)
{
print “Your guess: “;
$guess = <STDIN>;
chomp($guess);
$tries++;

if ($guess eq $answer)
{
last;
}
elsif ($guess > $answer)
{
print “\t$guess is too high\n”;
}
elsif ($guess < $answer)
{
print “\t$guess is too low\n”;
}
}

if ($guess eq $answer)
{
print “\nYou got it right!\n”;
print “It only took you $tries tries to do it!\n”;

open(FILE, “win.txt”) or die “error opening win.txt: $!”;
my $stats = <FILE>;
close(FILE);

$stats++;

open(FILE, “> win.txt”) or die “error opening win.txt: $!”;
print FILE “$stats”;
close(FILE);
}
else
{
print “\nYou lose! You’re only allowed $allowed guesses :( \n”;
print “Answer was $answer\n”;

open(FILE, “lose.txt”) or die “error opening lose.txt: $!”;
my $stats = <FILE>;
close(FILE);

$stats++;

open(FILE, “> lose.txt”) or die “error opening lose.txt: $!”;
print FILE “$stats”;
close(FILE);
}

open(FILE, “win.txt”) or die “error opening win.txt: $!”;
my $wins = <FILE>;
close(FILE);

open(FILE, “lose.txt”) or die “error opening lose.txt: $!”;
my $losses = <FILE>;
close(FILE);

print “You won $wins and lost $losses”;

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: -1 (from 1 vote)