简体   繁体   中英

Java Bowling Game - Referencing different objects

OK, so I have a bowling game score calculator and in it I have the main class that reads input from the command line and create a "PlayerScore" object for each player, the PlayerScore object has an arraylist and a method to add to that arraylist, the game is supposed to go frame by frame and alternate between the players, asking for the amount of pins they knocked down in each frame and then calculating and generating a formatted score sheet, So for example I input that there will be two players, then I store the player names in a String ArrayList and then an empty playerscore object in a seperate cooresponding PlayerScore Arraylist, I have a for loop that alternates between the players nested in a for loop that keeps track of the current frame, So when I input the scores for one player it should reference their playerscore object, here is my code:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
/*
* In each iteration call the add method of each players  PlayerScore object
*/

public class BowlingGameCalculator {
    static int Frame = 1;
    private static String res;
    static ArrayList<PlayerScore> playerArrays = new ArrayList<PlayerScore>();
    //static HashMap<String, PlayerScore> play = new HashMap<String, PlayerScore>();
    static ArrayList<String> newscore = new ArrayList<String>();
    static ArrayList<String> players = new ArrayList<String>();
    static String[] scores = new String[21];
    static int rscore;
    static ArrayList<Integer> running_score = new ArrayList<Integer>();

    public static void main(String[] args) throws IOException {
        System.out.println("Enter the number of bowlers");
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        for(int i =0; i<num; i++) {
            System.out.println("enter bowlers name:");
            Scanner inp = new Scanner(System.in);
            String input = inp.next();
            PlayerScore scr = new PlayerScore();
            // play.put(input, scr);
            players.add(input);
            playerArrays.add(scr);
        }
        System.out.println(players);
        outerloop:

        for(int j=0; j<players.size();) {
            for(int k=0; k< 10;) {
                if(j > players.size()-1) {
                    j = j-players.size();
                    Frame++;
                }

                System.out.println("Frame : " + Frame);
                if(Frame == 11) {
                    break outerloop;
                }
                String playr = players.get(j);
                System.out.println("enter rolls for " + playr);
                System.out.println("roll 1 : ");

                Scanner inp = new Scanner(System.in);
                int input = inp.nextInt();
                if(input == 10) {
                    PlayerScore score = playerArrays.get(j);
                    System.out.println(score);
                    score.addRoll(input);
                    score.show();
                    //   PlayerScore scr = play.get(j);
                    // scr.addRoll(input);
                    j++;
                }
                else {
                    System.out.println("roll 2 :");
                    Scanner inp2 = new Scanner(System.in);
                    int input2 = inp2.nextInt();
                    PlayerScore score = playerArrays.get(j);
                    System.out.println(score);
                    score.addRoll(input);
                    score.addRoll(input2);
                    score.show();
                    //  PlayerScore scr = play.get(playr);
                    // scr.addRoll(input);
                    //scr.addRoll(input2);
                    // System.out.println(scr);
                    // scr.show();
                    //System.out.println(play);
                    j++;
                }
            }
            //System.out.println(play);
        }
    }
}

And here is the PlayerScore class

import java.util.ArrayList;
public class PlayerScore {
    static ArrayList<Integer> score = new ArrayList<Integer>();
    static ArrayList<Integer> newscore = new ArrayList<Integer>();
    static ArrayList<Integer> running_score = new ArrayList<Integer>();
    public static int rscore;

    public PlayerScore() {

    }
    public PlayerScore(int[] in) {

    }
    public void addRoll(int x) {
        score.add(x);
    }

    //public boolean isComplete(){

    //}
    //public int getScore(){

    //}

    public ArrayList<Integer> show() {
        System.out.println(this.score);
        return this.score;
    }

now here is the output from a trial run

Enter the number of bowlers
2
enter bowlers name:
matt
enter bowlers name:
derp
[matt, derp]
Frame : 1
enter rolls for matt
roll 1 : 
5
roll 2 :
5
PlayerScore@169ca65
[5, 5]
Frame : 1
enter rolls for derp
roll 1 : 
5
roll 2 :
5
PlayerScore@1301ed8
[5, 5, 5, 5]
Frame : 2
enter rolls for matt
roll 1 : 

Ok, so as you can see it adds the rolls [5,5] to the arraylist in matt's playscore object but then it adds the rolls to the same ArrayList which made me think that I was accidentally referencing the same object but as you can see I printed out the object ID's to the console and it shows that I am indeed calling different references of the PlayerScore object, it should be [5,5] for my score then [5,5] for derps score in frame 1, any help is appreciated

  • One of you're problem is that you're using static fields, in particular the PlayerScore class's score ArrayList. This is a problem as the same ArrayList is shared by all PlayerScore instances, which I believe is causing your main bugs. The solution is easy: Don't use static fields here, and when you do use them, do so sparingly and for specific purpose.
  • Another problem is that your code has unnecessary junk in it. Why do you declare two completely different newscore and running_score fields in two different classes? You will want to eliminate one of these.
  • Also you are creating too many Scanner objects where only one is needed.
  • Your code formatting, especially your indentation, is horrific. Why is this important? The better your code formatting, the easier it is to see what line of code belongs to what block of execution, the easier it is to tell what your code is doing, the easier it is for both you and us to debug it. Don't try to make your and our jobs harder by not taking care to format your code well. A little bit of effort will go a long way towards this.

You have declared the score variable as static in the PlayerScore class which means it is a class variable - there is only one which will be shared by the two PlayerScore objects you have created.

http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM