Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For the below code i want to try and complicate the game slightly. Possibly add

ID: 3766942 • Letter: F

Question

For the below code i want to try and complicate the game slightly. Possibly add a virtual dartboard and have the program randomly score the games. I want to try to make it as close to a real game of darts as possible. I am unsure how to do this. Currently the user selects the number of players and rounds and has to manually enter each score. The program returns a winner and displays the scores for each player. How can i make this more intricate?

public class Darts extends JFrame
{
private JLabel spacer;

private JButton Darts2P5R;
private JButton Darts2P10R;
private JButton Darts3P5R;
private JButton Darts3P10R;

private JLabel Banner;
private JLabel TwoPlayers;
private JLabel ThreePlayers;

Scanner input = new Scanner (System.in);

// 5 Rounds 2 Players

int Player1Score1;
int Player1Score2;
int Player1Score3;
int Player1Score4;
int Player1Score5;
int Player2Score1;
int Player2Score2;
int Player2Score3;
int Player2Score4;
int Player2Score5;
int Player1Total;
int Player2Total;

// 10 Rounds 2 Players

int Player1Score1RD2;
int Player1Score2RD2;
int Player1Score3RD2;
int Player1Score4RD2;
int Player1Score5RD2;
int Player1Score6RD2;
int Player1Score7RD2;
int Player1Score8RD2;
int Player1Score9RD2;
int Player1Score10RD2;
int Player2Score1RD2;
int Player2Score2RD2;
int Player2Score3RD2;
int Player2Score4RD2;
int Player2Score5RD2;
int Player2Score6RD2;
int Player2Score7RD2;
int Player2Score8RD2;
int Player2Score9RD2;
int Player2Score10RD2;
int Player1TotalRD2;
int Player2TotalRD2;

// 5 Rounds 3 Players

int Player1Score1RD3;
int Player1Score2RD3;
int Player1Score3RD3;
int Player1Score4RD3;
int Player1Score5RD3;
int Player2Score1RD3;
int Player2Score2RD3;
int Player2Score3RD3;
int Player2Score4RD3;
int Player2Score5RD3;
int Player3Score1RD3;
int Player3Score2RD3;
int Player3Score3RD3;
int Player3Score4RD3;
int Player3Score5RD3;
int Player1TotalRD3;
int Player2TotalRD3;
int Player3TotalRD3;

// 10 Rounds 3 Players

int Player1Score1RD4;
int Player1Score2RD4;
int Player1Score3RD4;
int Player1Score4RD4;
int Player1Score5RD4;
int Player1Score6RD4;
int Player1Score7RD4;
int Player1Score8RD4;
int Player1Score9RD4;
int Player1Score10RD4;
int Player2Score1RD4;
int Player2Score2RD4;
int Player2Score3RD4;
int Player2Score4RD4;
int Player2Score5RD4;
int Player2Score6RD4;
int Player2Score7RD4;
int Player2Score8RD4;
int Player2Score9RD4;
int Player2Score10RD4;
int Player3Score1RD4;
int Player3Score2RD4;
int Player3Score3RD4;
int Player3Score4RD4;
int Player3Score5RD4;
int Player3Score6RD4;
int Player3Score7RD4;
int Player3Score8RD4;
int Player3Score9RD4;
int Player3Score10RD4;
int Player1TotalRD4;
int Player2TotalRD4;
int Player3TotalRD4;

public Darts()
{
  super("Darts");
  
  setLayout(new FlowLayout());
  
  Banner = new JLabel("Virtual Scoreboard: Darts");
  add(Banner);
  
  spacer = new JLabel(" ");
  add(spacer);
  
  TwoPlayers = new JLabel("Two Players");
  add(TwoPlayers);
  
  Darts2P5R = new JButton("5 Rounds");
  add(Darts2P5R);
  
  Darts2P10R = new JButton("10 Rounds");
  add(Darts2P10R);
  
  spacer = new JLabel(" ");
  add(spacer);
  
  ThreePlayers = new JLabel("Three Players");
  add(ThreePlayers);
  
  Darts3P5R = new JButton("5 Rounds");
  add(Darts3P5R);
  
  Darts3P10R = new JButton("10 Rounds");
  add(Darts3P10R);
  
  Darts2P5R.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    System.out.println("Two Player Dart Game: 5 Rounds ");
    
    System.out.println("[Player 1] Score 1: ");
    Player1Score1 = input.nextInt();
    System.out.println("[Player 2] Score 1: ");
    Player2Score1 = input.nextInt();
    
    System.out.println("[Player 1] Score 2: ");
    Player1Score2 = input.nextInt();
    System.out.println("[Player 2] Score 2: ");
    Player2Score2 = input.nextInt();
    
    System.out.println("[Player 1] Score 3: ");
    Player1Score3 = input.nextInt();
    System.out.println("[Player 2] Score 3: ");
    Player2Score3 = input.nextInt();
    
    System.out.println("[Player 1] Score 4: ");
    Player1Score4 = input.nextInt();
    System.out.println("[Player 2] Score 4: ");
    Player2Score4 = input.nextInt();
    
    System.out.println("[Player 1] Score 5: ");
    Player1Score5 = input.nextInt();
    System.out.println("[Player 2] Score 5: ");
    Player2Score5 = input.nextInt();
    
    Player1Total = Player1Score1 + Player1Score2 + Player1Score3 +
      Player1Score4 + Player1Score5;
    
    Player2Total = Player2Score1 + Player2Score2 + Player2Score3 +
      Player2Score4 + Player2Score5;
    
    if(Player1Total > Player2Total)
    {
     System.out.println(" Player 1 Won: Player 1 - " +
    Player1Total + ", Player 2 - " + Player2Total + ".");
    }
    
    else if(Player2Total > Player1Total)
    {
     System.out.println(" Player 2 Won: Player 1 - " +
    Player1Total + ", Player 2 - " + Player2Total + ".");
    }
    
    else
    {
     System.out.println(" Tie: Player 1 - " + Player1Total +
       ", Player 2 - " + Player2Total + ".");
    }
   }
  });
  
  Darts2P10R.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    System.out.println("Two Player Dart Game: 10 Rounds ");
    
    System.out.println("[Player 1] Score 1: ");
    Player1Score1RD2 = input.nextInt();
    System.out.println("[Player 2] Score 1: ");
    Player2Score1RD2 = input.nextInt();
    
    System.out.println("[Player 1] Score 2: ");
    Player1Score2RD2 = input.nextInt();
    System.out.println("[Player 2] Score 2: ");
    Player2Score2RD2 = input.nextInt();
    
    System.out.println("[Player 1] Score 3: ");
    Player1Score3RD2 = input.nextInt();
    System.out.println("[Player 2] Score 3: ");
    Player2Score3RD2 = input.nextInt();
    
    System.out.println("[Player 1] Score 4: ");
    Player1Score4RD2 = input.nextInt();
    System.out.println("[Player 2] Score 4: ");
    Player2Score4RD2 = input.nextInt();
    
    System.out.println("[Player 1] Score 5: ");
    Player1Score5RD2 = input.nextInt();
    System.out.println("[Player 2] Score 5: ");
    Player2Score5RD2 = input.nextInt();
    
    System.out.println("[Player 1] Score 6: ");
    Player1Score6RD2 = input.nextInt();
    System.out.println("[Player 2] Score 6: ");
    Player2Score6RD2 = input.nextInt();
    
    System.out.println("[Player 1] Score 7: ");
    Player1Score7RD2 = input.nextInt();
    System.out.println("[Player 2] Score 7: ");
    Player2Score7RD2 = input.nextInt();
    
    System.out.println("[Player 1] Score 8: ");
    Player1Score8RD2 = input.nextInt();
    System.out.println("[Player 2] Score 8: ");
    Player2Score8RD2 = input.nextInt();
    
    System.out.println("[Player 1] Score 9: ");
    Player1Score9RD2 = input.nextInt();
    System.out.println("[Player 2] Score 9: ");
    Player2Score9RD2 = input.nextInt();
    
    System.out.println("[Player 1] Score 10: ");
    Player1Score10RD2 = input.nextInt();
    System.out.println("[Player 2] Score 10: ");
    Player2Score10RD2 = input.nextInt();
    
    Player1TotalRD2 = Player1Score1RD2 + Player1Score2RD2 +
      Player1Score3RD2 + Player1Score4RD2 + Player1Score5RD2
      + Player1Score6RD2 + Player1Score7RD2 + Player1Score8RD2
      + Player1Score9RD2 + Player1Score10RD2;
    
    Player2TotalRD2 = Player2Score1RD2 + Player2Score2RD2 +
      Player2Score3RD2 + Player2Score4RD2 + Player2Score5RD2
      + Player2Score6RD2 + Player2Score7RD2 + Player2Score8RD2
      + Player2Score9RD2 + Player2Score10RD2;
    
    if(Player1TotalRD2 > Player2TotalRD2)
    {
     System.out.println(" Player 1 Won: Player 1 - " +
       Player1TotalRD2 + ", Player 2 - " + Player2TotalRD2 + ".");
    }
       
    else if(Player2TotalRD2 > Player1TotalRD2)
    {
     System.out.println(" Player 2 Won: Player 1 - " +
       Player1TotalRD2 + ", Player 2 - " + Player2TotalRD2 + ".");
    }
       
    else
    {
     System.out.println(" Tie: Player 1 - " + Player1TotalRD2 +
       ", Player 2 - " + Player2TotalRD2 + ".");
    }
   }
  });
  
  Darts3P5R.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    System.out.println("Three Player Dart Game: 5 Rounds ");
    
    System.out.println("[Player 1] Score 1: ");
    Player1Score1RD3 = input.nextInt();
    System.out.println("[Player 2] Score 1: ");
    Player2Score1RD3 = input.nextInt();
    System.out.println("[Player 3] Score 1: ");
    Player3Score1RD3 = input.nextInt();
    
    System.out.println("[Player 1] Score 2: ");
    Player1Score2RD3 = input.nextInt();
    System.out.println("[Player 2] Score 2: ");
    Player2Score2RD3 = input.nextInt();
    System.out.println("[Player 3] Score 2: ");
    Player3Score2RD3 = input.nextInt();
    
    System.out.println("[Player 1] Score 3: ");
    Player1Score3RD3 = input.nextInt();
    System.out.println("[Player 2] Score 3: ");
    Player2Score3RD3 = input.nextInt();
    System.out.println("[Player 3] Score 3: ");
    Player3Score3RD3 = input.nextInt();
    
    System.out.println("[Player 1] Score 4: ");
    Player1Score4RD3 = input.nextInt();
    System.out.println("[Player 2] Score 4: ");
    Player2Score4RD3 = input.nextInt();
    System.out.println("[Player 3] Score 4: ");
    Player3Score4RD3 = input.nextInt();
    
    System.out.println("[Player 1] Score 5: ");
    Player1Score5RD3 = input.nextInt();
    System.out.println("[Player 2] Score 5: ");
    Player2Score5RD3 = input.nextInt();
    System.out.println("[Player 3] Score 5: ");
    Player3Score5RD3 = input.nextInt();
    
    Player1TotalRD3 = Player1Score1RD3 + Player1Score2RD3 + Player1Score3RD3 +
      Player1Score4RD3 + Player1Score5RD3;
    
    Player2TotalRD3 = Player2Score1RD3 + Player2Score2RD3 + Player2Score3RD3 +
      Player2Score4RD3 + Player2Score5RD3;
    
    Player3TotalRD3 = Player3Score1RD3 + Player3Score2RD3 + Player3Score3RD3 +
      Player3Score4RD3 + Player3Score5RD3;
    
    if(Player1TotalRD3 > Player2TotalRD3 && Player1TotalRD3 > Player3TotalRD3)
    {
     System.out.println(" Player 1 Won: Player 1 -" +
       Player1TotalRD3 + ", Player 2 - " + Player2TotalRD3 + ", Player 3 - "
       + Player3TotalRD3 + ".");
    }
    
    else if(Player2TotalRD3 > Player1TotalRD3 && Player2TotalRD3 > Player3TotalRD3)
    {
     System.out.println(" Player 2 Won: Player 1 - " +
       Player1TotalRD3 + ", Player 2 - " + Player2TotalRD3 + ", Player 3 - " +
       Player3TotalRD3 + ".");
    }
    
    else if(Player3TotalRD3 > Player1TotalRD3 && Player3TotalRD3 > Player2TotalRD3)
    {
     System.out.println(" PLayer 3 Won: Player 1 - " +
       Player1TotalRD3 + ", Player 2 - " + Player2TotalRD3 + ", Player 3 - " +
       Player3TotalRD3 + ".");
    }
    
    else if(Player1TotalRD3 == Player2TotalRD3 || Player2TotalRD3 == Player1TotalRD3)
    {
           System.out.println(" Players 1 and 2 Tied: Player 1 - " + Player1TotalRD3 +
             ", Player 2 - " + Player2TotalRD3 + ", Player 3 - " + Player3TotalRD3
             + ".");
       }
    
    else if(Player3TotalRD3 == Player2TotalRD3 || Player2TotalRD3 == Player3TotalRD3)
    {
           System.out.println(" Players 2 and 3 Tied: Player 1 - " + Player1TotalRD3 +
             ", Player 2 - " + Player2TotalRD3 + ", Player 3 - " + Player3TotalRD3 +
             ".");
       }
    
    else if(Player1TotalRD3 == Player3TotalRD3 || Player3TotalRD3 == Player1TotalRD3)
    {
           System.out.println(" Players 1 and 3 Tied: Player 1 - " + Player1TotalRD3 +
             ", Player 2 - " + Player2TotalRD3 + ", Player 3 - " + Player3TotalRD3
             + ".");
       }
    
    else
    {
           System.out.println(" Tie: Player 1 - " + Player1TotalRD3 + ", Player 2 - "
             + Player2TotalRD3 + ", Player 3 - " + Player3TotalRD3 + ".");
    }
   }
  });
  
  Darts3P10R.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    System.out.println("Three Player Dart Game: 10 Rounds ");
    
    System.out.println("[Player 1] Score 1: ");
    Player1Score1RD4 = input.nextInt();
    System.out.println("[Player 2] Score 1: ");
    Player2Score1RD4 = input.nextInt();
    System.out.println("[Player 3] Score 1: ");
    Player3Score1RD4 = input.nextInt();
    
    System.out.println("[Player 1] Score 2: ");
    Player1Score2RD4 = input.nextInt();
    System.out.println("[Player 2] Score 2: ");
    Player2Score2RD4 = input.nextInt();
    System.out.println("[Player 3] Score 2: ");
    Player3Score2RD4 = input.nextInt();
    
    System.out.println("[Player 1] Score 3: ");
    Player1Score3RD4 = input.nextInt();
    System.out.println("[Player 2] Score 3: ");
    Player2Score3RD4 = input.nextInt();
    System.out.println("[Player 3] Score 3: ");
    Player3Score3RD4 = input.nextInt();
    
    System.out.println("[Player 1] Score 4: ");
    Player1Score4RD4 = input.nextInt();
    System.out.println("[Player 2] Score 4: ");
    Player2Score4RD4 = input.nextInt();
    System.out.println("[Player 3] Score 4: ");
    Player3Score4RD4 = input.nextInt();
    
    System.out.println("[Player 1] Score 5: ");
    Player1Score5RD4 = input.nextInt();
    System.out.println("[Player 2] Score 5: ");
    Player2Score5RD4 = input.nextInt();
    System.out.println("[Player 3] Score 5: ");
    Player3Score5RD4 = input.nextInt();
    
    System.out.println("[Player 1] Score 6: ");
    Player1Score1RD4 = input.nextInt();
    System.out.println("[Player 2] Score 6: ");
    Player2Score1RD4 = input.nextInt();
    System.out.println("[Player 3] Score 6: ");
    Player3Score1RD4 = input.nextInt();
    
    System.out.println("[Player 1] Score 7: ");
    Player1Score2RD4 = input.nextInt();
    System.out.println("[Player 2] Score 7: ");
    Player2Score2RD4 = input.nextInt();
    System.out.println("[Player 3] Score 7: ");
    Player3Score2RD4 = input.nextInt();
    
    System.out.println("[Player 1] Score 8: ");
    Player1Score3RD4 = input.nextInt();
    System.out.println("[Player 2] Score 8: ");
    Player2Score3RD4 = input.nextInt();
    System.out.println("[Player 3] Score 8: ");
    Player3Score3RD4 = input.nextInt();
    
    System.out.println("[Player 1] Score 9: ");
    Player1Score4RD4 = input.nextInt();
    System.out.println("[Player 2] Score 9: ");
    Player2Score4RD4 = input.nextInt();
    System.out.println("[Player 3] Score 9: ");
    Player3Score4RD4 = input.nextInt();
    
    System.out.println("[Player 1] Score 10: ");
    Player1Score5RD4 = input.nextInt();
    System.out.println("[Player 2] Score 10: ");
    Player2Score5RD4 = input.nextInt();
    System.out.println("[Player 3] Score 10: ");
    Player3Score5RD4 = input.nextInt();
    
    Player1TotalRD4 = Player1Score1RD4 + Player1Score2RD4 + Player1Score3RD4 +
      Player1Score4RD4 + Player1Score5RD4 + Player1Score6RD4 + Player1Score7RD4
      + Player1Score8RD4 + Player1Score9RD4 + Player1Score10RD4;
    
    Player2TotalRD4 = Player2Score1RD4 + Player2Score2RD4 + Player2Score3RD4 +
      Player2Score4RD4 + Player2Score5RD4 + Player2Score6RD4 + Player2Score7RD4
      + Player2Score8RD4 + Player2Score9RD4 + Player2Score10RD4;
    
    Player3TotalRD4 = Player3Score1RD4 + Player3Score2RD4 + Player3Score3RD4 +
      Player3Score4RD4 + Player3Score5RD4 + Player3Score6RD4 + Player3Score7RD4
      + Player3Score8RD4 + Player3Score9RD4 + Player3Score10RD4;
    
    if(Player1TotalRD4 > Player2TotalRD4 && Player1TotalRD4 > Player3TotalRD4)
    {
     System.out.println(" Player 1 Won: Player 1 - " +
       Player1TotalRD4 + ", Player 2 - " + Player2TotalRD4 + ", Player 3 - "
       + Player3TotalRD4 + ".");
    }
    
    else if(Player2TotalRD4 > Player1TotalRD4 && Player2TotalRD4 > Player3TotalRD4)
    {
     System.out.println(" Player 2 Won: Player 1 - " +
       Player1TotalRD4 + ", Player 2 - " + Player2TotalRD4 + ", Player 3 - " +
       Player3TotalRD4 + ".");
    }
    
    else if(Player3TotalRD4 > Player1TotalRD4 && Player3TotalRD4 > Player2TotalRD4)
    {
     System.out.println(" PLayer 3 Won: Player 1 - " +
       Player1TotalRD4 + ", Player 2 - " + Player2TotalRD4 + ", Player 3 - " +
       Player3TotalRD4 + ".");
    }
    
    else if(Player1TotalRD4 == Player2TotalRD4 || Player2TotalRD4 == Player1TotalRD4)
    {
           System.out.println(" Players 1 and 2 Tied: Player 1 - " + Player1TotalRD4 +
             ", Player 2 - " + Player2TotalRD4 + ", Player 3 - " + Player3TotalRD4
             + ".");
       }
    
    else if(Player3TotalRD4 == Player2TotalRD4 || Player2TotalRD4 == Player3TotalRD4)
    {
           System.out.println(" Players 2 and 3 Tied: Player 1 - " + Player1TotalRD4 +
             ", Player 2 - " + Player2TotalRD4 + ", Player 3 - " + Player3TotalRD4 +
             ".");
       }
    
    else if(Player1TotalRD4 == Player3TotalRD4 || Player3TotalRD4 == Player1TotalRD4)
    {
           System.out.println(" Players 1 and 3 Tied: Player 1 - " + Player1TotalRD4 +
             ", Player 2 - " + Player2TotalRD4 + ", Player 3 - " + Player3TotalRD4
             + ".");
       }
    
    else
    {
           System.out.println(" Tie: Player 1 - " + Player1TotalRD4 + ", Player 2 - "
             + Player2TotalRD4 + ", Player 3 - " + Player3TotalRD4 + ".");
    }
   }
  });
}
   public static void main(String args[])
   {
    Darts D = new Darts();
    D.setSize(175, 270);
    D.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    D.setVisible(true);
   }
}

Explanation / Answer

You can randomly select the number of players or number of games or both using the RandomGenerator Class. This class is instantiated by calling the getInstance method, instead of using the new operator. The getInstance method returns a single shared instance of a random generator.

For example:

RandomGenerator rg = RandomGenerator.getInstance();

Once the instance is created, you can either use the method

nextInt(int low, int high) that returns a random integer between low and high, inclusive.

You can also use the method:

int nextInt(int n) that returns a random int between 0 and n - 1.