Having some trouble with my java program and arrays. Here are the instructions I
ID: 3863585 • Letter: H
Question
Having some trouble with my java program and arrays. Here are the instructions I am suppose to follow.
Instructions Multi-Player Random Number Game!! ”
•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 wishes to play again. If so, reset all values.
Rubric: 0. Flowcharts are submitted and matches code – 15 pts
1. Program executes as submitted - 5 points
2. Random Number is selected for each player - 5 points
3. User is/are warned on out of range/illegal entries - 10 Pts
4. Correct response displayed for each entry – 3 pt each (too high / too low / correct)
5. Players are asked in the right order or skipped correctly (10 pts)
6. Users are notified of outcome of ranking (after all win) 10 pt
7. Correct output displaying number of guesses per player 10 pts
8. Incorrect guesses for players in a table at the end of play 10pt
9. Program prompts users to play again, and resets values – 10 Pts.
10. Program terminates gracefully - 1 points
BONUS POINTS: Ask the user for both a minimum and maximum possible value for the chosen random number. Remember – Your program MUST be able to generate a random number that includes the user’s selected minimum and maximum number!! (Worth 10 Pts Bonus)
Explanation / Answer
package chegg;
import java.io.IOException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.Scanner;
public class GameOfUserGuess {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int count = takeInput(scanner, "Enter number of users !!");
System.out.println(count);
User[] users = new User[count];
for (int indx = 0; indx < count; indx++) {
User user = new User(indx);
users[indx] = user;
}
int max = 10;
while (true) {
int allCorrect = 0;
for (int bindx = 0; bindx < count; bindx++) {
if (users[bindx].isCorrected()) {
allCorrect++;
}
}
if (allCorrect == count) {
for (int bindx = 0; bindx < count; bindx++) {
System.out.println(users[bindx].toString());
}
System.out
.println("Game finished ! Got all the correct number ");
break;
}
for (int indx = 0; indx < count; indx++) {
int randomNum = new Random().nextInt(10);
System.err
.println(" --------------------------------------------Random number "
+ randomNum);
if (!users[indx].isCorrected()) {
int userGuessValue = takeInput(scanner, "Guess any number");
users[indx].getUserGuessValue().add(userGuessValue);
if (userGuessValue == randomNum) {
System.out.println("CORRECT !!");
users[indx].setCorrected(true);
} else {
System.out.println(" Oops !! Number is incorrect ");
int difference = userGuessValue - randomNum;
if (difference < 0) {
difference = -(difference);
}
if (difference >= max) {
System.err.println(" Too High !!");
} else {
System.err.println(" Too Low !!");
}
}
}
}
}
}
public static int takeInput(Scanner scanner, String inputMessage) {
boolean isValidCount = false;
int number = -1;
do {
System.err.println(inputMessage);
while (!scanner.hasNextInt()) {
System.err.println("Input should be an integer value");
scanner.next();
}
try {
number = scanner.nextInt();
isValidCount = true;
break;
} catch (InputMismatchException e) {
System.err.print("input should be integer value");
} catch (NoSuchElementException e1) {
System.err.print("input should be integer value");
}
} while (!isValidCount);
return number;
}
}
class User implements Comparable<User> {
private int userId;
private List<Integer> userGuessValues;
private boolean isGuessCorrected;
public User(int userId) {
this.userId = userId;
this.userGuessValues = new ArrayList<Integer>();
}
public int getUserId() {
return userId;
}
public List<Integer> getUserGuessValue() {
return userGuessValues;
}
public boolean isCorrected() {
return isGuessCorrected;
}
public void setCorrected(boolean isGuessCorrected) {
this.isGuessCorrected = isGuessCorrected;
}
@Override
public String toString() {
return "User [userId=" + userId + ", userGuessValues="
+ userGuessValues + ", isGuessCorrected=" + isGuessCorrected
+ "]";
}
@Override
public int compareTo(User o) {
int i = -9999;
if (o != null)
i = -1;
if (this.getUserGuessValue() == o.getUserGuessValue()) {
i = 0;
} else {
i = 1;
}
return i;
}
}
Output
-----------
Enter number of users !!
3
3
--------------------------------------------Random number 8
Guess any number
8
--------------------------------------------Random number 7
Guess any number
CORRECT !!
7
--------------------------------------------Random number 7
Guess any number
CORRECT !!
4
Too Low !!
--------------------------------------------Random number 4
--------------------------------------------Random number 1
--------------------------------------------Random number 8
Guess any number
Oops !! Number is incorrect
3
Oops !! Number is incorrect
Too Low !!
--------------------------------------------Random number 6
--------------------------------------------Random number 6
--------------------------------------------Random number 7
Guess any number
7
CORRECT !!
User [userId=0, userGuessValues=[8], isGuessCorrected=true]
User [userId=1, userGuessValues=[7], isGuessCorrected=true]
User [userId=2, userGuessValues=[4, 3, 7], isGuessCorrected=true]
Game finished ! Got all the correct number
Description :
1. In above program output, random number is shown just beacuse you can easily test the all the correct senarion and get the result. Once you gone through with output or program logic you can comment out the print statment and then play.
2. Here i take user class whihc has three properties
user number - it shows unique user
number list - List maintains all the guess taken by user throughout the game.
boolean flag iscorrectred - this flag will show that whether the correct number has been choosen by the user or not.you can identify the users which are still playing game from this flag at any point of time in this program and for the same i have introduced toString method.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.