4 Replies - 1446 Views - Last Post: 02 August 2012 - 11:18 AM Rate Topic: -----

#1 LampeMW  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-August 12

Question regarding slot machine program

Posted 02 August 2012 - 10:21 AM

Hey, I had an assignment to design a slot machine GUI. Everything seems to be working properly thus far, other than I cannot figure out how to have the credits accumulate as they are won each round or have them subtracted after each spin. User starts with 50 credits and each spin costs 2 credits, and I have a public integer called val which is set to 50. I just cannot figure out how after each time the Spin button is clicked to deduct 2 coins. It works fine for the first spin, and changes to 48, but then stays at 48 for each consecutive roll unless the user wins, in which case the winnings are just added to 50, but nothing ever accumulates.

Here's the code, if anyone could guide in my a direction that would be wonderful.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slots;

import java.util.*;
import javax.swing.JOptionPane;        
/**
 *
 * @author MattLampe
 * Slot Machine Program
 */
public class SlotsGUI extends javax.swing.JFrame {
    
    public int val = 50;
    public int spins = 10;
    /**
     * Creates new form SlotsGUI
     */
    public SlotsGUI() {
        initComponents();
        availableSpinsText.setText("10");
        Reel1.setText("");
        Reel2.setText("");
        Reel3.setText("");
        currentCreditsText.setText("50");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        spinButton = new javax.swing.JButton();
        currentCredits = new javax.swing.JLabel();
        availableSpins = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        Reel1 = new javax.swing.JTextField();
        Reel2 = new javax.swing.JTextField();
        Reel3 = new javax.swing.JTextField();
        availableSpinsText = new javax.swing.JTextField();
        currentCreditsText = new javax.swing.JTextField();
        quitButton = new javax.swing.JButton();
        aboutButton = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        spinButton.setText("SPIN");
        spinButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                spinButtonActionPerformed(evt);
            }
        });

        currentCredits.setText("Current Credits");

        availableSpins.setText("Available Spins");

        jLabel1.setText("Reel 1");

        jLabel2.setText("Reel 2");

        jLabel3.setText("Reel 3");

        Reel1.setText("jTextField1");

        Reel2.setText("jTextField2");

        Reel3.setText("jTextField3");

        quitButton.setText("Quit");
        quitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitButtonActionPerformed(evt);
            }
        });

        aboutButton.setText("About");
        aboutButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutButtonActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Lucida Grande", 0, 36)); // NOI18N
        jLabel4.setText("Slot Machine");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(availableSpins)
                            .add(layout.createSequentialGroup()
                                .add(6, 6, 6)
                                .add(availableSpinsText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 79, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                        .add(85, 85, 85)
                        .add(currentCredits)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(currentCreditsText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 35, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(39, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(aboutButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(quitButton))))
            .add(layout.createSequentialGroup()
                .add(42, 42, 42)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(layout.createSequentialGroup()
                            .add(jLabel2)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(Reel2))
                        .add(layout.createSequentialGroup()
                            .add(jLabel1)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(Reel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 188, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(layout.createSequentialGroup()
                            .add(jLabel3)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(Reel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 188, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(layout.createSequentialGroup()
                        .add(102, 102, 102)
                        .add(spinButton)))
                .add(0, 0, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jLabel4)
                .add(71, 71, 71))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel4)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 27, Short.MAX_VALUE)
                .add(spinButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(Reel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(Reel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(Reel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(51, 51, 51)
                .add(availableSpins)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(availableSpinsText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(currentCredits)
                    .add(currentCreditsText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(16, 16, 16)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(quitButton)
                    .add(aboutButton)))
        );

        pack();
    }// </editor-fold>

    private void spinButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        Random r = new Random();
        int reel1, reel2, reel3;
        reel1 = r.nextInt(3);
        reel2 = r.nextInt(3);
        reel3 = r.nextInt(3);
        
        spins--;
        availableSpinsText.setText(String.valueOf(spins));
        
        currentCreditsText.setText(String.valueOf(val - 2));
        
        if (spins > 0)
        {
        if (reel1 == 0)
        {
            Reel1.setText("Orange");
        }
        else if (reel1 == 1)
        {
            Reel1.setText("Banana");
        }
        else if (reel1 == 2)
        {
            Reel1.setText("Apple");
        }
        
        if (reel2 == 0)
        {
            Reel2.setText("Orange");
        }
        else if (reel2 == 1)
        {
            Reel2.setText("Banana");
        }
        else if (reel2 == 2)
        {
            Reel2.setText("Apple");
        }
        
        if (reel3 == 0)
        {
            Reel3.setText("Orange");
        }
        else if (reel3 == 1)
        {
            Reel3.setText("Banana");
        }
        else if (reel3 == 2)
        {
            Reel3.setText("Apple");
        }
        }
        if (reel3 == reel2 && reel2 == reel1)
        {
            currentCreditsText.setText(String.valueOf(val + 5));
            JOptionPane.showMessageDialog(null, "Jackpot!  You've won 5 credits.");
        }
        else if (reel1 == reel2 || reel2 == reel3 || reel1 == reel3)
        {
            currentCreditsText.setText(String.valueOf(val + 1));
            JOptionPane.showMessageDialog(null, "You've matched two! You win 1 coin.");
        }
        else
        {
            JOptionPane.showMessageDialog(null, "You've lost this round.");
        }
        if (spins == 0)
        {
            JOptionPane.showMessageDialog(null, "Game Over, you have used your 10 spins.");
            System.exit(0);
        }
        
        
        
    }                                          

    private void quitButtonActionPerformed(java.awt.event.ActionEvent evt) {
        System.exit(0);
    }

    private void aboutButtonActionPerformed(java.awt.event.ActionEvent evt) {
        JOptionPane.showMessageDialog(null, "You have 10 turns to play the Slot Machine.\n If you have two matching items, you win 1 coin.\n "
                + "If you have three matching items, you win 5 coins.\n Each turn costs 2 coins.", "About", WIDTH);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SlotsGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SlotsGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SlotsGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SlotsGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        
        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                
                new SlotsGUI().setVisible(true); 
                
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JTextField Reel1;
    private javax.swing.JTextField Reel2;
    private javax.swing.JTextField Reel3;
    private javax.swing.JButton aboutButton;
    private javax.swing.JLabel availableSpins;
    private javax.swing.JTextField availableSpinsText;
    private javax.swing.JLabel currentCredits;
    private javax.swing.JTextField currentCreditsText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JButton quitButton;
    private javax.swing.JButton spinButton;
    // End of variables declaration
}




Is This A Good Question/Topic? 0
  • +

Replies To: Question regarding slot machine program

#2 Ghlavac  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 519
  • Joined: 14-January 09

Re: Question regarding slot machine program

Posted 02 August 2012 - 10:41 AM

You will get no help here for crappy generated UI code.
Was This Post Helpful? 0
  • +
  • -

#3 LampeMW  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-August 12

Re: Question regarding slot machine program

Posted 02 August 2012 - 10:42 AM

View PostGhlavac, on 02 August 2012 - 10:41 AM, said:

You will get no help here for crappy generated UI code.


That's unfortunate, sorry. Our professor required us to use Netbeans.
Was This Post Helpful? 0
  • +
  • -

#4 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,599
  • Joined: 25-December 08

Re: Question regarding slot machine program

Posted 02 August 2012 - 10:53 AM

You can use netbeans, but you don't have to use their WYSIWYG editor. The reason Ghlavac said you're not going to get any help is because that tool generates unreadable code, and doesnt generate it in a very logical manor.

Its not too hard to add a button to act as a lever, and a few fields to show credits and the slot machine results.
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2815
  • View blog
  • Posts: 11,917
  • Joined: 20-September 08

Re: Question regarding slot machine program

Posted 02 August 2012 - 11:18 AM

You don't alter the value of your instance variable 'val' (which should really be called something better, like 'credits'). You'd be better off using the following method and calling it when necessary
    private int updateCredits(int howMany) {
        credits += howMany;
        currentCreditsText.setText(String.valueOf(credits));

        return credits;
    }


This post has been edited by g00se: 02 August 2012 - 11:19 AM
Reason for edit:: indent

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1