Question

Create a method in Scores.java that takes 2 arguments: player1Scores and player2Scores. The method should print out the number of games player1 won and the number of games player2 won.

This is the last part to an assignment given by our teacher and I can't figure out where to start. I was thinking a simple compare each win but thats not provided by the Player1Score. It only stores the current score of the game thats going on. I need to be able to calculate all the games that have happened. Any idea how i could add this to my code? Here is a snippet of the program I'm working on so far.

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Scores {

public static void main (String args[]) 

{


}
static String allScoresFilename = "allscores.txt";

static String lastScoreFilename = "lastscores.txt";

public boolean writeScores(int player1Score, int player2Score) throws IOException {

PrintWriter scores = new PrintWriter (new File ("Score.txt"));

    scores.println("Player 1 \t Player 2 \n " +player1Score+"\t\t\t " +player2Score);

PrintWriter scores1 = new PrintWriter(new BufferedWriter(new FileWriter("Scores Append", true)));

    scores1.println("Player One:"+player1Score+"\tPlayer Two:"+player2Score);

    scores1.close();

    scores.close();

    return true;

}//End writeScores

public boolean appendScores(int player1Score, int player2Score) throws IOException{

    return true;

}//End appendScores

public void readScores() throws FileNotFoundException

{

    File scores = new File ("Scores Append");

    Scanner fileScanner = new Scanner (scores);

    fileScanner.useDelimiter("[\t|,|\n|\r|:]+");

    String players, player1;

    int numbers, i=0;

    int []p1= new int[10];

    int []p2= new int[10];

    while (fileScanner.hasNext())
    {

        players = fileScanner.next();

        p1[i] = fileScanner.nextInt();

        player1 = fileScanner.next();

        p2[i] = fileScanner.nextInt();

        System.out.println ("p1 is currently "+ p1[i]);

        System.out.println ("p2 is currently "+ p2[i]);

        i++;
    }

}//End readScores

public boolean wins(int player1Score, int player2Score) throws IOException

{

    return true;

}//End wins
}
Was it helpful?

Solution

Take the declaration for these arrays outside of readScores() and put it at the top of your class so that you can invoke wins with them.

int []p1= new int[10];

int []p2= new int[10];

Change the signature of wins to this:

public void wins(int []player1Scores, int []player2Scores)
{
    int player1Wins = 0;
    int player2Wins = 0;

    for (int i = 0; i < 10; i++)
        (player1Scores > player2Scores) ? player1Wins++ : player2Wins++;

    System.out.printf("Player 1 wins: %-10i Player 2 wins %-10i", player1Wins, player2Wins);
}//End wins

I won't be replying to any comments on this because I'm super busy tonight, but good luck Charles.

OTHER TIPS

You already have a readScores function which reads in a list of the player 1 and player 2 scores. Based on that, you can easily go through those Arrays and determine which player won each game.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top