Code doesn't seem to continue after a certain line

60 Views Asked by At

I've been trying to code a text-based video game and have found there to be a roadblock once the character gets to a certain part of the game.

This is the code for the came.

while (true) {
                input = userInput.nextLine().toUpperCase();
                switch (input) {
                case "R":
                    System.out.println("You encounter an old man who greets you \n"
                            + "[OLD MAN] 'Hello there my boy! I've been expecting you for a long time! Where have you been?' \n"
                            + "How do you respond? \n"
                            + "\n"
                            + "[P] (Pretend to know the old man) Hello, my old friend! Long time no see, how are you? \n"
                            + "[C] (Curious I'm sorry, you must have me mistaken for someone else. I don't know you? \n"
                            + "[S] (Stay silent) \n"
                            + "\n"
                            + "[PLEASE ENTER EITHER [P], [C] OR [S]");
                    while (true) {
                        input = userInput.nextLine().toUpperCase();
                        switch (input) {
                        case "P":
                            System.out.println("[OLD MAN] 'I'm alive, I found that stick you were talking about. It was very hard to find. Had to pay an arm and a leg, literally.'"
                                    + "\nThe old man looks puzzled, as if you'd forgotten something"
                                    + "\n[OLD MAN] 'Do you not have what I asked for? \n'"
                                    + "\n"
                                    + "[P] (Pretending) I did, but I forgot it at home!\n"
                                    + "[C] (Confused) I don't have it, sorry! I don't remember what you wanted.\n"
                                    + "[S] (Stay silent) \n");
                            while (true) {
                                input = userInput.nextLine().toUpperCase();
                                switch (input) {
                                case "P":
                                case "C":
                                case "S":
                                    System.out.println("[OLD MAN] 'Oh, I understand. Well, hopefully you remember our deal; you have to guess the number I'm thinking of now. It's between 1 and 3.'");
                                    // Play the random number guessing game
                                    playRandomNumberGuesser(1, 3, userInput);
                                    break;
                                default:
                                    System.out.println("[INVALID INPUT, PLEASE INPUT P, C, OR S");
                                    continue;
                                }
                                break;
                            }
                            break;

                        case "C":
                            System.out.println("[OLD MAN] 'What do you mean? We've known each other for years, do you not remember me?' \n"
                                    + "[PLEASE ENTER EITHER YES (Y) OR NO (N)");
                            while (true) {
                                input = userInput.nextLine().toUpperCase();
                                switch (input) {
                                case "Y":
                                    System.out.println("Oh, I see... you play some very weird games, my friend. Hopefully, you remember the game I had for you though. You have to guess the number I'm thinking of right now. It's between 1 and 3, you get one try.");
                                    // Play the random number guessing game
                                    playRandomNumberGuesser(1, 3, userInput);
                                    break;
                                case "N":
                                    System.out.println("I get the feeling you aren't who you say you are. In order to validate your identity, I'm going to play a game with you. I'm thinking of a number between 1 and 3, what is it.");
                                    // Play the random number guessing game
                                    playRandomNumberGuesser(1, 3, userInput);
                                    break;
                                default:
                                    System.out.println("[INVALID INPUT, PLEASE INPUT Y, N]");
                                    continue;
                                }
                                break;
                            }
                            break;

                        case "S":
                            System.out.println("[OLD MAN] 'Ahhh not the talking type today, I see.'\n"
                                    + "[OLD MAN] If you do recall, I do have a game you have to play. It's a simple number guessing game. You guess a number between 1 and 3.");
                            // Play the random number guessing game
                            playRandomNumberGuesser(1, 3, userInput);
                            break;
                        default:
                            System.out.println("[INVALID INPUT, PLEASE ENTER Y OR N]");
                            continue;
                        }
                        break;
                    }
                    break;

                case "L":
                    System.out.println("You continue onto a path that leads to a tree \n"
                            + "The tree asks you to play a random number game where you pick a number between 1-3. \n");
                    playRandomNumberGuesser(1, 3, userInput);
                    break;
                    
                default:
                    System.out.println("[INVALID INPUT, PLEASE ENTER EITHER [R] OR [L]]");
                    continue;
                }
                break;
            }
