0 Replies - 1435 Views - Last Post: 06 December 2012 - 01:03 PM Rate Topic: -----

#1 joeskrodelis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-December 12

need an if statement for my game

Posted 06 December 2012 - 01:03 PM

i am writing a java game to create a fruit machine game but im having a few problems. my problem is that im trying to build a one armed bandit using Java but i carnt figure out the If statement that lets you get:-

3 shapes of the same type win £1
2 shapes of the same type win 50p
one particular shape, say a apple win 20p
. My images are also not spinning and sometimes the image will only display 2 images when it has been spun. Also my game will allow itself to start even when no money has been inserted into the machine. If you could help out and get back to me that would be a huge help.


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;

public class TheElectronicBandit extends JFrame implements ActionListener{
	private JButton btnAddcoin, btnSpin, btnBye, panelLeft, panelMiddle, panelRight, btnHold1, btnHold2, btnHold3;
	private JLabel lblLeft;
	private JTextField txtCoin, txtWinnings;
	private int num1;
	private Random random;
	private Icon iconPic1, iconPic2, iconPic3, iconPic4, iconPic5, iconPic6;
	private Timer timer1, timer2, timer3;
	private int ones, twos, threes, fours, fives, sixs, counter;

	public static void main(String[] args)
	{
		TheElectronicBandit TheElectronicBandit = new TheElectronicBandit();
	}
	
	public TheElectronicBandit()
	{
		timer1 = new Timer(200, this);
		timer2 = new Timer(200, this);
		timer3 = new Timer(200, this);
		JOptionPane.showMessageDialog(null,"Hello and welcome to the One Armed Bandit!!");
		num1 = 0;
		random = new Random();
		ones =0;twos=0;threes=0;fours=0;fives=0;sixs=0;  counter=0;
		setLayout(new FlowLayout());
		txtCoin = new JTextField(3);
		txtCoin.setFont(new Font("TimesRoman", Font.BOLD, 16));
		txtCoin.setBackground(Color.green);
		txtCoin.setForeground(Color.red);
		txtCoin.setText(num1 + " ");
		txtWinnings = new JTextField(30);
		txtWinnings.setFont(new Font("TimesRoman", Font.BOLD, 16));
		btnAddcoin = new JButton("Insert Coins");
		btnSpin = new JButton("Play");
		btnBye = new JButton("Bye!!");
		btnHold1 = new JButton("Hold");
		btnHold2 = new JButton("Hold");
		btnHold3 = new JButton("Hold");
		panelLeft = new JButton();
		panelLeft.setBackground(Color.green);
		panelLeft.setPreferredSize(new Dimension(100, 100));
		panelMiddle = new JButton();
		panelMiddle.setBackground(Color.green);
		panelMiddle.setPreferredSize(new Dimension(100, 100));
		panelRight = new JButton();
		panelRight.setBackground(Color.green);
		panelRight.setPreferredSize(new Dimension(100, 100));
		
	      setBackground(Color.green) ;
	      setLayout(new FlowLayout()) ;

                        add(txtCoin);
	    				add(btnAddcoin);
                        add(btnSpin);
                        add(panelLeft);
                        add(panelMiddle);
                        add(panelRight);
                        add(btnHold1);
                        add(btnHold2);
                        add(btnHold3);
                        add(txtWinnings);
                        add(btnBye);

		btnAddcoin.addActionListener(this);
		btnSpin.addActionListener(this);
		btnBye.addActionListener(this);
		
		iconPic1 = new ImageIcon("bin/iconPic1.png");
		iconPic2 = new ImageIcon("bin/iconPic2.png");
		iconPic3 = new ImageIcon("bin/iconPic3.png");
		iconPic4 = new ImageIcon("bin/iconPic4.png");
		iconPic5 = new ImageIcon("bin/iconPic5.png");
		iconPic6 = new ImageIcon("bin/iconPic6.png");
		
		setTitle("Simple GUI Demo. . . .");
		setSize(400, 300);
		setVisible(true);
		
		
	}
	
