I have made a fruit slot machine and I am Stuck at how to code to count when:
3 pictures match
two pictures match
wins
lost
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.Timer;
class Bandit extends JFrame implements ActionListener{
private int num1, num2, num3, counter;
private int matchThree, matchTwo, win, lost;
double money, prizeMoney;
private JButton btnSpinPictures,btnSpinPictures2,btnSpinPictures3,btnPlay, btnExit, btn£, btnCredit, btnCollect;
private JLabel lblCredit, lblWinnings,lblLost, lblMatchThree, lblMatchTwo,lblWon;
private JTextField txtCredit, txtWinnings;
private Timer Timer;
private Icon iconPic1, iconPic2, iconPic3, iconPic4, iconPic5, iconPic6, iconPic7, iconPic8;
private Random random;
public static void main(String[] args) {
Bandit spinApp = new Bandit();
spinApp.setVisible(true) ;
}
public Bandit(){
super("The Electronic Bandit!!!");
//iCons
iconPic1 = new ImageIcon("bin/1.gif");
iconPic2 = new ImageIcon("bin/2.gif");
iconPic3 = new ImageIcon("bin/3.gif");
iconPic4 = new ImageIcon("bin/4.gif");
026 iconPic5 = new ImageIcon("bin/5.gif");
iconPic6 = new ImageIcon("bin/6.gif");
iconPic7 = new ImageIcon("bin/7.gif");
iconPic8 = new ImageIcon("bin/8.gif");
Timer = new Timer(100, this);
random = new Random ();
btnSpinPictures = new JButton("");
btnSpinPictures.setPreferredSize(new Dimension(200, 200)) ;
btnSpinPictures.setBackground(Color.white );
btnSpinPictures2 = new JButton("");
btnSpinPictures2.setPreferredSize(new Dimension(200, 200)) ;
btnSpinPictures2.setBackground(Color.white );
btnSpinPictures3 = new JButton("");
btnSpinPictures3.setPreferredSize(new Dimension(200, 200)) ;
btnSpinPictures3.setBackground(Color.white );
btnExit = new JButton("Leaving?");
btnExit.setBackground(Color.white);
btnPlay = new JButton("Spin");
btnPlay.setBackground(Color.white);
btnPlay.setEnabled(false);
btnCredit = new JButton("Credit Winnings to Playing Money");
btnCollect = new JButton("Collect Winnings");
btn£ = new JButton ("Add £1");
lblCredit = new JLabel ("Credit");
lblCredit.setPreferredSize(new Dimension(100, 50)) ;
lblCredit.setBackground(Color.white);
lblWinnings = new JLabel ("Winnings");
lblWinnings.setPreferredSize(new Dimension(100, 50)) ;
lblWinnings.setBackground(Color.white);
lblMatchTwo = new JLabel();
lblMatchTwo.setText("Matched Two: ");
lblMatchThree = new JLabel();
lblMatchThree.setText("Matched Three: ");
lblWon = new JLabel();
lblWon.setText("Won: ");
lblLost = new JLabel();
lblLost.setText("Lost: ");
txtCredit = new JTextField (5);
txtCredit.setFont(new Font("Arial",Font.BOLD,16));
txtCredit.setForeground(Color.red);
txtCredit.setText(money + " ");
txtWinnings = new JTextField (4);
txtWinnings.setFont(new Font("Arial",Font.BOLD,16));
txtWinnings.setForeground(Color.green);
txtWinnings.setText(prizeMoney + " ");
//Add Buttons
add(btnSpinPictures);add(btnSpinPictures2);add(btnSpinPictures3) ;
add(btnPlay);add(btnExit);add(btn£);add(btnCredit);add(btnCollect);
add(lblCredit);add(txtCredit);add(lblWinnings);add(txtWinnings);
//Actions
btnPlay.addActionListener(this) ;
btnExit.addActionListener(this) ;
btn£.addActionListener(this);
btnCredit.addActionListener(this);
btnCollect.addActionListener(this);
//Window
setBackground(Color.cyan) ;
setLayout(new FlowLayout()) ;
setSize(850,400);
setVisible(true);
}
//Button Actions
public void actionPerformed( ActionEvent e){
//Quit Program
if (e.getSource() == btnExit){
JOptionPane.showMessageDialog(null,"Thank You For Playing");
System.exit(0);
}
//Add£
if (e.getSource()== btn£ ){
txtCredit.setForeground(Color.red);
money = money + 100;
txtCredit.setText(money +" ");
}
//Take credit away
if (e.getSource() == btnPlay ){
money = money - 20;
txtCredit.setText(money + " ");
}
//Add Winnings to credit
if (e.getSource() == btnCredit ){
money = money + prizeMoney;
JOptionPane.showMessageDialog(null,"You Have turn " + prizeMoney + " to credit");
prizeMoney = 0;
txtCredit.setText(money + " ");
txtWinnings.setText(prizeMoney + "");
}
//Collect Winnings
if (e.getSource() == btnCollect ){
JOptionPane.showMessageDialog(null,"You Have Collected " + prizeMoney);
prizeMoney = 0;
txtWinnings.setText (prizeMoney + " ");
}
//No play
if (money <20){
btnPlay.setEnabled(false);
}
//play
if (money >0){
btnPlay.setEnabled(true);
//creditLimit
if (money >=500){
btn£.setEnabled(false);
}
if (money <=480){
btn£.setEnabled(true);
}
//Assign timers to case
if (e.getSource()== Timer){
setIcon();
}
if (e.getSource()== Timer){
setIcon2();
}
if (e.getSource()== Timer){
setIcon3();
}
//Play Bandit
if (e.getSource() == btnSpin ){
counter = 0;
Timer.start();
} }
}
private void stopTimer(){
Timer.stop();
//Winnings
if (num1==num2 && num2==num3){
prizeMoney = prizeMoney + 100;
txtWinnings.setText(prizeMoney+ " ");
JOptionPane.showMessageDialog(null,"You Have Won 100");}
else if (num1==num2 || num2 == num3 || num1 == num3 ) {
prizeMoney = prizeMoney + 50;
txtWinnings.setText(prizeMoney + " ");
JOptionPane.showMessageDialog(null,"You Have Won 50" );}
else if (num1 == 7 || num2 == 7 || num3 == 7){
prizeMoney = prizeMoney + 20;
txtWinnings.setText(prizeMoney + " ");
JOptionPane.showMessageDialog(null,"You Have Won 20" );}
}
public void matchCheck() {
if (num1 == num2 && num2 == num3) {
lblMatchThree.setText("Matched Three: "+matchThree());;
} else if (num1 == num2 || num1 == num3) {
lblMatchTwo.setText("Matched Two: "+matchTwo());
} else if (num2 == num3) {
lblMatchTwo.setText("Matched Two: "+matchTwo());
} else {
lblLost.setText("Lost: "+lose());
}
lblWon.setText("Wins: "+win());
}
public int matchThree() {
++matchThree;
return matchThree;
}
/** Increments matchTwo by 1 and returns value. */
public int matchTwo() {
++matchTwo;
return matchTwo;
}
/** Increments lost by 1 and returns value. */
public int lose() {
++lost;
return lost;
}
/** Increments win by 1, increases progress bar and returns value. */
public int win() {
win = matchThree + matchTwo;
return win;
}
//spin pics
private void setIcon(){
num1 = random.nextInt(8);
switch (num1){
case 0:
btnSpinPictures.setIcon(iconPic1);
counter = counter + 1;
break;
case 1:
btnSpinPictures.setIcon(iconPic2);
counter = counter + 1;
break;
case 2:
btnSpinPictures.setIcon(iconPic3);
counter = counter + 1;
break;
case 3:
btnSpinPictures.setIcon(iconPic4);
counter = counter + 1;
break;
case 4:
btnSpinPictures.setIcon(iconPic5);
counter = counter + 1;
break;
case 5:
btnSpinPictures.setIcon(iconPic6);
counter = counter + 1;
break;
case 6:
btnSpinPictures.setIcon(iconPic7);
counter = counter + 1;
break;
case 7:
btnSpinPictures.setIcon(iconPic8);
counter = counter + 1;
break;
}
}
private void setIcon2(){
num2 = random.nextInt(8);
switch (num2){
case 0:
btnSpinPictures2.setIcon(iconPic1);
counter = counter + 1;
break;
case 1:
btnSpinPictures2.setIcon(iconPic2);
counter = counter + 1;
break;
case 2:
btnSpinPictures2.setIcon(iconPic3);
counter = counter + 1;
break;
case 3:
btnSpinPictures2.setIcon(iconPic4);
counter = counter + 1;
break;
case 4:
btnSpinPictures2.setIcon(iconPic5);
counter = counter + 1;
break;
case 5:
btnSpinPictures2.setIcon(iconPic6);
counter = counter + 1;
break;
case 6:
btnSpinPictures2.setIcon(iconPic7);
counter = counter + 1;
break;
case 7:
btnSpinPictures2.setIcon(iconPic8);
counter = counter + 1;
break;
}
}
private void setIcon3(){
num3 = random.nextInt(8);
switch (num3){
case 0:
btnSpinPictures3.setIcon(iconPic1);
counter = counter + 1;
break;
case 1:
btnSpinPictures3.setIcon(iconPic2);
counter = counter + 1;
break;
case 2:
btnSpinPictures3.setIcon(iconPic3);
counter = counter + 1;
break;
case 3:
btnSpinPictures3.setIcon(iconPic4);
counter = counter + 1;
break;
case 4:
btnSpinPictures3.setIcon(iconPic5);
counter = counter + 1;
break;
case 5:
btnSpinPictures3.setIcon(iconPic6);
counter = counter + 1;
break;
case 6:
btnSpinPictures3.setIcon(iconPic7);
counter = counter + 1;
break;
case 7:
btnSpinPictures3.setIcon(iconPic8);
counter = counter + 1;
break;
}
{if (counter >=100);
stopTimer();
} }}
Mod edit - fixed code tags

New Topic/Question
Reply



MultiQuote



|