// Mountainous area

            System.out.println("You continue on your journey and encounter a path that diverts left or right.");
            System.out.println("Which way do you go? [PLEASE ENTER R FOR RIGHT OR L FOR LEFT]");

            while (true) {
                input = userInput.nextLine().toUpperCase();
                switch (input) {
                    case "L":
                        System.out.println("You walk up a path, where the wind seems to get louder and louder and the air gets thinner and thinner.");
                        System.out.println("You suddenly hear a bark from a bush to your right. You open the bush to reveal a small puppy, it looks scared. Do you take it?");
                        System.out.println("[PLEASE INPUT EITHER YES (Y) OR NO (N)]");
                        
                        while (true) {
                            input = userInput.nextLine().toUpperCase();
                            switch (input) {
                                case "Y":
                                    System.out.println("You pick up the dog and fit him into your jacket, keeping him around for later");
                                    inventory.add("DOG");
                                    System.out.println("INVENTORY - " + inventory);
                                    System.out.println("You turn around and walk up the other part of the path into a forest");
                                    break;
                                case "N":
                                    System.out.println("You leave the dog alone.");
                                    System.out.println("You turn around and walk up the other part of the path into a forest");
                                    break;
                                default:
                                    System.out.println("[INVALID INPUT] Without enough time to react, the dog runs off.");
                                    System.out.println("You turn around and walk up the other part of the path into a forest");
                                    break;
                            }
                            break;
                        }

                    case "R":
                        System.out.println("You walk into a beautiful forest, almost mystical.");
                        System.out.println("There's a strong, quick gust of wind leading you to your right, almost pushing you towards it.");
                        System.out.println("Do you follow it?");
                        System.out.println("[PLEASE INPUT EITHER YES [Y] OR NO [N]");

                        // Prompt for user input
                        break;

                    default:
                        System.out.println("[INVALID INPUT, PLEASE ENTER [L] OR [R]");
                        break;
                }
                break;
            }
        }
    }

The Random number gen game -

    public static void playRandomNumberGuesser(int min, int max, Scanner userInput) {
        Random random = new Random();
        int randomNumber = random.nextInt((max - min) + 1) + min;
        int guess;
        boolean guessedCorrectly = false;

        do {
            System.out.print("Enter your guess (" + min + "-" + max + "): ");
            guess = userInput.nextInt();

            if (guess < min || guess > max) {
                System.out.println("Please enter a number between " + min + " and " + max + ".");
            } else if (guess < randomNumber) {
                System.out.println("Too low. Try again.");
            } else if (guess > randomNumber) {
                System.out.println("Too high. Try again.");
            } else if (guess == randomNumber) {
                System.out.println("[OLD MAN] 'Well done!! you've done well'\n"
                        + "[OLD MAN] 'You really do have a knack for these games. I'll tell you that. Here, take this for your journey.'\n");
                guessedCorrectly = true;
            }
        } while (!guessedCorrectly);
    }

The output that's coming out is not desired.

Output -

An old man appears from out of the tree laughing INVENTORY: [STICK] You are bestowed a stick for your great deed. You continue on your journey and encounter a path that diverts left or right. Which way do you go? [PLEASE ENTER R FOR RIGHT OR L FOR LEFT] [INVALID INPUT, PLEASE ENTER [L] OR [R]

Desired output -

An old man appears from out of the tree laughing INVENTORY: [STICK] You are bestowed a stick for your great deed. You continue on your journey and encounter a path that diverts left or right. Which way do you go? [PLEASE ENTER R FOR RIGHT OR L FOR LEFT] (Prompt the user to enter a scanner to continue into the mountainous area.)

I tried making it so that at the end of the first while loop, it prompted the user to do the game and then progress further, but that just results in errors and doesn't really do what I want it to.

I've also tried frankensteining the code into itself, disregarding the class and just putting it into the code but it isn't doing it. It still displays issues.

Thanks guys.

0

There are 0 best solutions below