Java coin toss score problem (score is multiplied instead of incremented)

92 Views Asked by At

My score in my JAVA coin toss program is multiplied instead of incremented and i have no idea why it's happening. I also know that i have some redundant code in this thing (ex. buttonsPanel.add(button2); buttonsPanel.add(button); this.add(buttonsPanel); titlePanel.add(titleText); this.add(titlePanel); this.add(player_turn); this.add(playerOneScore); this.add(playerTwoScore); I will solve it after i find the solution to my score problem. )

Here is my code :

` Random random = new Random();

  int player1Score = 0;
    
  int player2Score = 0;


 JPanel buttonsPanel; 
 JButton button;
 JButton button2;

 JPanel titlePanel;
 JLabel titleText;
 JLabel player_turn;
 JLabel playerOneScore;
 JLabel playerTwoScore;
 


CoinToss(){
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Coin Toss test");
    this.setSize(600,600);
    this.setLayout(null);
    this.getContentPane().setBackground(Color.black);
    
    
    titlePanel = new JPanel();
    titlePanel.setBackground(Color.blue);
    titlePanel.setBounds(200, 10, 200, 40);
    
    titleText = new JLabel();
    titleText.setBackground(Color.blue);
    titleText.setForeground(Color.yellow);
    titleText.setFont(new Font("Ink Free",Font.ITALIC,25));
    titleText.setHorizontalAlignment(JLabel.CENTER);
    titleText.setText("Coin Toss Game");
    titleText.setOpaque(true);
    
    player_turn = new JLabel();
    player_turn.setBounds(225, 200, 150, 50);
    player_turn.setBackground(Color.blue);
    player_turn.setForeground(Color.yellow);
    player_turn.setFont(new Font("Ink Free",Font.ITALIC,25));
    player_turn.setHorizontalAlignment(JLabel.CENTER);
    player_turn.setText("Player1 turn");
    player_turn.setOpaque(true);
    
    
    
    
    
    buttonsPanel = new JPanel();
    buttonsPanel.setBounds(200, 400, 200, 50);
    buttonsPanel.setBackground(Color.blue);
    
    
    button = new JButton("Tails");
    button.setFocusable(false);
    button.addActionListener(this);
    button.setFont(new Font("Ink Free",Font.ITALIC,25));
    button.setForeground(Color.green);
    
    button2 = new JButton("Heads");
    button2.addActionListener(this);
    button2.setFocusable(false);
    button2.setFont(new Font("Ink Free",Font.ITALIC,25));
    button2.setForeground(Color.green);
    
    playerOneScore = new JLabel();
    playerOneScore.setBounds(50, 270, 210, 50);
    playerOneScore.setBackground(Color.blue);
    playerOneScore.setText("Player 1 score is : "+player1Score);
    playerOneScore.setFont(new Font("Ink Free",Font.ITALIC,25));
    playerOneScore.setOpaque(true);
    
    
    playerTwoScore = new JLabel();
    playerTwoScore.setBounds(350, 270, 210, 50);
    playerTwoScore.setBackground(Color.blue);
    playerTwoScore.setText("Player 2 score is : "+player2Score);
    playerTwoScore.setFont(new Font("Ink Free",Font.ITALIC,25));
    playerTwoScore.setOpaque(true);
    
    
    buttonsPanel.add(button2);
    buttonsPanel.add(button);
    this.add(buttonsPanel);
    titlePanel.add(titleText);
    this.add(titlePanel);
    this.add(player_turn);
    this.add(playerOneScore);
    this.add(playerTwoScore);
    this.setVisible(true);
}








@Override
public void actionPerformed(ActionEvent e) {
    
    int result = toss_coin(random);
    
    if(e.getSource() == button2 ) {
        if(result == 0) {
            
            player1CorrectGuess();
            updatePLayerTurn();
        }
        else {
            
            player2IncorrectGuess(result);
            updatePLayerTurn2();
        }
    }
    
    else if(e.getSource() == button) {
        if(result == 1) {
            
            player1CorrectGuess();
            updatePLayerTurn2();
        }
        else {
            
            player2IncorrectGuess(result);
            updatePLayerTurn();
        }
    }
    
    WinRate();
    
}




static int toss_coin(Random random) {
    
     int result = random.nextInt(2);
     
     return result;
     
    }





private void player1CorrectGuess() {
    JOptionPane.showMessageDialog(null, "Correct. It's heads");
    player1Score++;
    playerOneScore.setText("Player 1 score is : "+player1Score);
}

private void player2IncorrectGuess(int result) {
    JOptionPane.showMessageDialog(null, "Incorrect, the result was : " + (result == 0 ? "heads" : "tails") + ".");
    player2Score++;
    playerTwoScore.setText("Player 2 score is : "+player1Score);
}

private void updatePLayerTurn() {
    player_turn.setText("Player2 turn");
}

private void updatePLayerTurn2() {
    player_turn.setText("Player1 turn");
}

private void WinRate() {
    
    
    
    int totalNumberOfSimulations = 20;
    
     int numberOfWinsForPlayer1 = 0;
     
     int numberOfWinsForPlayer2 = 0;
    

      int maxScore = 10;
    
      boolean turn_start = true;
    
    for(int i=0;i<totalNumberOfSimulations;i++) {
        player1Score = 0;
        player2Score = 0;
        
        while(player1Score < maxScore && player2Score < maxScore) {
            
            int result = toss_coin(random);
            
            if(turn_start) {
                if(result == 0) {
                    player1Score++;
                }
                turn_start = false;
            }
            
            else {
                if(result == 1) {
                    player2Score++;
                }
                turn_start = true;
            }
            
        if (player1Score == maxScore) {
            numberOfWinsForPlayer1++;
        }
        else if(player2Score == maxScore) {
            numberOfWinsForPlayer2++;
        }
              
        if (player1Score == maxScore && player2Score == maxScore) {
            JOptionPane.showMessageDialog(null, "It's a tie, nobody wins.");
        }
            
        }
        
        
    }
    
    int winningChances = (numberOfWinsForPlayer1 * 100) / totalNumberOfSimulations;
    int winningChances2 = (numberOfWinsForPlayer2 * 100) / totalNumberOfSimulations;
    
    
    JOptionPane.showMessageDialog(null, "Player 1 winning chances: " + winningChances + "%"+"\nPlayer 2 winning chances: "+ winningChances2+ "%"); ;
    
    
    
    //System.out.println("Player 1 winning chances: " + winningChances + "%");
    //System.out.println("Player 2 winning chances: " + winningChances2 + "%");

}`

I tried getting help from chat GPT but that thing confused me even more.

0

There are 0 best solutions below