Spring Semester 2002


Lab Notes Seven: 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.

  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:

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:

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:

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!


Last updated on Feb 20, 2002, by Adrian German for A348/A548