	public void actionPerformed(ActionEvent event){
		
		if (event.getSource()== btnAddcoin){
			txtCoin.setForeground(Color.black);
			num1 = num1 + 100;
			txtCoin.setText(num1 + " ");
		}
		if (event.getSource()== btnSpin){
			txtCoin.setForeground(Color.red);
			num1 = num1 - 25;
			txtCoin.setText(num1 + " ");
		}
		if (event.getSource()== btnBye){
			System.exit(0);
		}

		if (num1 >= 500){
			btnAddcoin.setEnabled(false);

		}
		if (num1 <= 0){
			btnAddcoin.setEnabled(true);

		}
		if (num1 >= 500){
			btnSpin.setEnabled(true);
		}
		if (num1 == 0){
			btnSpin.setEnabled(false);
		}			
		if (event.getSource()== btnBye){
			JOptionPane.showMessageDialog(null,"Bye!! :-) " );
			System.exit(0);
		}
			
		if (event.getSource()== btnSpin){
			counter = 0;
			timer1.start();
			timer2.start();
			timer3.start();
		}
		
		if (event.getSource()== timer1){
			int num;
			
			num = random.nextInt(6);
			switch (num){
			case 1:
				panelLeft.setIcon(iconPic1);
				ones = ones +1;
				counter =  counter + 1;
				break;
			case 2:
				panelLeft.setIcon(iconPic2);
				twos = twos + 1;
				counter =  counter + 1;
				break;
			case 3:
				panelLeft.setIcon(iconPic3);
				threes= threes +1;
				counter =  counter + 1;
				break;
			case 4:
				panelLeft.setIcon(iconPic4);
				fours = fours +1;
				counter =  counter + 1;
				break;
			case 5:
				panelLeft.setIcon(iconPic5);
				fives = fives +1;
				counter =  counter + 1;
				break;
			case 6:
				panelLeft.setIcon(iconPic6);
				sixs = sixs +1;
				counter =  counter + 1;
				break;
		}
			
		
		}
		if (event.getSource()== timer2){
			int num;
			
			num = random.nextInt(6);
			switch (num){
			case 1:
				panelMiddle.setIcon(iconPic1);
				ones = ones +1;
				counter =  counter + 1;
				break;
			case 2:
				panelMiddle.setIcon(iconPic2);
				twos = twos + 1;
				counter =  counter + 1;
				break;
			case 3:
				panelMiddle.setIcon(iconPic3);
				threes= threes +1;
				counter =  counter + 1;
				break;
			case 4:
				panelMiddle.setIcon(iconPic4);
				fours = fours +1;
				counter =  counter + 1;
				break;
			case 5:
				panelMiddle.setIcon(iconPic5);
				fives = fives +1;
				counter =  counter + 1;
				break;
			case 6:
				panelMiddle.setIcon(iconPic6);
				sixs = sixs +1;
				counter =  counter + 1;
				break;
		}
		}
		
		if (event.getSource()== timer3){
			int num;
			
			num = random.nextInt(6);
			switch (num){
			case 1:
				panelRight.setIcon(iconPic1);
				ones = ones +1;
				counter =  counter + 1;
				break;
			case 2:
				panelRight.setIcon(iconPic2);
				twos = twos + 1;
				counter =  counter + 1;
				break;
			case 3:
				panelRight.setIcon(iconPic3);
				threes= threes +1;
				counter =  counter + 1;
				break;
			case 4:
				panelRight.setIcon(iconPic4);
				fours = fours +1;
				counter =  counter + 1;
				break;
			case 5:
				panelRight.setIcon(iconPic5);
				fives = fives +1;
				counter =  counter + 1;
				break;
			case 6:
				panelRight.setIcon(iconPic6);
				sixs = sixs +1;
				counter =  counter + 1;
				break;
		}
			
		
		}
       	if (counter >20)
    		stopTimer();}
	
	
    public void stopTimer(){
    	timer1.stop();
    	timer2.stop();
    	timer3.stop();
    }
    
    	
    }

Attached image(s)

  • Attached Image
  • Attached Image
  • Attached Image
  • Attached Image
  • Attached Image
  • Attached Image
  • Attached Image
  • Attached Image

This post has been edited by macosxnerd101: 06 December 2012 - 01:05 PM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Page 1 of 1