Trying to make this into multiplayer…Needs to be up to 5 players. I have the basics already, just cant figure out how to make it work for multiplayer. The ways that I tried we’re not storing the guesses for each person. This is the stripped down version.
import java.util. * ;
public class randomNumber {
public static void hint(int answer, int guess) {
if(answer < guess) {
System.out.println("Incorrect! \n TOO HIGH!.");
} else if(answer > guess) {
System.out.println("Incorrect! \n TOO LOW!.");
} else if(answer == guess) {
System.out.println("YOU GOT IT!");
}
}
public static void main(String[] args) {
System.out.println("How many Players will be playing?");
int players = players();
int player = 0;
player++;
System.out.println("Player " + player + " Enter a number");
int guess = guess();
int answer = random();
int count = 0;
while (guess != answer) {
hint(answer, guess);
count = count(count);
guess = guess();
}
hint(answer, guess);
System.out.println("It took " + count(count) + " tries!");
}
public static int random() {
return(int)(Math.random() * 100) + 1;
}
public static int players() {
Scanner my_input = new Scanner(System. in );
return my_input.nextInt();
}
public static int guess() {
Scanner my_input = new Scanner(System. in );
return my_input.nextInt();
}
public static int count(int c){
c++;
return c;
}
}
You have made a class, it’s the first step in O.O. programming
But, all your methods are
static, it’s a limitation.You have to remove all the
statickeyword but the main and to instantiate your game 5 times.It’s the second step in O.O.: the instances.
Once done you’ll see some details need to be unique, you’ll create a second class, instantiate one time. This class will be in relation with the 5 instances.
Walk on this way yourself…