|
Spring Semester 2002 |
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.
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 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 will make a good review for the upcoming exam.
To review PHP, use the following links:
Perhaps a bit of help will be posted here (or somewhere) a bit later.
Good luck and let me know if we can be of any help!
A348/A548