Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Random number java multiplayer game *I want to modify the code I already have if possible in order to fit the requirements described below* It

Random number java multiplayer game *I want to modify the code I already have if possible in order to fit the requirements described below* It has to ask players all at the same time what their guesses are Ask how many users will be playing (1-n) Remember Error Checking on ALL inputs Choose a random number for each player Ask each player (one guess per round) to enter their guess Alert the user with Too High, Too Low, CORRECT! Record the answer for each user, if incorrect. If the user was correct, skip them in subsequent rounds. Once all players have guessed their correct number, print out the number of guesses it took for each one to guess correctly, the incorrect responses, and show a ranking of the players. Ask if the user(s) wish to play again.If so, reset all values.

import java.util.ArrayList;

import java.util.Random;

import java.util.Scanner;

public class solvedmultiplay {

public static void main(String[] args) {

//Variables

String playAgain = "No";

boolean isAWinner = false;

int minimum_value,maximum_value,players;

Scanner keyboard = new Scanner(System.in);

Random random = new Random();

int[] player_score;

do {

System.out.print(" How many players do you want to play the game ? ");

//calling first method

players = getAnInt();

player_score = new int[players];

System.out.print(" Enter the minimum possible value for the random number : ");

minimum_value = getAnInt();

System.out.print(" Enter the maximum possible value for the random number : ");

maximum_value = getAnInt();

for(int i=1; i<=players; i++){

int magicNum = random.nextInt(maximum_value)+minimum_value;

player_score[i-1] = guessNumber(magicNum,i);

System.out.println("It took you "+player_score[i-1]+" tries to guess correctly!");

}

printRankScore(player_score);

//ask the user if they want to play again

System.out.print(" Hey - play again? Type Yes or No: ");

playAgain = keyboard.nextLine();

while(true)

{

if((!playAgain.equalsIgnoreCase("Yes")) && (!playAgain.equalsIgnoreCase("No")))

{

System.out.println("You must Enter YES -OR- NO!!");

System.out.print(" Hey - play again? Type Yes or No: ");

playAgain = keyboard.nextLine();

}else

System.out.println("thanks for playing have a nice day");

break;}

}while(playAgain.equalsIgnoreCase("Yes"));

keyboard.close();}

public static void incorrectGuesses(int wrongGuess) {

int n = 0;

for(int i=0; i

{

ArrayList incorrectGuesses[] = new ArrayList[n];

incorrectGuesses[i] = new ArrayList();

System.out.println("Player " + (i+1) + ":");

System.out.println("No. of guesses: " + (incorrectGuesses[i].size()+1));

System.out.print("Incorrect guesses: ");

for (int j=0; j

System.out.print(incorrectGuesses[i].get(j) + " ");}}

public static void printRankScore(int player_score[])

{

int[] rank = rankPlayer(player_score);

System.out.println(" Rank Player Guesses ");

for(int i=0;i

{

System.out.println(" "+(rank[i]+1) + "\t"+(i+1)+"\t"+player_score[i]);}}

public static int[] rankPlayer(int player_score[]){

int[] rank = new int[player_score.length];

for(int i=0;i

rank[i] = i;

int min;

for(int i=0;i

min =i;

for(int j=i+1;j

if(player_score[rank[j]] < player_score[rank[min]])

min = j;}

if(min != i) {

int temp_rank = rank[i];

rank[i] = rank[min];

rank[min] = temp_rank;}}

return rank;}

public static int guessNumber(int magicNum,int player) {

int score = 0;

int userGuess;

Scanner scan = new Scanner(System.in);

while(true) {

System.out.println("OK player "+player+" gimme a guess ... integers only.");

userGuess = getAnInt();

score++;

if(isGuessCorrect(userGuess, magicNum)){

break; }}

return score;}

public static boolean isGuessCorrect (int guess, int magic) {

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;

}

return winner;

}

public static int getAnInt () {

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Inference Control In Statistical Databases From Theory To Practice Lncs 2316

Authors: Josep Domingo-Ferrer

2002nd Edition

3540436146, 978-3540436140

More Books

Students also viewed these Databases questions

Question

2. Identify issues/causes for the apparent conflict.

Answered: 1 week ago