0

https://i.stack.imgur.com/5bPS6.jpg

So pretty much i've been trying to program this slot machine game in java and i've been recently having issues with the method that i'm trying to approach this solution. The game works as follows: everytime you click on the label named x1 you invest 1 bitcoin and have a higher chance to win and win 1 bitcoin. clicking on label x3 you get to play with a medium chance to win(you win 3 bitcoin), and if label x5 is clicked you'll get the lowest chance of winning 5 bitcoin. 1 click on those labels enables the button "GO WIN!", which later on you have to click in order everything to run. At the moment I have no issue with programming it to make you win only 1 bitcoin with a certain chance, although the other two labels are the issue. any advice?

The jframe looks bad I know, although this is just practice for me :)

Code so far:

   // TODO add your handling code here:
    btnWinLose.setEnabled(false);
    int slot1 = (int)(4*Math.random()+0);
    int slot2 = (int)(4*Math.random()+0); 
    int slot3 = (int)(4*Math.random()+0);

    lbl_1.setText(String.valueOf(slot1));
    lbl_2.setText(String.valueOf(slot2));
    lbl_3.setText(String.valueOf(slot3));

    if(slot1 == slot2 ){
        start = start +1;
        lbl_Coin.setText(start + " COINS");
    }
    else{
    start = start - 1;
    lbl_Coin.setText(start + " COINS");
    }
    if(start == 0){
        int n = JOptionPane.showConfirmDialog(
                this, 
                "No coins left, start again?"+  
                JOptionPane.YES_NO_OPTION);
     if(n == 0){
         start=10;
         lbl_Coin.setText(String.valueOf(start + " COINS"));

     }
     else{
         System.exit(0);
     }
    }   
  • What exactly is the problem with the other labels? Are you struggling to identify which is clicked – TM00 Mar 01 '18 at 20:37
  • I'm not struggling with which is clicked, I'm struggling how to program the other labels to do what i need them to do. For example: label x3 clicked -> makes the button "GO WIN!" enabled -> click the button -> program goes to actionperformed -> takes out 1 coin from the account -> randomizes 3 different numbers between 0-9 etc. the rest is shown here above. Now the issue is that my program only takes out 1 coin from the account when pressing any of the labels x1,x3 and x5. I need it to do something else everytime i click on a different label after the button is clicked. – John Smith Mar 01 '18 at 20:56
  • more clarification: x3 clicked, actionperformed now knows it shall remove 3 bitcoin from account and if won, account will recieve 9. The chances of winning are lowered – John Smith Mar 01 '18 at 21:00
  • if you want I can edit the post and share my entire code. Now providing that I have given you the instructions of how the program is meant to function, I can now clarify yet again: I have no idea how to program it when there are 3 other labels at play and each on gives another outcome after picking a label and then clicking a button – John Smith Mar 01 '18 at 21:05
  • Yes that is exactly what I want it to do – John Smith Mar 01 '18 at 21:29
  • If you have a int choice =0; If you enter labelx1 choice = 1, labelx2 choice =2 etc and in action performed you use the choicevalue for the dofferent calculations – bajen micke Mar 01 '18 at 21:30

1 Answers1

0

One way you can determine which label has been clicked is by using a MouseListener on each label. See this question as reference. So make your GUI class implement MouseListener and do the following:

public class myGUI extends JFrame implements ActionListener, MouseListener{

 private JLabel x1,x2,x3;

 public myGUI(){
   // do constructor stuff
   x1 = new JLabel("x1");
   x2 = new JLabel("x2");
   x3 = new JLabel("x3");

  x1.addMouseListener(this);
  x2.addMouseListener(this);
  x3.addMouseListener(this);

 }

  // other methods

  @Override
  public void mouseClicked(MouseEvent e) {
        if(e.getSource().equals(x1)){
          // do x1 stuff
        }
        else if(e.getSource().equals(x2)){
          // do x2 stuff
        }
        else if(e.getSource().equals(x3)){
          // do x3 stuff
        }

  }

}
TM00
  • 1,330
  • 1
  • 14
  • 26