Fall Semester 2002

Lab Notes Seven: Exam Problems (Computer Games)

In this lab you will implement a game.

The game works as follows:

1. When the game starts the computer chooses a secret number.

The number is chosen randomly between the values of 1 and 100 (inclusive).

2. The user then proceeds to guess the number.

3. Every time the user enters a number the computer provides an answer.

4. If the guess is smaller than the number, the computer reports it.

5. If the guess is bigger than the number, the computer reports it.

6. If the guess is equal to the number the computer reports that too.

7. Each guess is tallied into a variable, count.

8. If the user guesses the number before or with the 10th guess, the user wins.

9. If the count becomes 10 and the user still hasn't guessed the number, the user looses.

You are to provide three implementations:

• one in CGI with Perl

• one in PHP (without using sessions)

• one in PHP (using sessions)

Before you start programming, design your program.

Here's a flow chart to get you started.

Here (also) is an implementation to get an idea.

burrowww.cs.indiana.edu% cat number
#!/usr/bin/perl

print "Hi there, are you ready?\n";

\$x = 1 + int(rand(100)); # this is the number

print "I have chosen a number (between 1 and 100). \n";

\$count = 0;

print "You need to guess it in at most 10 tries.\n";

while (true) {

\$count += 1;
print "Guess", \$count, "> ";
\$guess = <STDIN>;

if (\$guess < \$x) { print "Try higher.\n"; }
elsif (\$guess > \$x) { print "Try lower.\n"; }
else { print "Congratulations!";
print "You guessed in \$count attempts.\n";
exit;
}

if (\$count == 10) {
print "You just ran out of attempts.\n";
print "You lost. Better luck next time. Good-bye!\n";
exit;
}

}
burrowww.cs.indiana.edu%
Notice though that this is a standalone program.

Here's a session (one game played) with the program:

burrowww.cs.indiana.edu% ./number
Hi there, are you ready?
I have chosen a number (between 1 and 100).
You need to guess it in at most 10 tries.
Guess1> 50
Try lower.
Guess2> 25
Try higher.
Guess3> 27
Try higher.
Guess4> 36
Try lower.
Guess5> 30
Try lower.
Guess6> 28
Try higher.
Guess7> 29
Congratulations!You guessed in 7 attempts.
burrowww.cs.indiana.edu%
Your lab assignment is to produce the same over the web.

You are to:

• write a CGI script
• a PHP script (without sessions) and
• another PHP script (that uses sessions)

that implements the behaviour described above.

This would make a good review (or help) for the exam next week.

To review PHP, use the following links:

The assignment for this week's lab is to have all that follows looked up and implemented and nicely indexed on your site. I will post a few more details on the web site until then.

So here are the solutions (in PHP, with and without sessions, and CGI).

I start with the sessions implementation.

I look over the basic standalone implementation of the game:

burrowww.cs.indiana.edu% cat number
#!/usr/bin/perl

print "Hi there, are you ready?\n";

\$x = 1 + int(rand(100)); # this is the number

print "I have chosen a number (between 1 and 100). \n";

\$count = 0;

print "You need to guess it in at most 10 tries.\n";

while (true) {

\$count += 1;
print "Guess", \$count, "> ";
\$guess = <STDIN>;

if (\$guess < \$x) { print "Try higher.\n"; }
elsif (\$guess > \$x) { print "Try lower.\n"; }
else { print "Congratulations!";
print "You guessed in \$count attempts.\n";
exit;
}

if (\$count == 10) {
print "You just ran out of attempts.\n";
print "You lost. Better luck next time. Good-bye!\n";
exit;
}

}
burrowww.cs.indiana.edu%
This is the starting point of my design.

Here's what I do in gameOne.php, step by step.

<? session_start();

?>

<html>
<head><title>Number Guessing</title></head>
<body bgcolor=white>

</body>
</html>
This gives us an ID, and (so far) no output.

