3 Replies - 1249 Views - Last Post: 29 March 2012 - 07:22 PM Rate Topic: -----

#1 LeggsofMacgyver  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-March 12

problems with slot machine program

Posted 29 March 2012 - 03:44 PM

I have made the slot machine and got the pictures to randomize after each pull, but for some reason I cannot get my supplier class Bandit to count how much money was spent, won, and the gross amount. Any hints as how to get it to work?
public class Bandit
{
  private Rectangle Slots, Redbox;
  private double spent, addSpent, holdWinning, Result, winnings;
  private Image firstSlot, secondSlot, thirdSlot, Bar, Seven, Apple, Orange, Grape, Cherries;
  private int setPic, setPic2, setPic3;
  private JLabel  spending, resulting, winning;
    public Bandit(int x, int y, JFrame w){
        Slots = new Rectangle(x, y, 270,150);
        Redbox = new Rectangle(5,5, 260,92);
        Redbox.setBackground(Color.red);
        Slots.add(Redbox,0); 
        JLabel spending = new JLabel("Spent: $" + spent);
        spending.setForeground(Color.yellow);
        spending.setBounds(15, 100,150,30);
        Slots.add(spending, 0);
        JLabel resulting = new JLabel("Result: $" + (winnings - spent));
        resulting.setBounds(50,120,150,30);
        resulting.setForeground(Color.white);
        Slots.add(resulting,0);
        JLabel winning = new JLabel("winning: $" + winnings);
        winning.setBounds(100, 100, 150, 30);
        winning.setForeground(Color.yellow);
        Slots.add(winning, 0);
        firstSlot = new Image(10,10,72,72, "images/BAR.png"); 
        Redbox.add(firstSlot,0);
        secondSlot = new Image(96,10,72,72, "images/seven.png");
        Redbox.add(secondSlot, 0);
        thirdSlot = new Image(168,10,72,72, "images/seven.png");
        Redbox.add(thirdSlot, 0);
        Redbox.repaint();
        w.add(Slots, 0);
        Slots.repaint();
    }

   
    public void Roll(){
        double randomPic, randomPic2, randomPic3;
        randomPic = (int) (Math.random() * 100);
        randomPic2 = (int) (Math.random() * 100);
        randomPic3 = (int) (Math.random() * 100);
        
        if(randomPic < 13){
          firstSlot.setImage("images/BAR.png");
          setPic = 1;  
        } else if(13 < randomPic && randomPic < 29 ){
            firstSlot.setImage( "images/seven.png");
            setPic = 2;
        } else if(29 < randomPic && randomPic < 46){
            firstSlot.setImage( "images/grapes.gif");
            setPic = 3;
        }else if(46 < randomPic && randomPic < 63){
            firstSlot.setImage( "images/apple.jpg");
            setPic = 4;
        }else if(63 < randomPic && randomPic < 80){
            firstSlot.setImage( "images/orange.gif");
            setPic = 5;
        }else if(80 < randomPic){
            firstSlot.setImage( "image/cherries.png");
            setPic = 6; 
        }
        if(randomPic2 < 13){
          secondSlot.setImage("images/BAR.png");
          setPic2 = 1;  
        } else if(13 < randomPic2 && randomPic2 < 29 ){
            secondSlot.setImage( "images/seven.png");
            setPic2 = 2;
        } else if(29 < randomPic2 && randomPic2 < 46){
            secondSlot.setImage( "images/grapes.gif");
            setPic2 = 3;
        }else if(46 < randomPic2 && randomPic2 < 63){
            secondSlot.setImage( "images/apple.jpg");
            setPic2 = 4;
        }else if(63 < randomPic2 && randomPic2 < 80){
            secondSlot.setImage( "images/orange.gif");
            setPic2 = 5;
        }else if(80 < randomPic2){
            secondSlot.setImage( "image/cherries.png");
            setPic2 = 6; 
        }
        if(randomPic3 < 13){
          thirdSlot.setImage( "images/BAR.png");
          setPic3 = 1;  
        } else if(13 < randomPic3 && randomPic3 < 29 ){
            thirdSlot.setImage("images/seven.png");
            setPic3 = 2;
        } else if(29 < randomPic3 && randomPic3 < 46){
            thirdSlot.setImage("images/grapes.gif");
            setPic3 = 3;
        }else if(46 < randomPic3 && randomPic3 < 63){
            thirdSlot.setImage("images/apple.jpg");
            setPic3 = 4;
        }else if(63 < randomPic3 && randomPic3 < 80){
            thirdSlot.setImage( "images/orange.gif");
            setPic3 = 5;
        }else if(80 < randomPic3){
            thirdSlot.setImage("image/cherries.png");
            setPic3 = 6; 
        }
        SpentCounter();
        winner();
    }
    public void SpentCounter(){
        spent = 0 + (.25 * addSpent);
    }
    
