I have created a bowling Alley program that gives results for the number of players you enter in. My problem is that my output is always the same number plus itself every round for player1 and for player2 the score is always 0.
I have tried moving around my random number generator but it did not work.
client code:
import java.util.Scanner;
public class Bowling {
public static void main(String[]args){
int playerAmount, flag = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of players in the game: ");
playerAmount = input.nextInt();
Bowlers[] bowlers = new Bowlers[playerAmount];
for (int i = 0; i < playerAmount; i++) {
bowlers[i] = new Bowlers(i);
}
for (int i = 0; i < 10; i++){
for(int j = 0; j < playerAmount; j++){
flag = 0;
while(flag != 2){
bowlers[j].setPinKnock();
bowlers[j].changePinAmount();
if(bowlers[j].getPinAmount() == 0){
flag ++;
}
flag++;
}
bowlers[j].changeScore();
}
for(int g = 0; g < playerAmount; g++){
System.out.println(bowlers[g]);
}
}
}
}
bowlers code:
public class Bowlers {
private int score, pins,tag,pinKnock;
public Bowlers(int i) {
score = 0;
pins = 10;
tag = i;
pinKnock = 0;
}
public void setPinKnock(){
pinKnock = (int)(Math.random() * ((pins - 1) + 1)) + 1;
}
public void changePinAmount(){
pins -= pinKnock;
}
public int getPinAmount(){
return pins;
}
public void changeScore(){
if(pins == 0 && pinKnock == 10){
score += 20;
}else if(pins != 0){
score += (10 - pins);
}else{
score += 15;
}
}
public String toString(){
return "Player" + tag + " Score: " + score;
}
}
Here is my output now after edits:
Player0 Score: 9
Player1 Score: 9
Player2 Score: 8
Player0 Score: 24
Player1 Score: 24
Player2 Score: 23
Player0 Score: 36
Player1 Score: 36
Player2 Score: 35
Player0 Score: 49
Player1 Score: 49
Player2 Score: 47
Player0 Score: 64
Player1 Score: 63
Player2 Score: 60
Player0 Score: 79
Player1 Score: 75
Player2 Score: 74
Player0 Score: 93
Player1 Score: 87
Player2 Score: 87
Player0 Score: 106
Player1 Score: 101
Player2 Score: 99
Player0 Score: 119
Player1 Score: 113
Player2 Score: 112
Player0 Score: 132
Player1 Score: 126
Player2 Score: 124