Game03: Multiple Levels - Exercise

Contact Us or call 1-877-932-8228
Game03: Multiple Levels - Exercise

Game03: Multiple Levels

Duration: 15 to 30 minutes.

What if we want to offer gamers multiple levels of difficulty in our game? We could make the random number multiplier a property of the Game class, and set a value into it with a constructor, after asking the user what level they'd like to play.

  1. Add a range field to the Game class (an int).
  2. Add a constuctor that accepts a level parameter; use a char.
  3. Within that constructor, use a switch to process the incoming level:
    • Uppercase or lowercase B means Beginner, set the range to 10.
    • I means Intermediate; set the range to 100.
    • A means Advanced; set the range to 1000.
    • Any other value results a Beginner game; after all, if they can't answer a simple question correctly, how could we expect them to handle something more advanced.
    • You could put the default option stacked above the "B" cases, so that you can print an error message and then fall through to the 'B' logic
  4. Use range as the parameter when you call the Random object's nextInt method (move the logic that creates the Random object and generates the answer to this constructor).
  5. Add a default constructor that calls this new constructor, passing 'I' for intermediate.
  6. The prompt given by the play method should now take the range into account.
  7. In the main method, ask the user for the level and call the new constructor with their response.


import util.*;
import java.util.*;

public class Game {
	private Random r = new Random();
	private int answer;
	private int range = 10;
	public Game() {
	public Game(char level) {
		switch (level) {
				System.out.println("Invalid option: " +
						level + ", using Beginner");
			case 'b':
			case 'B':
				range = 10;	
			case 'i':
			case 'I': range = 100;
			case 'a':
			case 'A': range = 1000;
		Random r = new Random();
		answer = r.nextInt(range) + 1;
	public void play() {
		int guess;
		guess = 
			KeyboardReader.getPromptedInt("Enter a number 1 - " + range + ": ");
		if (guess < answer) System.out.println("Too low");
		else if (guess > answer) System.out.println("Too high");
		else System.out.println("Correct!");
	public static void main(String[] args) {
		char level = KeyboardReader.getPromptedChar("What level (B, I, A)? ");
		new Game(level).play();		

The switch tests for the three letters, stacking cases for uppercase and lowercase values. The default catches all other responses and falls through to the Beginner logic.