0
votes

I am reading in a file in java and adding it to a multi dimensional array.

The file looks like this:

14.9 18.9 -18.6 -7.7 11.5 0.6 -5.3 -15.5 -1.5 -12.5 19.2 -8.8
2.9 -2.9 13.4 -12.8 -9.8 6.2 11.0 0.9 3.7 17.3 -0.2 -18.7
18.5 -2.8 6.7 16.5 7.4 -17.5 17.8 -14.3 -11.2 14.6 7.9 -9.7
7.0 16.8 12.9 -0.2 -7.4 0.7 8.6 11.8 3.4 5.7 3.1 -15.6
-12.2 -5.4 -4.1 -15.6 -14.8 -5.1 -0.6 4.2 14.1 13.5 13.2 -9.9
1.6 2.1 11.5 9.1 -13.8 -3.9 15.8 15.4 8.9 16.8 6.8 20.0
13.7 -12.6 12.2 1.1 18.7 -19.0 -11.4 -10.7 -2.9 -0.6 -9.5 0.4
14.2 -3.3 6.8 -5.9 -4.5 12.2 19.3 -13.0 -3.7 14.5 6.9 14.6
1.9 -10.7 19.2 -3.7 -10.7 -18.5 3.1 -16.5 10.6 -1.9 -17.7 -15.3
-17.4 0.2 4.1 1.1 -12.1 -20.0 -10.4 -9.3 -19.0 15.1 -3.7 -16.7
-14.9 -6.9 -5.9 -11.9 -15.7 -19.7 -8.1 -3.8 8.5 8.0 17.3 9.7
-4.0 -5.4 16.0 7.8 19.6 -7.0 0.2 9.3 -9.4 13.1 -5.3 -7.0
-14.1 -11.8 -6.6 8.5 16.2 -13.6 6.4 2.0 17.0 -0.5 -10.6 18.2
-1.0 18.5 9.1 13.4 6.6 -3.7 -15.1 11.8 10.7 -8.1 14.1 12.8
-2.7 1.2 -3.1 17.0 6.2 14.5 5.3 -12.9 -5.6 12.2 10.5 -17.1
-19.6 11.0 6.1 15.2 5.1 5.7 6.5 -19.6 -18.7 11.2 -1.2 11.0
-5.9 17.9 -15.4 -2.5 8.4 16.8 -2.3 -13.6 -12.5 16.1 -19.6 -16.5
-4.4 2.4 -6.6 3.1 6.3 16.6 -1.5 11.6 14.3 -15.5 -12.7 -11.7
5.2 4.1 -2.1 -18.9 -16.0 1.9 17.3 12.7 -2.8 -3.4 -3.3 12.0
-12.1 5.2 -12.8 -19.9 -11.8 -17.1 -14.6 3.4 -7.1 12.7 8.3 0.5
3.5 2.7 -3.1 -9.9 17.1 2.3 18.1 -8.8 -2.8 -14.2 -4.6 17.3
0.2 18.7 -16.8 18.5 4.5 -7.8 -1.3 -10.0 5.8 -11.4 -11.5 12.4
19.3 13.2 -6.7 3.3 -17.9 -2.4 -6.8 -11.8 9.5 -13.1 -0.7 -3.1
-3.3 6.2 1.3 -11.3 6.2 -5.9 -11.7 14.4 -11.4 0.8 0.1 2.0
-2.0 -8.1 -13.4 13.9 -6.3 -13.3 -14.0 17.9 10.1 19.9 -5.3 -13.3
1.2 -3.5 -2.5 5.5 -12.8 -16.7 -9.7 -18.7 5.5 5.4 -3.8 13.3
-15.5 5.3 -9.4 -19.7 -4.7 -0.8 17.8 3.6 -12.5 -1.7 13.8 -8.2
3.0 12.4 14.8 -19.4 16.2 -3.8 7.4 -6.4 -8.2 5.2 -1.5 18.9
-13.0 12.6 -0.8 -1.3 11.2 -0.4 10.3 -11.8 -4.4 3.2 9.0 -0.1
7.2 -17.0 11.3 14.9 -14.7 18.7 19.6 13.7 13.4 -16.3 7.5 -1.2

Each line represents a game of darts for two players. The first 6 are pairs of 3 for player one and the second 6 are pairs of 3 for player two.

