1. Making Decisions (Guessing Game)


Guessing Game

The guessing game and its variations will be used to illustrate a number of new
concepts. The first (basic) version will serve to illustrate how to read user 
input from the keyboard and will provide another example of using while loops.

Goal: Write a procedure that allows the user to play the guessing game. 
The game should pick a random number and repeatedly ask the player to guess a 
number as long as the guess is different from the secret number.
Here is an outline of the game process:
1. Pick a secret number

2. Prompt player for a guess

3. As long as the guess is incorrect (i.e. guess is different from secret):

      4. Inform player that guess is incorrect 

      5. Prompt player for new guess

6. Show a congratulatory message
The Java translation of the English description is:
void playGuessingGame()
{
     double hintX = canvas.getWidth() / 2;    // where to draw messages
     double hintY = canvas.getHeight() / 2;

     int secret = 10;                                  // (step 1)    (not truly random yet)

     int guess = canvas.readInt( "Guess a number" );   // (step 2)

     while( guess != secret )                          // (step 3)
     {
          canvas.clear();

          canvas.drawText( hintX, hintY, "Incorrect guess.", "red" );  // (step 4)

          canvas.sleep( 1 );    // to see message

          guess = canvas.readInt( "Guess a number" );                  // (step 5)
     }

     canvas.clear();
     canvas.drawText( hintX, hintY, "Congratulations!", "blue" );      // (step 6)
}