Question: I'm trying to build a multiplayer java game but I am having some trouble incorporating a few elements. The code must: 1) prompt the user
I'm trying to build a multiplayer java game but I am having some trouble incorporating a few elements. The code must:
1) prompt the user to play again
2) ask how many players they would like to play with (I already have this part of the code functional)
3) print how many tries it took for each player (i have this part of the code but it does not run as expected)
4) print a table at the end of the game showing all the guesses for each player
5) Rank the players in order of tries it took to guess the correct number (least guesses to the correct answer would be first)
6) use at least 5 methods
Bonus: Ask the user for both a minimum and maximum possible value for the chosen random number.
I would like to work with the code that I already have; I will copy and paste it below
public static void main (String [] args) {
boolean playAgain = false;
do {
boolean YNError = false;
boolean isAWinner = false;
//boolean playAgain = true;
Random random = new Random();
System.out.println("How many players do you want to play the game");
int players = getAnInt();
int[] player_score = new int[players];
for(int i=1; i<=players; i++){
int score = 0;
int magicNum = random.nextInt(5)+1;
while(true){
System.out.println("OK player "+i+" gimme a guess ... integers only.");
int userGuess = getAnInt();
score++;
if(isGuessCorrect(userGuess, magicNum)){
player_score[i-1] = score;
System.out.println("It took you "+score+" tries to guess correctly!");
break;
}
}
}
System.out.println("");
for(int i=1; i<=players; i++){
if (isAWinner == true) {
System.out.println("It took player"+i+ " " +player_score[i-1]+" attempts to correctly guess. ");}
}while(isAWinner=false);
//boolean YNError = true;
do {
System.out.println("Hey - play again? Type Yes or No: ");
Scanner keyboard = new Scanner(System.in);
String inputYN = keyboard.nextLine();
if (inputYN.compareToIgnoreCase("NO") == 0 ) {
playAgain = false;
YNError = false;
System.out.println("Thanks for playing - have a nice day");
}else if (inputYN.compareToIgnoreCase("YES") == 0 ) {
playAgain = true;
YNError = false;
System.out.println("Here we go again!");
}else {
playAgain = true;
YNError = true;
System.out.println("You must Enter YES -OR- NO!!");
}
}while (YNError == true);
} while (playAgain == true);
}
static boolean isGuessCorrect (int guess, int magic) {
// int [] savedGuesses = new int [guess];
boolean winner = false;
if (guess >magic) {
System.out.println("hey too high!");
} else if (guess < magic) {
System.out.println("hey too low!");
} else {
System.out.println("CORRECT");
winner = true;
// System.out.println(savedGuesses);
}
return winner;
}
static int getAnInt () {
int score;
int sum = 0;
int enteredNumber = 0;
Scanner myScanner = new Scanner(System.in);
boolean numberError = false;
String enteredString = "";
do {
try {
enteredString = myScanner.next(); //Read into a string
enteredNumber = Integer.parseInt(enteredString.trim()); //then cast as a integer
numberError = false; //if we haven't bailed out, then the number must be valid.
} catch(Exception e) {
System.out.println("Your entry: \"" + enteredString + "\" is invalid...Please try again");
numberError = true; //Uh-Oh...We have a problem.
}
} while (numberError == true ); //Keep asking the user until the correct number is entered.
return enteredNumber;
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
