Slot Machine

Need a JFrame and a working loop

Page 1 of 1

6 Replies - 1355 Views - Last Post: 21 November 2010 - 01:41 PM Rate Topic: -----

Topic Sponsor:

#1 babyboo109  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-November 10

Slot Machine

Posted 21 November 2010 - 10:52 AM

Hi all,

I desperately need some help with the following.

I'm in my first year, studying Java, and we were asked to come up with a working slot machine.
I wrote the code, attached below, which is working. However, it needs to be in a JFrame, which I am really struggling with. And I also need some way to let the player continue on, without having the program close on them. At least until they enter 0 to quit.

I would really appreciate anyone's input.

Thanks lots,

import java.util.*;
import javax.swing.*;


public class SlotMachine {

   public static void main(String[] args) {

               System.out.println("Welcome to our Slot Machine\n\nRULES:\n\n"
               + "1. You can bet between 1 and 4 slot coins per spin\n"
               + "2. Each slot coin is worth €0.25\n"
               + "3. Enter 0 to close the slot machine\n"
               + "4. Have fun and Good luck!!\n\n");

        int Bet;
        Scanner input = new Scanner(System.in);

        System.out.print("Please enter your bet from 1-4 here, or 0 to close : ");
        Bet = input.nextInt();

        while(Bet>4){
        System.out.println("\nYou have not entered a valid bet.\n\nPlease try again :\n");
        Bet = input.nextInt();
        }
        while(Bet<=0){
            System.exit(0);
       }

        int a = (Bet*10);
        int b = (Bet*7);
        int c = (Bet*5);
        int d = (Bet*3);
        int e = Bet;

        double Total1 = (a*0.25);
        double Total2 = (b*0.25);
        double Total3 = (c*0.25);
        double Total4 = (d*0.25);
        double Total5 = (e*0.25);
       
        Random Shapes = new Random();
        int combination = Shapes.nextInt(28);

           switch (combination){
               case 1:
                System.out.println("\nBELL BELL BELL\n\nYou have won " + a + " slot coins\n"
                        + "Total Winnings: €" + Total1 + "\nWell done!");
                   break;
               case 2:
                System.out.println("\nGRAPES GRAPES GRAPES\n\nYou have won " + b + " slot coins\n"
                        + "Total Winnings: €" + Total2 + "\nWell done!");
                   break;
               case 3:
                   System.out.println("\nCHERRY CHERRY CHERRY\n\nYou have won "+ c + " slot coins\n"
                           + "Total Winnings: €" + Total3 + "\nWell done!");
                   break;
               case 4:
                   System.out.println("\nCHERRY CHERRY BELL\n\nYou have won " + d + " slot coins\n"
                           + "Total Winnings: €" + Total4 + "\nWell done!");
                   break;
               case 5:
                   System.out.println("\nCHERRY CHERRY GRAPES\n\nYou have won " + d + " slot coins\n"
                           + "Total Winnings: €" + Total4 + "\nWell done!");
                   break;
               case 6:
                   System.out.println("\nCHERRY BELL CHERRY\n\nYou have won " + d + " slot coins\n"
                           + "Total Winnings: €" + Total4 + "\nWell done!");
                   break;
               case 7:
                   System.out.println("\nCHERRY GRAPES CHERRY\n\nYou have won " + d + " slot coins\n"
                           + "Total Winnings: €" + Total4 + "\nWell done!");
                   break;
               case 8:
                   System.out.println("\nBELL CHERRY CHERRY\n\nYou have won " + d + " slot coins\n"
                           + "Total Winnings: €" + Total4 + "\nWell done!");
                   break;
               case 9:
                   System.out.println("\nGRAPES CHERRY CHERRY\n\nYou have won " + d + " slot coins\n"
                           + "Total Winnings: €" + Total4 + "\nWell done!");
                   break;
               case 10:
                   System.out.println("\nCHERRY BELL GRAPES\n\nYou have won " + e + " slot coins\n"
                           + "Total Winnings: €" + Total5 + "\nWell done!");
                   break;
               case 11:
                   System.out.println("\nCHERRY GRAPES BELL\n\nYou have won " + e + " slot coins\n"
                           + "Total Winnings: €" + Total5 + "\nWell done!");
                   break;
               case 12:
                   System.out.println("\nBELL CHERRY GRAPES\n\nYou have won " + e + " slot coins\n"
                           + "Total Winnings: €" + Total5 + "\nWell done!");
                   break;
               case 13:
                   System.out.println("\nGRAPES CHERRY BELL\n\nYou have won " + e + " slot coins\n"
                           + "Total Winnings: €" + Total5 + "\nWell done!");
                   break;
               case 14:
                   System.out.println("\nGRAPES BELL CHERRY\n\nYou have won " + e + " slot coins\n"
                           + "Total Winnings: €" + Total5 + "\nWell done!");
                   break;
               case 15:
                   System.out.println("\nBELL GRAPES CHERRY\n\nYou have won " + e + " slot coins\n"
                           + "Total Winnings: €" + Total5 + "\nWell done!");
               case 16:
                   System.out.println("\nCHERRY BELL BELL\n\nYou have won " + e + " slot coins\n"
                           + "Total Winnings: €" + Total5 + "\nWell done!");
                   break;
               case 17:
                   System.out.println("\nCHERRY GRAPES GRAPES\n\nYou have won " + e + " slot coins\n"
                           + "Total Winnings: €" + Total5 + "\nWell done!");
                   break;
               case 18:
                   System.out.println("\nBELL CHERRY BELL\n\nYou have won " + e + " slot coins\n"
                           + "Total Winnings: €" + Total5 + "\nWell done!");
                   break;
               case 19:
                   System.out.println("\nBELL BELL CHERRY\n\nYou have won " + e + " slot coins\n"
                           + "Total Winnings: €" + Total5 + "\nWell done!");
                   break;
               case 20:
                   System.out.println("\nBELL BELL GRAPES\n\nSorry, you have not won anything on this occasion.");
                   break;
               case 21:
                   System.out.println("\nBELL GRAPES BELL\n\nSorry, you have not won anything on this occasion.");
                   break;
               case 22:
                   System.out.println("\nBELL GRAPES GRAPES\n\nSorry, you have not won anything on this occasion.");
                   break;
               case 23:
                   System.out.println("\nGRAPES CHERRY GRAPES\n\nYou have won " + e + " slot coins\n"
                           + "Total Winnings: €" + Total5 + "\nWell done!");
                   break;
               case 24:
                   System.out.println("\nGRAPES BELL BELL\n\nSorry, you have not won anything on this occasion.");
                   break;
               case 25:
                   System.out.println("\nGRAPES BELL GRAPES\n\nSorry, you have not won anything on this occasion.");
                   break;
               case 26:
                   System.out.println("\nGRAPES GRAPES CHERRY\n\nYou have won " + e + " slot coins\n"
                           + "Total Winnings: €" + Total5 + "\nWell done!");
                   break;
               case 27:
                   System.out.println("\nGRAPES GRAPES BELL\n\nSorry, you have not won anything on this occasion.");
                   break;
               default:
       }
      }
     }