    public void winner(){
        if(setPic == 1 && setPic2 == 1 && setPic3 == 1){
            winnings = .25 * 100;
        }else if(setPic == 2 && setPic2 == 2 && setPic3 == 2){
            winnings = .25 * 20;
        }else if(setPic == 3 && setPic2 == 3 && setPic3 == 3){
            winnings = .25 * 10;
        }else if(setPic == 4 && setPic2 == 4 && setPic3 == 4){
            winnings = .25 * 10;
        }else if(setPic == 5 && setPic2 == 5 && setPic3 == 5){
            winnings = .25 * 8;
        }else if(setPic == 6 && setPic2 == 6 && setPic3 == 6){
            winnings = .25 * 8;
        }else if(setPic == 2 && setPic2 == 2 && setPic3 == 1){
            winnings = .25 * 5;
        }else if(setPic == 2 && setPic2 == 1 && setPic3 == 2){
            winnings = .25 * 5;
        }else if(setPic == 1 && setPic2 == 2 && setPic3 == 2){
            winnings = .25 * 5;
        }else if(setPic == 3 && setPic2 == 3 && setPic3 == 1){
            winnings = .25 * 5;
        }else if(setPic == 3 && setPic2 == 1 && setPic3 == 3){
            winnings = .25 * 5;
        }else if(setPic == 1 && setPic2 == 3 && setPic3 == 3){
            winnings = .25 * 5;
        }else if(setPic == 5 && setPic2 == 5){
            winnings = .25 * 2;
        }else if(setPic == 5 && setPic3 == 5){
            winnings = .25 * 2;
        }else if(setPic2 == 5 && setPic3 == 5){
            winnings = .25 * 2;
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: problems with slot machine program

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4189
  • View blog
  • Posts: 11,863
  • Joined: 18-April 07

Re: problems with slot machine program

Posted 29 March 2012 - 03:53 PM

You can't be showing things like winnings or spent in your class' constructor, that is called when you create the object. You haven't even called "Roll" yet where those values are set.

Your constructor sets up your labels and default values, then once your object is created you call "Roll" on it which will then set your values and then AFTER that is when you can show the values of winnings, spent etc.

Your spent is the value entered by the user as their wager before the Roll(), your winnings are what you calculate based on the wager and the result of the wheels (you appear to do that fine) and the total gross is just the current balance (after spent is removed) plus this roll's winnings.

So remove some of that stuff out of your constructor. Create a function that shows the values of winnings/spent/total and make sure you call that function after Roll() is finished executing (in other words, call it at the end of the Roll() function).

:)

This post has been edited by Martyr2: 29 March 2012 - 03:54 PM

Was This Post Helpful? 0
  • +
  • -

#3 LeggsofMacgyver  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-March 12

Re: problems with slot machine program

Posted 29 March 2012 - 05:05 PM

I'm sorry for the confusion but bandit is my supplier class, not my main driver class.

import javax.swing.*;
import java.awt.*;
public class Driver extends ThreeButtons { 
    private ThreeButtonFrame win;
    private Bandit SlotMachine1, SlotMachine2;
    
    public Driver()   {
        win = new ThreeButtonFrame( "Three Armed Bandit" );
        SlotMachine1 = new Bandit(50,50,win);
        SlotMachine2 = new Bandit(310,250,win);
        
    }
    
    public void leftAction() {
        SlotMachine1.Roll();
        win.repaint();
    }
    
    public void midAction() {
    }
    
    public void rightAction() {
        SlotMachine2.Roll();
        win.repaint();
    }
} 


Along with my Bandit supplier class.

import javax.swing.*;
import java.awt.*;

public class Bandit
{
  private Rectangle Slots, Redbox;
  private double spent, addSpent, holdWinning, Result, winnings;
  private Image firstSlot, secondSlot, thirdSlot, Bar, Seven, Apple, Orange, Grape, Cherries;
  private int setPic, setPic2, setPic3;
  private JLabel  spending, resulting, winning;
    public Bandit(int x, int y, JFrame w){
        Slots = new Rectangle(x, y, 270,150);
        Redbox = new Rectangle(5,5, 260,92);
        Redbox.setBackground(Color.red);
        Slots.add(Redbox,0); 
        JLabel spending = new JLabel("Spent: $" + spent);
        spending.setForeground(Color.yellow);
        spending.setBounds(15, 100,150,30);
        Slots.add(spending, 0);
        JLabel resulting = new JLabel("Result: $" + (winnings - spent));
        resulting.setBounds(50,120,150,30);
        resulting.setForeground(Color.white);
        Slots.add(resulting,0);
        JLabel winning = new JLabel("winning: $" + winnings);
        winning.setBounds(100, 100, 150, 30);
        winning.setForeground(Color.yellow);
        Slots.add(winning, 0);
        firstSlot = new Image(10,10,72,72, "images/BAR.png"); 
        Redbox.add(firstSlot,0);
        secondSlot = new Image(96,10,72,72, "images/seven.png");
        Redbox.add(secondSlot, 0);
        thirdSlot = new Image(168,10,72,72, "images/seven.png");
        Redbox.add(thirdSlot, 0);
        Redbox.repaint();
        w.add(Slots, 0);
        Slots.repaint();
    }

   
    public void Roll(){
        double randomPic, randomPic2, randomPic3;
        randomPic = (int) (Math.random() * 100);
        randomPic2 = (int) (Math.random() * 100);
        randomPic3 = (int) (Math.random() * 100);
        
        if(randomPic < 13){
          firstSlot.setImage("images/BAR.png");
          setPic = 1;  
        } else if(13 < randomPic && randomPic < 29 ){
            firstSlot.setImage( "images/seven.png");
            setPic = 2;
        } else if(29 < randomPic && randomPic < 46){
            firstSlot.setImage( "images/grapes.gif");
            setPic = 3;
        }else if(46 < randomPic && randomPic < 63){
            firstSlot.setImage( "images/apple.jpg");
            setPic = 4;
        }else if(63 < randomPic && randomPic < 80){
            firstSlot.setImage( "images/orange.gif");
            setPic = 5;
        }else if(80 < randomPic){
            firstSlot.setImage( "image/cherries.png");
            setPic = 6; 
        }
        if(randomPic2 < 13){
          secondSlot.setImage("images/BAR.png");
          setPic2 = 1;  
        } else if(13 < randomPic2 && randomPic2 < 29 ){
            secondSlot.setImage( "images/seven.png");
            setPic2 = 2;
        } else if(29 < randomPic2 && randomPic2 < 46){
            secondSlot.setImage( "images/grapes.gif");
            setPic2 = 3;
        }else if(46 < randomPic2 && randomPic2 < 63){
            secondSlot.setImage( "images/apple.jpg");
            setPic2 = 4;
        }else if(63 < randomPic2 && randomPic2 < 80){
            secondSlot.setImage( "images/orange.gif");
            setPic2 = 5;
        }else if(80 < randomPic2){
            secondSlot.setImage( "image/cherries.png");
            setPic2 = 6; 
        }
        if(randomPic3 < 13){
          thirdSlot.setImage( "images/BAR.png");
          setPic3 = 1;  
        } else if(13 < randomPic3 && randomPic3 < 29 ){
            thirdSlot.setImage("images/seven.png");
            setPic3 = 2;
        } else if(29 < randomPic3 && randomPic3 < 46){
            thirdSlot.setImage("images/grapes.gif");
            setPic3 = 3;
        }else if(46 < randomPic3 && randomPic3 < 63){
            thirdSlot.setImage("images/apple.jpg");
            setPic3 = 4;
        }else if(63 < randomPic3 && randomPic3 < 80){
            thirdSlot.setImage( "images/orange.gif");
            setPic3 = 5;
        }else if(80 < randomPic3){
            thirdSlot.setImage("image/cherries.png");
            setPic3 = 6; 
        }
        SpentCounter();
        winner();
    }
    public void SpentCounter(){
        addspent = addspent++
        spent = 0 + (.25 * addSpent);
    }
    
    public void winner(){
        if(setPic == 1 && setPic2 == 1 && setPic3 == 1){
            winnings = .25 * 100;
        }else if(setPic == 2 && setPic2 == 2 && setPic3 == 2){
            winnings = .25 * 20;
        }else if(setPic == 3 && setPic2 == 3 && setPic3 == 3){
            winnings = .25 * 10;
        }else if(setPic == 4 && setPic2 == 4 && setPic3 == 4){
            winnings = .25 * 10;
        }else if(setPic == 5 && setPic2 == 5 && setPic3 == 5){
            winnings = .25 * 8;
        }else if(setPic == 6 && setPic2 == 6 && setPic3 == 6){
            winnings = .25 * 8;
        }else if(setPic == 2 && setPic2 == 2 && setPic3 == 1){
            winnings = .25 * 5;
        }else if(setPic == 2 && setPic2 == 1 && setPic3 == 2){
            winnings = .25 * 5;
        }else if(setPic == 1 && setPic2 == 2 && setPic3 == 2){
            winnings = .25 * 5;
        }else if(setPic == 3 && setPic2 == 3 && setPic3 == 1){
            winnings = .25 * 5;
        }else if(setPic == 3 && setPic2 == 1 && setPic3 == 3){
            winnings = .25 * 5;
        }else if(setPic == 1 && setPic2 == 3 && setPic3 == 3){
            winnings = .25 * 5;
        }else if(setPic == 5 && setPic2 == 5){
            winnings = .25 * 2;
        }else if(setPic == 5 && setPic3 == 5){
            winnings = .25 * 2;
        }else if(setPic2 == 5 && setPic3 == 5){
            winnings = .25 * 2;
        }
    }
} 

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: problems with slot machine program

Posted 29 March 2012 - 07:22 PM

        Redbox.setBackground(Color.red);
        Slots.add(Redbox,0); 



A Rectangle is a class that hold a Rectangle object coordinates (x, y, width, height)

It is NOT a GUI element (JComponent)

You cannot:
- add() something to a Rectangle
- set a Rectangle color
- add a Rectangle to a JComponent (JPanel, JFrame, ...)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1