2

I know my documentation is not superb.

This program is to create a Rock, Paper, Scissors Game:

I initially struggled with getting the case statements withing case statements to work properly but I feel like I resolved the issue.

My question: is this an acceptable implementation of nesting a switch statement within a switch statement. Otherwise, I could put an if statement within the userChoice Switch statement instead.

public static void main(String[] args) {

    // Variable declarations
    Scanner keyboard = new Scanner(System.in);
    Random rand = new Random();
    int sentinel = 4;
    int userChoice;
    int computerChoice;

    // Display Rock, Paper, Scissors menu
    menu();

    // Obtain user's choice
    userChoice = validOption("\nPlease make a selection: ", 1, 4);

    // Display game results
    while (userChoice != sentinel) {
        switch (userChoice) {
            case 1:
                // Generate computer's choice
                computerChoice = rand.nextInt(3) + 1; 
                System.out.println("Computer's choice: " + computerChoice);

                // Determine outcome of the round
                switch (computerChoice) {
                    case 1:
                        System.out.println("Rock cannot defeat Rock. Draw.");
                        break;
                    case 2:
                        System.out.println("Paper covers Rock. Computer wins.");
                        break;
                    case 3:
                        System.out.println("Rock smashes Scissors. You win!");
                        break;
                }
                // Display menu selection and obtain user choice
                menu();
                userChoice = validOption("\nPlease make a selection: ", 1, 4);
                break;

            case 2: 
                // Generate computer's choice
                computerChoice = rand.nextInt(3) + 1; 
                System.out.println("Computer's choice: " + computerChoice);

                // Determine outcome of the round
                switch (computerChoice) {
                    case 1:
                        System.out.println("Paper covers Rock. You win!");
                        break;
                    case 2:
                        System.out.println("Paper cannot defeat Paper. Draw.");
                        break;     
                    case 3:
                        System.out.println("Scissors cut Paper. Computer wins.");
                        break;
                }
                //Display menu selection and obtain user choice
                menu();
                userChoice = validOption("\nPlease make a selection: ", 1, 4);
                break;

            case 3:
                // Generate computer's choice
                computerChoice = rand.nextInt(3) + 1; 
                System.out.println("Computer's choice: " + computerChoice);
                // Determine outcome of the round
                switch (computerChoice) {
                    case 1:
                        System.out.println("Rock smashes Scissors. Computer wins.");
                        break;
                    case 2:
                        System.out.println("Scissors cut Paper. You win!");
                        break;
                    case 3:
                        System.out.println("Scissors cannot defeat Scissors. Draw.");
                        break;
                }
                // Display menu selection and obtain user choice
                menu();
                userChoice = validOption("\nPlease make a selection: ", 1, 4);
                break;
        }
    }     
    System.out.println("Game Over.");
}

// Create menu method
public static void menu () {
    System.out.println("\n1 = Rock");
    System.out.println("2 = Paper");
    System.out.println("3 = Scissors");
    System.out.println("4 = End Game\n");
}

/**
 * Protects option input from incorrect value (non-numeric, too high or too low)
 * @param prompt
 * @param minValue
 * @param maxValue
 * @return 
 */
public static int validOption (String prompt,
                                int minValue,
                                int maxValue) {
    Scanner keyboard = new Scanner (System.in);
    int value;
    String errorMessage = "Incorrect value. Please select options "
            + "1, 2, 3 or 4\n";
    do {
        System.out.print(prompt);
        if (keyboard.hasNextInt()) {
            value = keyboard.nextInt();
            if (value < minValue || value > maxValue) {
                System.out.println(errorMessage);
            } else {
                break; // Exit loop.
            }
        } else {
            System.out.println(errorMessage);
        }
        keyboard.nextLine(); // Clears buffer.
    } while (true);
    return value;
}

}

Rye Guy
  • 45
  • 2
  • Of course it will work, but this makes code hard to read and understand. I cannot say it is a good practice to do like that. Instead try extract some stuff to separate methods at least. – ikos23 Apr 07 '18 at 18:37

1 Answers1

2

IMHO, as long as it is readable, writing switch statements inside switch statements is fine per se. To make the code more readable, you can use constants instead of 1, 2, 3 to refer to the choices:

final int ROCK = 1;
final int PAPER = 1;
final it SCISSORS  = 1;

Your switches can look like this:

switch (computerChoice) {
    case ROCK:
        System.out.println("Rock cannot defeat Rock. Draw.");
        break;
    case PAPER:
        System.out.println("Paper covers Rock. Computer wins.");
        break;
    case SCISSORS:
        System.out.println("Rock smashes Scissors. You win!");
        break;
}

Another thing you could improve is to move these two parts out of the outer switch:

// Generate computer's choice
computerChoice = rand.nextInt(3) + 1; 
System.out.println("Computer's choice: " + computerChoice);

// and

menu();
userChoice = validOption("\nPlease make a selection: ", 1, 4);

since you are doing this in every case.

You can also extract the inner switch statements into methods like this:

private static handlePlayerRock() {
    // add the inner switch for the case ROCK of the outer switch here
}

private static handlePlayerScissors() {
    // add the inner switch for the case SCISSORS of the outer switch here
}

private static handlePlayerPaper() {
    // add the inner switch for the case PAPER of the outer switch here
}

Your switch will then look like this:

switch (userChoice) {
    case ROCK: handlePlayerRock();
    case PAPER: handlePlayerPaper();
    case SCISSORS: handlePlayerScissors();
}

There are more advanced ways to use for this problem other than nested switches. You can put all the possibilities into a HashMap<Combination, String>, where Combination is a class that represents the player and the computer's choices. The map will contain things like:

(ROCK, ROCK) -> "Rock cannot defeat Rock. Draw."
(ROCK, PAPER) -> "Paper covers Rock. Computer wins."
...

And you can just use both the computer's and the player's choice to access the value associated with the key.

Sweeper
  • 213,210
  • 22
  • 193
  • 313