1. Making Decisions (Guessing Game)
- if statements
- combining conditions with AND && , OR ||
- comparisons (!= , ==)
Adding maximum allowed trials (the AND, i.e. &&, operator)
We extended the game by adding a maximum number of allowed trials. This required
a few modifications:
(1) communicating the maximum trials to the game:
- done via input/parameter maxTrials to the game procedure
(2) keeping track of the number of guess made by the user:
- create a counter trials at the point of obtaining the first guess
(set to 1, since one attempt has just been made)
- increment the counter every time the player is asked for input
(3) incorporating the counter to stop the game if trials limit reached:
- update the loop condition
(4) modifying the final message:
- if player has guessed correctly:
show congratulatory message
otherwise:
encourage player to play again
void playGuessingGame( int maxTrials ) // (1)
{
double hintX = canvas.getWidth() / 2;
double hintY = canvas.getHeight() / 2;
int secret = 10;
int guess = canvas.readInt( "Guess a number" );
int trials = 1; // (2.1)
while( guess != secret && trials < maxTrials ) // (3)
{
canvas.clear();
canvas.drawText( hintX, hintY, "Incorrect guess.", "red" );
canvas.sleep( 1 );
guess = canvas.readInt( "Guess a number" );
trials = trials + 1; // (2.2)
}
canvas.clear();
if (guess == secret) // (4)
{
canvas.drawText( hintX, hintY, "Congratulations, you win!!!", "blue" );
}
else
{
canvas.drawText( hintX, hintY "Sorry, play again!!!", "blue" );
}
}
Stopping condition and Game Over messages
Imposing a limit on the number of trials requires that we modify the stopping
condition of the while loop. Here is how it could be expressed in English
along with its Java equivalent:
as long as guess is different from secret AND there are trials left:
---------- ------------------------------ --- ---------------------
while ( guess != secret && trials < maxTrials )
Another implication of having a limit on the trials is that the player may lose
the game. This means that we can no longer display a congratulatory message --
we need to decide whether the player won or lost:
In English In Java
if guess is correct: if (guess == secret)
{
print "You win" canvas.drawText( hintX, hintY, "Congratulations, you win!!!" );
}
otherwise: else
{
print "You lose" canvas.drawText( hintX, hintY, "Sorry, play again!!!" );
}
Adding a range (the OR, i.e. ||, operator)
Next we added a range to limit the possible choices for the guess. This required
two modifications:
(1) communicating the range to the game:
- done via inputs/parameters low, high to the game procedure
(2) choosing a new secret number that falls in the given range:
- for now using the average of the two limits of the range
(3) communicating an error message to the user (incorrect guess vs out of range):
- done by adding a hint; here is how we might express the decision whether
the guess is invalid, i.e. outside of the range in English and java:
In English In Java
if guess is under low OR guess is above high: if (guess < low || guess > high)
{
print "out of range" canvas.drawText( hintX, hintY, "Guess is out of range!!!" );
}
otherwise: else
{
print "incorrect guess" canvas.drawText( hintX, hintY, "Incorrect guess!!!" );
}
Here is the new code:
void playGuessingGame( int low, int high, int maxTrials ) // (1)
{
double hintX = canvas.getWidth() / 2;
double hintY = canvas.getHeight() / 2;
int secret = ( low + high ) / 2 // (2)
// int secret = canvas.getRandomInt( low, high );
int guess = canvas.readInt( "Guess a number" );
int trials = 1;
while( guess != secret && trials < maxTrials )
{
canvas.clear();
if (guess < low || guess > high) // (3)
{
canvas.drawText(hintX, hintY, "Guess is out of range!!!");
}
else
{
canvas.drawText(hintX, hintY, "Incorrect guess!!!");
}
canvas.sleep( 1 );
guess = canvas.readInt( "Guess a number" );
trials = trials + 1;
}
canvas.clear();
if (guess == secret)
{
canvas.drawText( hintX, hintY, "Congratulations, you win!!!", "blue" );
}
else
{
canvas.drawText( hintX, hintY "Sorry, play again!!!", "blue" );
}
}