Number Guessing Game

Modules - Structured Programming 2, Procedural Programming 1

 

Teacher’s Notes:

========================================================================

There are a lot of ways to make this assignment easier or harder.  Be creative and add or remove any parts you may like!

As a challenge, you can also get students to write a cheating algorithm that runs occasionally.  I have found that cheating the player more than 20% of the time makes it very obvious so I tell the players that the code should only run (at most) 20% of the time.  It is up to them how they do this but it must not be detectable. 

You may notice that I have a swap algorithm in each of the errorTrap and rng methods.  This is to check if the min is greater than the max.  If this is the case, the method will swap the values so that they are in the correct order.  This makes it so future uses of these methods don't require the programmer to know the order of the arguments.

 

Assignment:

========================================================================

 

In this project, you will write a program that implements a number guessing game where the computer generates a random number and gives the player three tries to guess the number.  You should implement this in the following order:

1. Use a method to generate a random number.  You should output this to test it and leave this output in place until your program is complete.  This will help with testing and debugging later.  The method header for this should be as follows:

public static int rng(int min, int max)

 

2. Give the player one try to guess the number.  Tell them if they got it right or wrong.

3. Revise the program to give the player 3 tries to guess the number.  If they get it right, they don't guess again - even if they have tries.

4. Add in an error trapping method to make sure that the input is always between a given min to a given max and is of the correct type.  These should be implemented with variables because the max value will be changeable later.  The header for this should be as follows:

public static int errorTrap(int min, int max)

 

5. Add in a method that gives the player a hint telling them that they are hot, warm, or cold.  The player's guess is hot if they are 1 away from the answer.  They are warm if their guess is 2 away from the answer.  If the guess is neither hot nor warm, then it is cold.  Alternately, you can make it so that guesses are hot if they are within 10% of the maximum answer value and warm if they are within 20% of the maximum answer value.  The method header for this should be as follows:

public static void hwcResponse(int guess, int answer, int max)

or

public static void hwcResponse(int guess, int answer)

 

6. Ask the player if they want to play again.  If they choose to play again, the program must generate a new number and also reset their tries.

7. Add a scoreboard to the program and update the code to track the wins and losses.

8. Add a level selecting system to the code so that the player can choose between easy (1-10), medium (1-20), and hard (1-30) with 3,4, and 5 guesses respectively.

 

 

Solution (JAVA):

========================================================================

 
package code;
import java.util.Scanner;
public class GuessingGame {
    public static int rng(int min, int max) {
        if(min > max) {
            int temp = min;
            min = max;
            max = temp;
        }
        
        return (int)(Math.random()*(max-min+1)+min);
    }
    
    public static int errorTrap(int min, int max)throws Exception {
        if(min > max) {
            int temp = min;
            min = max;
            max = temp;
        }
        
        Scanner input = new Scanner(System.in);
        int data = min -1;
        do {
            try {
                
                System.out.println("Enter a number from " + min + " to " + max +": ");
                data = input.nextInt();
            }
            catch(Exception e) {
                input.next();
            }
        }while(data < min || data > max);
        return data;
    }
    
    public static void hwcResponse(int guess, int answer, int max) {
        int difference = answer - guess;
        if(difference < 0)
            difference *= -1;
        
        if(difference <= 0.1 * max)
            System.out.println("...but you are hot.");
        else if(difference <= 0.2 * max)
            System.out.println("...but you are warm.");
        else
            System.out.println("...but you are cold.");
    }
    
    public static void main(String[] args)throws Exception {
        
        int answer = 0, guess = 0, tries = 3, min = 1, max = 10;
        int wins = 0, losses = 0, playAgain = 1, level = 1;
        
        do {
            System.out.println("What level would you like to play at (1=easy, 2=medium, 3=hard): ");
            level = errorTrap(1,3);
            if(level == 1) {
                max = 10;
                tries = 3;
            }
            else if (level == 2) {
                max = 20;
                tries = 4;
            }
            else {
                max = 30;
                tries = 5;
            }
            
            
            answer = rng(min,max);
            System.out.println(answer); //comment this out when done
            System.out.println("==============Number Guessing Game==============");
            System.out.println("\tWins: " + wins + "\t\tLosses: " + losses);
            System.out.println("================================================");
            
            do {
                guess = errorTrap(min,max);
                
                tries--;
                if(guess == answer) {
                    System.out.println("You got it!");
                    wins++;
                }
                else {
                    System.out.println("That's not it...");
                    if(tries != 0)
                        hwcResponse(guess, answer, max);
                    else {
                        System.out.println("You lose.");
                        losses++;
                    }
                        
                }
            }while(tries > 0 && guess != answer);
            System.out.println("Would you like to play again (1=yes, 0=no)? ");
            playAgain = errorTrap(0,1);
        }while(playAgain == 1);
        
    }
}