The program reads the file and then spits out the score based for each line.

Example:

input: (14.9, 18.9) (-18.6, -7.7) (11.5, 0.6) (-5.3, -15.5) (-1.5, -12.5) (19.2, -8.8)
output: Player two wins: Player one score 280, Player two score 290.

How do I handle each line to score the first player and then score the second player throws?

Edit

Sorry for not showing code, thought the question could be answered with out.

public class DartBoard {
    private double[][] darts;

    public DartBoard(String file) throws FileNotFoundException {
        Scanner scan = new Scanner(new File(file));
        int numberRows = 12;
        int numberCols = 30;

        darts = new double[numberRows][numberCols];
        for(int i = 0; i < numberRows; i++){
            for(int j = 0; j < numberCols; j++){
                if(scan.hasNextDouble()){
                    darts[i][j] = scan.nextDouble();
                }
            }
        }
    }

    public double distance(double x, double y){
        return Math.sqrt(x*x + y*y);
    }

    public int score(double x, double y){
        double dist = distance(x,y);
        int s = 0;
        if(dist < 4){
            s = 100;
        } else if(dist < 8){
            s = 80;
        } else if(dist < 12){
            s = 60;
        } else if(dist < 16){
            s = 40;
        } else if(dist < 20){
            s = 20;
        }
        return s;
    }
}

I am at a lost on how to score each line. Every two doubles would be passed to the method score() and once I got 3 score that would be added for player ones total score and then same for the next 6 doubles. And then repeat until all lines are processed.

2
What have you tried? Show us what you have. Where are you stuck? Can you: 1) Read the file? 2) Read lines from the file? 3) Split a line into 12 strings? 4) Parse them to double? 5) Pair them? 6) Separate the pairs into 3 for player one and 3 for player 2? 7) If you can do all of that, then what is the problem? --- Voting to close as "Too broad", aka "Needs more focus", since the question is way too open right now, which means it just looks like you want us to write your code for you, and that's not what StackOverflow is about. - Andreas
Show us some code!... - Idle_Mind
@Andreas I added the code I already have. I am just so confused on how to go about splitting the values into each player. The txt file has a line of 12 doubles and that's how I am adding it to the multidimensional array. - Alexander McComb
@Idle_Mind add the working code I have now. Just lost on what the next steps would be for reading each line to scored and how that would be processed! - Alexander McComb
To me, it looks like your file has 30 rows of 12 columns, so why does code have int numberRows = 12; int numberCols = 30;? - Andreas

2 Answers

1
votes

Here's how I would approach it:

import java.io.File;
import java.util.Scanner;
import java.text.DecimalFormat;
public class DartBoard
{

    public static void main(String[] args)
    {
        String fileName = "C:\\Users\\mikes\\Documents\\darts.txt";
        try {
            DartBoard db = new DartBoard(fileName);
            db.DisplayScores();
        } catch (Exception ex) {
            ex.printStackTrace();
        }                             
    }

    private double[][] darts;

    public DartBoard(String file) throws Exception {
        // we are going to read the file TWICE

        // figure out how many rows/cols we have:
        Scanner scan = new Scanner(new File(file));             
        int numberRows = 0;
        int numberCols = -1;
        while(scan.hasNextLine())
        {
            String row = scan.nextLine();
            numberRows++;
            String[] columns = row.split(" ");
            if (numberCols == -1)
            {
                numberCols = columns.length;
            }
            else if (columns.length != numberCols)
            {
                throw new IllegalArgumentException("Number of columns in each row does not match!");
            }
        }

        // instantiate the array and populate it:
        darts = new double[numberRows][numberCols];
        scan = new Scanner(new File(file));
        for(int i = 0; i < numberRows; i++){
            for(int j = 0; j < numberCols; j++){
                if(scan.hasNextDouble()){
                    darts[i][j] = scan.nextDouble();
                }
            }
        }
    }

