![]() |
![]() Spring Semester 2003 |
In this lab you will implement a game.
The game works as follows:
The number is chosen randomly between the values of 1 and 100 (inclusive).
count
.
count
becomes 10 and the user still hasn't guessed
the number, the user looses.
You are to provide three implementations:
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.
Notice though that this is a standalone program.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%
Here's a session (one game played) with the program:
Your lab assignment is to produce the same over the web.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%
You are to:
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:
This is the starting point of my design.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%
Here's what I do in gameOne.php
, step by step.
This gives us an ID, and (so far) no output.<? session_start(); ?> <html> <head><title>Number Guessing</title></head> <body bgcolor=white> </body> </html>
Let's bring in the secret number (and show it).
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?> </body> </html>
<? 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.
Not much changed but now we got our loop.<? 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>
Let's now play with it, accepting (and processing) input.
Note there's no time limit.<? 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>
Let's keep track of how many times the user is guessing.
Now it's easy to reset the game.<? 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>
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 > $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>
If you implement this with hidden fields we can do the following:<? 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>
Taking a close look would make you realize the changes are minor.<? 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>
Translating this in Perl using CGI is immediate.
With this last example we also see that PHP was a bit too forgiving, earlier.#!/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> };
( 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
Three more tasks:
One more task: