I am trying to make a game in drjava, in which two players play a two-player game called 'Remove the Last Stone'. I have successfully coded the core of the game. However, after the game finishes, I am required to give the user three options on how they want to proceed after playing the initial starting game. The three options are:
1) Play another game with the same players. 2) Exit the game. 3) Play another game with different players.
So basically, my game would prompt the two players that initially started with playing the game to enter '1' if they want to play another game. What pressing this key would do, is that instead of asking for the players' names again, it would directly ask how many rounds they want to play, and continue on with the rest of the game. At the end of the initial game, the user could enter '2' if 2 other/different players want to play the game. What my code is supposed to do in this case, is to loop back to the very beginning of the code, in which it asks for the names of the players, since two new players are playing. Finally, the user can choose to enter '3' to exit out of the game. I have tried to code this, but for some reason, I am having issues with looping back to 2 different parts of my game code based on the user's input between pressing '1' and '2'. I am also having problems with exiting the game, when the user chooses to press 3. My issue is that when any of these 3 keys are pressed ('1', '2', or '3'), my game repeatedly asks how many rounds the user want to play. This is not supposed to happen, as these 3 keys are supposed to perform different operations.
Here is my game code:
// Import the Scanner class
import java.util.Scanner;
public class StrategyGame {
public static void main(String[] args) {
// Create a new Scanner object for keyboard input
Scanner keyboard = new Scanner(System.in);
// Variable declarations
String namePlayerOne, namePlayerTwo;
String lastPlayerTurn = "";
boolean playDifferentGame = true;
boolean playSameGame = true;
boolean playRounds = true;
boolean validInputPlayerOne = false;
boolean validInputPlayerTwo = false;
boolean validProceed = false;
int userGameRounds, playerRemoveLimit, gameStonePile, playerTwoRemoval, proceedChoice;
int playerOneRemoval = 0;
int numRounds = 0;
int playerOneWins = 0;
int playerTwoWins = 0;
// Do while loop responsible for executing whole game first, then repeatedly loops if the same two
// players that started the game want to play again
do {
System.out.println();
System.out.println("Player 1, please enter your name:");
namePlayerOne = keyboard.nextLine();
System.out.println("Player 2, please enter your name:");
namePlayerTwo = keyboard.nextLine();
System.out.println();
// Display game instructions and how to play game
System.out.println(namePlayerOne + " and " + namePlayerTwo + ", welcome to the Strategy Game!"
+ " The rules of this game are very simple. You guys will be iteratively asked"
+ " to remove some stones from a pile of stones, which will total different amounts"
+ " for each round. For each round, you will only be allowed to remove a specific maximum"
+ " number of stones. A round from the game will finish when either one of you removes the"
+ " remaining stones and there are zero stones left in the pile. You will be asked how many"
+ " rounds you want to play, and so, a game will finish when your chosen number of rounds"
+ " have been completed.");
// Do while loop that repeatedly loops, after the very first game, if two different players want to play
do {
System.out.println();
System.out.println("How many rounds would you like to play? You can select between 2-5 rounds inclusively.");
userGameRounds = keyboard.nextInt();
// Ask user how many rounds they want to play, between 2-5 rounds (inclusively)
while (userGameRounds < 2 || userGameRounds > 5) {
System.out.println();
System.out.println("Invalid input. Please try again.");
userGameRounds = keyboard.nextInt();
}
// If user-input is valid; enters into execution of game rounds
while (playRounds == true) {
gameStonePile = (int) (11 * Math.random() + 10);
playerRemoveLimit = (int) (3 * Math.random() + 2);
numRounds = numRounds + 1;
System.out.println();
System.out.println("It is now round " + numRounds + ". For each turn, each player"
+ " is only allowed to remove a maximum of " + playerRemoveLimit + " stones.");
System.out.println("** Important Note: Each player should also avoid removing stones that exceed"
+ " the number of stones that are remianing in the stone pile, even if the amount you"
+ " remove is within the removing limit range. (i.e., You remove 3 stones, when 2 stones are left in pile.)"
+ " **");
// Until game stone pile reaches zero, the 2 players continue alternating turns
while (gameStonePile != 0) {
if (gameStonePile != 0) {
do {
validInputPlayerOne = false;
System.out.println();
// Player One's turn
System.out.println(namePlayerOne + ", it is your turn. Please enter the amount of stones you want to remove."
+ " In the stone pile currently, there are " + gameStonePile + " stone(s).");
// Get amount Player 1 wants to remove from current/remaining stone pile
playerOneRemoval = keyboard.nextInt();
while (playerOneRemoval < 1 || playerOneRemoval > playerRemoveLimit || playerOneRemoval > gameStonePile) {
System.out.println("Invalid input. Please try again.");
playerOneRemoval = keyboard.nextInt();
}
// Subtract amount Player 1 decides to remove from the remaining game stone pile
validInputPlayerOne = true;
gameStonePile = gameStonePile - playerOneRemoval;
// Record that Player 1 was the most recent player that removed stones from pile
lastPlayerTurn = namePlayerOne;
// When the stone pile reaches zero, determine Player 1 as winner
if (gameStonePile == 0) {
playerOneWins = playerOneWins + 1;
System.out.println();
System.out.println(namePlayerOne + " wins round #" + numRounds + "! The current score is:");
System.out.println();
System.out.println(namePlayerOne + ": " + playerOneWins);
System.out.println(namePlayerTwo + ": " + playerTwoWins);
}
} while (playerOneRemoval >= 1 && playerOneRemoval <= playerRemoveLimit && validInputPlayerOne == false);
}
if (gameStonePile != 0) {
do {
validInputPlayerTwo = false;
// Player Two's turn
System.out.println(namePlayerTwo + ", it is your turn. Please enter the amount of stones you want to"
+ " remove. In the stone pile currently, there are " + gameStonePile + " stone(s).");
// Get amount Player 2 wants to remove from current/remaining stone pile
playerTwoRemoval = keyboard.nextInt();
while (playerTwoRemoval < 1 || playerTwoRemoval > playerRemoveLimit || playerTwoRemoval > gameStonePile) {
System.out.println("Invalid input. Please try again.");
playerTwoRemoval = keyboard.nextInt();
}
validInputPlayerTwo = true;
// Subtract amount Player 2 decides to remove from the remaining game stone pile
gameStonePile = gameStonePile - playerTwoRemoval;
// Record that Player 1 was the most recent player that removed stones from pile
lastPlayerTurn = namePlayerTwo;
if (gameStonePile == 0) {
playerTwoWins = playerTwoWins + 1;
System.out.println();
System.out.println(namePlayerTwo + " wins round #" + numRounds + "! The current score is:");
System.out.println();
System.out.println(namePlayerOne + ": " + playerOneWins);
System.out.println(namePlayerTwo + ": " + playerTwoWins);
}
} while (playerTwoRemoval >= 1 && playerOneRemoval <= playerRemoveLimit && validInputPlayerTwo == false);
}
// When the user-requested # of rounds have been reached and game-stone pile reaches zero from
// very last round, display end of game message
if (numRounds == userGameRounds && gameStonePile == 0) {
numRounds = 0;
playRounds = false;
System.out.println();
System.out.println("Hooray! The game has finished!");
// Determine the best player so far and their # of wins
if (playerOneWins > playerTwoWins) {
System.out.print(namePlayerOne + " is the best player so far with " + playerOneWins + " win(s).");
}
else if (playerTwoWins > playerOneWins) {
System.out.print(namePlayerTwo + " is the best player so far with " + playerTwoWins + " win(s).");
}
else{
System.out.print(namePlayerOne + " and " + namePlayerTwo + ", it looks like it's a draw!");
}
}
// Once game between two players finishes, ask user how they want to proceed based on three
// options
if (numRounds == 0){
System.out.println();
System.out.println("How would you like to proceed? You have three options. Enter '1' if you would like to"
+ " play another game with the same players. Enter '2' if you want to exit the game."
+ " Enter '3' to play another game with different players.");
// Get user-input of how they want to proceed
proceedChoice = keyboard.nextInt();
// Check for valid input
while (proceedChoice <1 || proceedChoice >3){
validProceed = false;
System.out.println("Invalid input. Please try again.");
proceedChoice = keyboard.nextInt();
}
validProceed = true;
// Loop back accordingly to if the same players want to play a new game, different players
// want to play a new game, and exit the game if user chooses to do so
if (proceedChoice == 1) {
playSameGame = true;
}
else if (proceedChoice == 2) {
playDifferentGame = true;
}
else if (proceedChoice == 3){
playDifferentGame = false;
}
}
}
}
} while (playSameGame == true);
} while (playDifferentGame == true);
keyboard.close();
} // main method
} // Strategy Game class
I know this issue has to do something with my loops, but I can't seem to figure it out. I would appreciate some help. Thanks!
Your loop looks for your playSameGame variable to be false for it to exit. But it is never set to false anywhere in your code. So it will repeat forever.
I agree with IanGabes comment for writing good stack overflow questions, but also just for learning how to debug these things on your own. The best way is to make your code as simple as possible when you have a problem.