    public void DisplayScores() throws Exception
    {
        if (darts != null)
        {
            int rows = darts.length;
            int cols = darts[0].length;
            if (cols % 2 == 0)
            {
                System.out.println("Scoring Results:");
                DecimalFormat f = new DecimalFormat("000");
                for(int r = 0; r < rows; r++)
                {
                    int player1Score = 0;
                    int player2Score = 0;
                    for(int c = 0; c < cols; c += 2)
                    {
                        if (c < cols / 2)
                        {
                            player1Score += score(darts[r][c], darts[r][c+1]);
                        }
                        else
                        {
                            player2Score += score(darts[r][c], darts[r][c+1]); 
                        }
                    }
                    String winner = (player1Score == player2Score ? "Tie" : player1Score > player2Score ? "One" : "Two");
                    System.out.println("Winner: Player " + winner + "  |  Player One Score: " + f.format(player1Score) + "  |  Player Two Score: " + f.format(player2Score));
                }
            }
            else
            {
                throw new IllegalArgumentException("Number of columns cannot be odd!");
            }
        }
        else
        {
            throw new IllegalArgumentException("No data to work with!");
        }
    }

    private double distance(double x, double y){
        return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
    }

    private int score(double x, double y){
        double dist = distance(x,y);
        int s = 0;
        if(dist < 4){
            s = 100;
        } else if(dist < 8){
            s = 80;
        } else if(dist < 12){
            s = 60;
        } else if(dist < 16){
            s = 40;
        } else if(dist < 20){
            s = 20;
        }
        return s;
    }

}

Output:

Scoring Results:
Winner: Player Tie  |  Player One Score: 060  |  Player Two Score: 060
Winner: Player One  |  Player One Score: 160  |  Player Two Score: 100
Winner: Player Tie  |  Player One Score: 060  |  Player Two Score: 060
Winner: Player Two  |  Player One Score: 140  |  Player Two Score: 160
Winner: Player Two  |  Player One Score: 100  |  Player Two Score: 120
Winner: Player One  |  Player One Score: 180  |  Player Two Score: 020
Winner: Player Two  |  Player One Score: 060  |  Player Two Score: 200
Winner: Player One  |  Player One Score: 140  |  Player Two Score: 060
Winner: Player Tie  |  Player One Score: 080  |  Player Two Score: 080
Winner: Player One  |  Player One Score: 100  |  Player Two Score: 060
Winner: Player Two  |  Player One Score: 060  |  Player Two Score: 140
Winner: Player Two  |  Player One Score: 100  |  Player Two Score: 140
Winner: Player Two  |  Player One Score: 080  |  Player Two Score: 100
Winner: Player One  |  Player One Score: 120  |  Player Two Score: 080
Winner: Player One  |  Player One Score: 160  |  Player Two Score: 080
Winner: Player One  |  Player One Score: 100  |  Player Two Score: 060
Winner: Player One  |  Player One Score: 080  |  Player Two Score: 040
Winner: Player One  |  Player One Score: 180  |  Player Two Score: 080
Winner: Player Tie  |  Player One Score: 120  |  Player Two Score: 120
Winner: Player Two  |  Player One Score: 040  |  Player Two Score: 140
Winner: Player One  |  Player One Score: 160  |  Player Two Score: 060
Winner: Player Two  |  Player One Score: 080  |  Player Two Score: 120
Winner: Player Two  |  Player One Score: 100  |  Player Two Score: 160
Winner: Player One  |  Player One Score: 200  |  Player Two Score: 180
Winner: Player One  |  Player One Score: 120  |  Player Two Score: 040
Winner: Player One  |  Player One Score: 180  |  Player Two Score: 120
Winner: Player One  |  Player One Score: 100  |  Player Two Score: 080
Winner: Player Two  |  Player One Score: 060  |  Player Two Score: 140
Winner: Player Tie  |  Player One Score: 180  |  Player Two Score: 180
Winner: Player Two  |  Player One Score: 040  |  Player Two Score: 080
1
votes

I am at a lost on how to score each line.

Start by processing each line, i.e. create a loop over each row in the 2D array.

for (double[] round : darts)

Every two doubles would be passed to the method score()

So loop over the columns in the row, stepping 2 at a time.

for (int i = 0; i < round.length; i += 2)
    score = score(round[i], round[i + 1])

once I got 3 score that would be added for player ones total score and then same for the next 6 doubles

So add first half to player 1 total and add second half to player 2 total

if (i < row.length / 2)
    totalPlayer1 += score
else
    totalPlayer2 += score

And then repeat until all lines are processed.

See first answer, where you created a loop.