Let's bring in the secret number (and show it).

<? session_start();

if (session_is_registered("secretNumber")) {
// there is a secret number variable in the session file
} else {
session_register("secretNumber");
\$secretNumber = rand(0, 100);
// we keep the secret number in the session variable
}

?>

<html>
<head><title>Number Guessing</title></head>
<body bgcolor=white>

Here's the secret number: <?=\$secretNumber?>

</body>
</html>
Now let's provide a user interface.

<? session_start();

if (session_is_registered("secretNumber")) {
// there is a secret number variable in the session file
} else {
session_register("secretNumber");
\$secretNumber = rand(0, 100);
// we keep the secret number in the session variable
}

?>

<html>
<head><title>Number Guessing</title></head>
<body bgcolor=white>

Here's the secret number: <?=\$secretNumber?>

<form>

Please type your number here: <input type="text" name="guess"> <p>

Push <input type="submit" value="Proceed"> to submit your guess. <p>

To start a new game please press

<input type="submit" name="reset" value="reset"> <p>

</form>

</body>
</html>

Now let's add the postage, you see.

<? session_start();

if (session_is_registered("secretNumber")) {
// there is a secret number variable in the session file
} else {
session_register("secretNumber");
\$secretNumber = rand(0, 100);
// we keep the secret number in the session variable
}

?>

<html>
<head><title>Number Guessing</title></head>
<body bgcolor=white>

Here's the secret number: <?=\$secretNumber?>

<form method="POST" action="<?=\$SCRIPT_NAME?>">

Please type your number here: <input type="text" name="guess"> <p>

Push <input type="submit" value="Proceed"> to submit your guess. <p>

To start a new game please press

<input type="submit" name="reset" value="reset"> <p>

</form>

</body>
</html>
Not much changed but now we got our loop.

Let's now play with it, accepting (and processing) input.

<? session_start();

if (session_is_registered("secretNumber")) {
// there is a secret number variable in the session file
} else {
session_register("secretNumber");
\$secretNumber = rand(0, 100);
// we keep the secret number in the session variable
}

if (\$guess > \$secretNumber) {
echo "\$guess is too high. Try lower.<p> ";
} else if (\$guess < \$secretNumber) {
echo "\$guess is too low. Try higher.<p>";
} else {
echo "Congratulations, you guessed it.<p> ";
}

?>

<html>
<head><title>Number Guessing</title></head>
<body bgcolor=white>

The secret number is: <?=\$secretNumber?>

<form method="POST" action="<?=\$SCRIPT_NAME?>">

Please type your number here: <input type="text" name="guess"> <p>

Push <input type="submit" value="Proceed"> to submit your guess. <p>

To start a new game please press <input type="submit" name="reset" value="reset"> <p>

</form>

</body>
</html>
Note there's no time limit.

Let's keep track of how many times the user is guessing.

<? session_start();

if (session_is_registered("secretNumber")) { }
else {
session_register("secretNumber");
\$secretNumber = rand(0, 100);
}

if (\$guess > \$secretNumber) {
echo "\$guess is too high. Try lower.<p> ";
} else if (\$guess < \$secretNumber) {
echo "\$guess is too low. Try higher.<p>";
} else {
echo "Congratulations, you guessed it.<p> ";
}

if (session_is_registered("numberOfAttempts")) {
\$numberOfAttempts += 1;
} else {
session_register("numberOfAttempts");
\$numberOfAttempts = 1;
}

echo "This attempt has number \$numberOfAttempts for you. <p>";

if (\$numberOfAttempts >= 10) {
echo "Sorry, you lost. New game started. <p>";
\$numberOfAttempts = 0;
\$secretNumber = rand(0, 100);
}

?>

<html>
<head><title>Number Guessing</title></head>
<body bgcolor=white>

The secret number is: <?=\$secretNumber?>

<form method="POST" action="<?=\$SCRIPT_NAME?>">

