0

I made a program that plays a game with 2 users. They choose a starting number, and each turn, they can take 1, 2, or 3 numbers. Whoever reaches 0 first, loses. My code might make the game a bit more clearer:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int rock = 0, player1 = 0, player2 = 0, menu = 0, win1 = 0, win2 = 0, turns1 = 0, turns2 = 0, rocks1 = 0, rocks2 = 0;
        double sqrt = 0;
        String name1 = "", name2 = "";
        boolean bool = true;

        // Prompts users with names
        System.out.println("Welcome to the game, Get Rocked!");
        System.out.println("What is Player 1's name?");
        name1 = s.nextLine();
        System.out.println("What is Player 2's name?");
        name2 = s.nextLine();
        System.out.printf("\nHello, %s and %s!", name1, name2); // 2 players

        while (bool == true) {
            if (menu == 0) { // displays a menu and interface
                System.out.println("\nWelcome to the menu. Please choose an option:");
                System.out.println("1. How to play \n2. Play game \n3. Exit");
                menu = s.nextInt();
            }

            if (menu == 1) {
                System.out.println("\nIn the game, Get Rocked, there is a starting number of rocks.");
                System.out.println("Each player takes between 1 and 3 rocks, every turn.");
                System.out.println("The player that takes the last rock loses, or... GETS ROCKED!");
                while (menu != 0) { // user returns to main menu after "How to Play" is displayed
                    System.out.println("Enter 0 to continue");
                    menu = s.nextInt();
                }
            }

            else if (menu == 2) {
                // user must choose a rock starting number >= 16 AND a perfect square
                // checks if rock is less than 16 and square root value is not equal to original rock number
                while (rock < 16 || ((sqrt * sqrt) != rock)) { // if it is, then it prompts user again
                    System.out.println("\nPlease enter a starting rock number, that is greater than or equal to 16, AND a perfect square:");
                    rock = s.nextInt();
                    sqrt = Math.floor(Math.sqrt(rock)); // gets the square root value of the rock number, rounded down
                }

                System.out.println("There are " + rock + " rocks");
                while (rock != 0) { // loop to complete game
                    while ((player1 < 1 || player1 > 3) && rock != 0) { // loops code until player chooses between 1 and 3 rocks
                        System.out.println(name1 + ", how many rocks do you want to take?"); // prompts users with names
                        player1 = s.nextInt();

                        if (player1 > 0 && player1 < 4) { // players can only take 1 to 3 rocks
                            if (player1 > rock) { // players can not take more rocks than there are left
                                System.out.println("There are only " + rock + " rocks left");
                                player1 = 0; // resets guess value so that above loop can run
                            }
                            else {
                                rock -= player1;
                                System.out.println("There are now " + rock + " rocks left"); // announces number of rocks left
                                turns1++; // counts total turns
                                rocks1 += player1; // counts total rocks picked

                                if (rock == 0) { // player loses if they pick up last rock
                                    System.out.println(name1 + " GOT ROCKED!!");  // prompts users with names
                                    win2++; // counts total wins
                                }
                            }
                        } else { // makes sure user enters a proper rock value
                            System.out.println("Please enter a value from 1 to 3");
                        }
                    }

                    while ((player2 < 1 || player2 > 3) && rock != 0) { // loops code until player chooses between 1 and 3 rocks
                        System.out.println(name2 + ", how many rocks do you want to take?");  // prompts users with names
                        player2 = s.nextInt();

                        if (player2 > 0 && player2 < 4) { // players can only take 1 to 3 rocks
                            if (player2 > rock) { // players can not take more rocks than there are left
                                System.out.println("There are only " + rock + " rocks left");
                                player2 = 0; // resets guess value so that above loop can run
                            }
                            else {
                                rock -= player2;
                                System.out.println("There are now " + rock + " rocks left"); // announces number of rocks left
                                turns2++; // counts total turns
                                rocks2 += player2; // counts total rocks picked

                                if (rock == 0) { // player loses if they pick up last rock
                                    System.out.println(name2 + " GOT ROCKED!!");  // prompts users with names
                                    win1++; // counts total wins
                                }
                            }
                        } else { // makes sure user enters a proper rock value
                            System.out.println("Please enter a value from 1 to 3");
                        }
                    }
                    // resets player values for next round
                    player1 = 0;
                    player2 = 0;
                }

                menu = 4;
                while (menu == 4) {
                    // asks user to play again
                    String answer = ""; // local variable, no need for global variable
                    System.out.println("\nWould you like to play again? y/n");
                    answer = s.next();
                    if (answer.equalsIgnoreCase("yes") || answer.equalsIgnoreCase("y"))
                        menu = 2; // restarts game if user chooses to play again
                    else if (answer.equalsIgnoreCase("no") || answer.equalsIgnoreCase("n"))
                        menu = 0; // goes back to main menu if user chooses not to play again
                    else
                        System.out.println("Please enter a valid answer.");
                }
            }

            else if (menu == 3) {
                if (win1 > 0 || win2 > 0) { // shows the scoreboard (wins, turns taken, rocks picked, etc.)
                    System.out.println("\nHere is the scoreboard!");
                    System.out.printf("%-20s %s%n", name1, name2);
                    System.out.printf("%d total wins \t\t %d total wins%n", win1, win2);
                    System.out.printf("%d total turns \t\t %d total turns%n", turns1, turns2);
                    System.out.printf("%d rocks picked \t %d rocks picked%n", rocks1, rocks2);
                }
                // prompts users with names
                // program completes when user selects exit
                System.out.printf("\nBye %s and %s, thanks for playing and have a great day!", name1, name2);
                bool = false; // ends main while loop, ending the program
            }

            else { // loops if user does not choose a valid option from the menu
                System.out.println("\nPlease choose a valid option.");
                menu = 0;
            }
        }
    }
}

The problem with the code is in the part below. The program should request the user for input, but instead it runs the else statement, then loops again. However, this only happens if answer = s.nextLine();. If i change the code to answer = s.next(), the error disappears. In order to recreate the problem, you can run the game, and wait for the play again screen to come. The program will ask you to play again, but before you can input any response, it will say Please enter a valid answer, and loop again. It seems as if there's already some input in the String, but the string is empty. Can someone please help me? Why does it run the else statement without asking the user, only when the code is answer = s.nextLine();? Any help would be appreciated.

menu = 4;
while (menu == 4) {
    // asks user to play again
    String answer = ""; // local variable, no need for global variable
    System.out.println("\nWould you like to play again? y/n");
    answer = s.nextLine();
    if (answer.equalsIgnoreCase("yes") || answer.equalsIgnoreCase("y"))
        menu = 2; // restarts game if user chooses to play again
    else if (answer.equalsIgnoreCase("no") || answer.equalsIgnoreCase("n"))
        menu = 0; // goes back to main menu if user chooses not to play again
    else
        System.out.println("Please enter a valid answer.");
}
Yassin Hajaj
  • 20,020
  • 9
  • 41
  • 81

0 Answers0