This post has been edited by Dogstopper: 21 November 2010 - 12:18 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Slot Machine

#2 raymens  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 42
  • Joined: 02-June 10

Re: Slot Machine

Posted 21 November 2010 - 11:01 AM

you should put your code in code tags.

So you want to put all the println in the JFrame?
Was This Post Helpful? 0
  • +
  • -

#3 babyboo109  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-November 10

Re: Slot Machine

Posted 21 November 2010 - 11:03 AM

Not just that.

I need the user to be able to input their bet, and make sure that's valid, before the randomiser runs.

Thanks
Was This Post Helpful? 0
  • +
  • -

#4 raymens  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 42
  • Joined: 02-June 10

Re: Slot Machine

Posted 21 November 2010 - 11:05 AM

Have you read something about user interfaces yet?
Like using JFrames, JPanels, JLabels etc.
Was This Post Helpful? 0
  • +
  • -

#5 babyboo109  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-November 10

Re: Slot Machine

Posted 21 November 2010 - 11:22 AM

Yes I have. I've been playing around with the code but the problem I am having, is using the input from the user to be able to work out their winnings.
And I thought it would be best to put the working code up, instead of my mushed up attempt at a JFrame.

Thanks
Was This Post Helpful? 0
  • +
  • -

#6 I ♣ Seals  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 52
  • Joined: 20-November 10

Re: Slot Machine

Posted 21 November 2010 - 11:38 AM

As someone who's spent far more time on online casinos than can possibly be good for anyone, I can tell you that the usual way of getting the bet amount from the user is by a pair of buttons which respectively raise or lower the bet within an array of predefined amounts.

Also, instead of using a double for the bet amount, you might want to just use an int and have 1 be equivalent to 0.01€.
Was This Post Helpful? 0
  • +
  • -

#7 raymens  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 42
  • Joined: 02-June 10

Re: Slot Machine

Posted 21 November 2010 - 01:41 PM

What I would do is create two JLabels and a JButton, make one label editable for the user to input. Use the button to calculate and change the other label accordingly to the calculated result. You could base it on a simple label editing snippet/tutorial, just add the random calculation.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1