Please type your number here: <input type="text" name="guess"> <p>

Push <input type="submit" value="Proceed"> to submit your guess. <p>

To start a new game please press

<input type="reset" name="reset" value="reset"> <p>

</form>

</body>
</html>
Now it's easy to reset the game.

<? session_start();

if (\$reset) {
echo "Sorry, you lost. New game started. <p>";
\$numberOfAttempts = 0;
\$secretNumber = rand(0, 100);

} else {

if (session_is_registered("secretNumber")) { }
else {
session_register("secretNumber");
\$secretNumber = rand(0, 100);
}

if (\$guess > \$secretNumber) {
echo "\$guess is too high. Try lower.<p> ";
} else if (\$guess < \$secretNumber) {
echo "\$guess is too low. Try higher.<p>";
} else {
echo "Congratulations, you guessed it.<p> ";
}

if (session_is_registered("numberOfAttempts")) {
\$numberOfAttempts += 1;
} else {
session_register("numberOfAttempts");
\$numberOfAttempts = 1;
}

echo "This attempt has number \$numberOfAttempts for you. <p>";

if (\$numberOfAttempts >= 10) {
echo "Sorry, you lost. New game started. <p>";
\$numberOfAttempts = 0;
\$secretNumber = rand(0, 100);
}
}

?>

<html>
<head><title>Number Guessing</title></head>
<body bgcolor=white>

The secret number is: <?=\$secretNumber?>

<form method="POST" action="<?=\$SCRIPT_NAME?>">

Please type your number here: <input type="text" name="guess"> <p>

Push <input type="submit" value="Proceed"> to submit your guess. <p>

To start a new game please press

<input type="submit" name="reset" value="reset"> <p>

</form>

</body>
</html>
Last thing is to make sure that if we don't receive a guess we don't do anything.

<? session_start();

if (\$reset) {
echo "Sorry, you lost. New game started. <p>";
\$numberOfAttempts = 0;
\$secretNumber = rand(0, 100);

} else {

if (session_is_registered("secretNumber")) { }
else {
session_register("secretNumber");
\$secretNumber = rand(0, 100);
}

if (\$guess) {

if (\$guess > \$secretNumber) {
echo "\$guess is too high. Try lower.<p> ";
} else if (\$guess < \$secretNumber) {
echo "\$guess is too low. Try higher.<p>";
} else {
echo "Congratulations, you guessed it.<p> ";
}

if (session_is_registered("numberOfAttempts")) {
\$numberOfAttempts += 1;
} else {
session_register("numberOfAttempts");
\$numberOfAttempts = 1;
}

} else { print "Please enter a guess. <p>"; }

echo "This attempt has number \$numberOfAttempts for you. <p>";

if (\$numberOfAttempts >= 10) {
echo "Sorry, you lost. New game started. <p>";
\$numberOfAttempts = 0;
\$secretNumber = rand(0, 100);
}

}

?>

<html>
<head><title>Number Guessing</title></head>
<body bgcolor=white>

The secret number is: <?=\$secretNumber?>

<form method="POST" action="<?=\$SCRIPT_NAME?>">

Please type your number here: <input type="text" name="guess"> <p>

Push <input type="submit" value="Proceed"> to submit your guess. <p>

To start a new game please press

<input type="submit" name="reset" value="Reset"> <p>

</form>

</body>
</html>
If you implement this with hidden fields we can do the following:

<?
if (\$reset) {
echo "Sorry, you lost. New game started. <p>";
\$numberOfAttempts = 0;
\$secretNumber = rand(0, 100);

} else {

if (\$secretNumber) { }
else {
\$secretNumber = rand(0, 100);
}

if (\$guess) {

if (\$guess > \$secretNumber) {
echo "\$guess is too high. Try lower.<p> ";
} else if (\$guess < \$secretNumber) {
echo "\$guess is too low. Try higher.<p>";
} else {
echo "Congratulations, you guessed it.<p> ";
}

if (\$numberOfAttempts) {
\$numberOfAttempts += 1;
} else {
\$numberOfAttempts = 1;
}

} else { print "Please enter a guess. <p> "; }

\$numberOfAttempts += 0; // to make the empty string a number

echo "This attempt has number \$numberOfAttempts for you. <p>";

if (\$numberOfAttempts >= 10) {
echo "Sorry, you lost. New game started. <p>";
\$numberOfAttempts = 0;
\$secretNumber = rand(0, 100);
}
}

?>

<html>
<head><title>Number Guessing</title></head>
<body bgcolor=white>

The secret number is: <?=\$secretNumber?>

<form method="POST" action="<?=\$SCRIPT_NAME?>">

Please type your number here: <input type="text" name="guess"> <p>

Push <input type="submit" value="Proceed"> to submit your guess. <p>

To start a new game please press

<input type="submit" name="reset" value="Reset"> <p>

<input type="hidden" name="numberOfAttempts" value="<?=\$numberOfAttempts?>">
<input type="hidden" name="secretNumber" value="<?=\$secretNumber?>">

</form>

</body>
</html>
Taking a close look would make you realize the changes are minor.

Translating this in Perl using CGI is immediate.

#!/usr/bin/perl

use CGI; \$q = new CGI; \$reset = \$q->param('reset');
\$guess = \$q->param('guess');
\$secretNumber = \$q->param('secretNumber');
\$numberOfAttempts = \$q->param('numberOfAttempts');
\$SCRIPT_NAME = \$ENV{"SCRIPT_NAME"};

print qq{Content-type: text/html\n\n<html>
<head><title>Number Guessing</title></head>
<body bgcolor=white>

};

if (\$reset) {
print "Sorry, you lost. New game started. <p>";
\$numberOfAttempts = 0;
\$secretNumber = int(rand(100));

} else {

if (\$secretNumber) { }
else {
\$secretNumber = int(rand(100));
}

if (\$guess) {

if (\$guess > \$secretNumber) {
print "\$guess is too high. Try lower.<p> ";
} elsif (\$guess < \$secretNumber) {
print "\$guess is too low. Try higher.<p>";
} else {
print "Congratulations, you guessed it.<p> ";
}

if (\$numberOfAttempts) {
\$numberOfAttempts += 1;
} else {
\$numberOfAttempts = 1;
}

} else { print "Please enter a guess. <p> "; }

\$numberOfAttempts += 0; # to make the empty string a number

print "This attempt has number \$numberOfAttempts for you. <p>";

if (\$numberOfAttempts >= 10) {
print "Sorry, you lost. New game started. <p>";
\$numberOfAttempts = 0;
\$secretNumber = int(rand(100));
}
}

print qq{

The secret number is: \$secretNumber

<form method="POST" action="\$SCRIPT_NAME">

Please type your number here: <input type="text" name="guess"> <p>

Push <input type="submit" value="Proceed"> to submit your guess. <p>

To start a new game please press

<input type="submit" name="reset" value="Reset"> <p>

<input type="hidden" name="numberOfAttempts" value="\$numberOfAttempts">
<input type="hidden" name="secretNumber" value="\$secretNumber">

</form>

</body>
</html>

};
With this last example we also see that PHP was a bit too forgiving, earlier.

( Do you see why I claim that?)

But that's OK, as long as you are aware of the changes.

This is the end of the lab.

The exam is indeed just like this problem, only a bit different.

A348/A548 LAB ASSIGNMENT SEVEN

As an UNDERGRADUATE

Three more tasks:

1. Implement all three programs.
2. Index them clearly on your web site.
3. Answer this question: why is it said above that PHP was too forgiving?

As a GRADUATE add the following:

One more task:

1. Implement as much as you can of the Hangman game using PHP.

Last updated: Oct 13, 2002 by Adrian German for A348/A548