7 Replies - 313 Views - Last Post: 25 February 2012 - 09:56 PM Rate Topic: -----

#1 Dorito_125  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 88
  • Joined: 13-December 08

Who want's to gamble! (Virtually!)

Posted 24 February 2012 - 03:19 PM

So I just finished writing the first version of my second programming assignment. We're just suppossed to write simple slot machine. I can't use custom methods and I'll probably have to take out my array and use something else because we haven't covered them yet. While actually.. I'll probably have to take out Thread.sleep() as well but I just thought it added a nice finished touch.

It's actually pretty fun!

I also know I should be using try catch blocks and not throw main but we're not allowed to yet so I just did that for myself and you guys to have some fun. It'll have to be removed before being turned in.

Let me know if you actually play it!

What do you think?

/**
 * Slot Machine
 * 
 * @author Andrew Hood
 * @version 02/23/2012
 */
import java.util.*;
 
class SlotMachine
{
    public static void main(String[] args) throws Exception
    {
        Scanner input = new Scanner(System.in);
        Random randNum = new Random();
        final int SENTINEL = 0;
        int numOfCoins = 1, bet, slot1Result, slot2Result, slot3Result, totalWinnings = 0, totalLoss, winnings, originalNumOfCoins, payoffFactor = 0;
        double balance;
        String[] slots = {"Bell", "Grape", "Triangle", "Wildcard" };
        while(numOfCoins != SENTINEL)
        {
        do
        {
            System.out.print("Enter the number of coins you are depositing: ");
            originalNumOfCoins = input.nextInt();
            numOfCoins = originalNumOfCoins;
        } while(numOfCoins <= 0);
        
        do
        {
            do
            {
                System.out.print("Enter your bet (1-4), enter 0 to exit: ");
                bet = input.nextInt();
            } while( bet < 0 || bet > 4 || bet > numOfCoins);
            
            numOfCoins -= bet;
            
            if(bet != 0)
            {
                System.out.println("Generating new combination...\n");
                Thread.sleep(1500);
                slot1Result = randNum.nextInt(4);
                slot2Result = randNum.nextInt(4);
                slot3Result = randNum.nextInt(4);
                
                System.out.print(slots[slot1Result] + "\t");
                Thread.sleep(400);
                System.out.print(slots[slot2Result] + "\t");
                Thread.sleep(400);
                System.out.println(slots[slot3Result] + "\n");
                Thread.sleep(400);
                
                switch(slot1Result)
                {
                    case 0: switch(slot2Result) // Bell 
                            {
                                case 0: switch(slot3Result) // Bell 
                                        {
                                            case 0: payoffFactor = 10; break; // Bell
                                            case 1: payoffFactor = 0; break; // Grape
                                            case 2: payoffFactor = 0; break; // Triangle
                                            case 3: payoffFactor = 3; break; // Wildcard
                                            default: System.out.println("0,0");
                                        }
                                        break;
                                case 1: switch(slot3Result) // Grape
                                        {
                                            case 0: payoffFactor = 0; break; // Bell
                                            case 1: payoffFactor = 0; break; // Grape
                                            case 2: payoffFactor = 0; break; // Triangle
                                            case 3: payoffFactor = 0; break; // Wildcard
                                            default: System.out.println("0,1");
                                        }
                                        break;
                                case 2: switch(slot3Result) // Triangle
                                        {
                                            case 0: payoffFactor = 0; break; // Bell
                                            case 1: payoffFactor = 0; break; // Grape
                                            case 2: payoffFactor = 0; break; // Triangle
                                            case 3: payoffFactor = 0; break; // Wildcard
                                            default: System.out.println("0,2");
                                        }
                                        break;
                                case 3: switch(slot3Result) // Wildcard
                                        {
                                            case 0: payoffFactor = 3; break; // Bell
                                            case 1: payoffFactor = 0; break; // Grape
                                            case 2: payoffFactor = 0; break; // Triangle
                                            case 3: payoffFactor = 0; break; // Wildcard
                                            default: System.out.println("0,3");
                                        }
                                        break;
                            }
                            break;
                    case 1: switch(slot2Result) // Grape 
                            {
                                case 0: switch(slot3Result) // Bell 
                                        {
                                            case 0: payoffFactor = 0; break; // Bell
                                            case 1: payoffFactor = 0; break; // Grape
                                            case 2: payoffFactor = 0; break; // Triangle
                                            case 3: payoffFactor = 0; break; // Wildcard
                                            default: System.out.println("1,0");
                                        }
                                        break;
                                case 1: switch(slot3Result) // Grape
                                        {
                                            case 0: payoffFactor = 0; break; // Bell
                                            case 1: payoffFactor = 7; break; // Grape
                                            case 2: payoffFactor = 0; break; // Triangle
                                            case 3: payoffFactor = 1; break; // Wildcard
                                            default: System.out.println("1,1");
                                        }
                                        break;
                                case 2: switch(slot3Result) // Triangle
                                        {
                                            case 0: payoffFactor = 0; break; // Bell
                                            case 1: payoffFactor = 0; break; // Grape
                                            case 2: payoffFactor = 0; break; // Triangle
                                            case 3: payoffFactor = 0; break; // Wildcard
                                            default: System.out.println("1,2");
                                        }
                                        break;
                                case 3: switch(slot3Result) // Wildcard
                                        {
                                            case 0: payoffFactor = 0; break; // Bell
                                            case 1: payoffFactor = 1; break; // Grape
                                            case 2: payoffFactor = 0; break; // Triangle
                                            case 3: payoffFactor = 0; break; // Wildcard
                                            default: System.out.println("1,3");
                                        }
                                        break;
                            }
                            break;
                    case 2: switch(slot2Result) // Triangle 
                            {
                                case 0: switch(slot3Result) // Bell 
                                        {
                                            case 0: payoffFactor = 0; break; // Bell
                                            case 1: payoffFactor = 0; break; // Grape
                                            case 2: payoffFactor = 0; break; // Triangle
                                            case 3: payoffFactor = 0; break; // Wildcard
                                            default: System.out.println("3,0");
                                        }
                                        break;
                                case 1: switch(slot3Result) // Grape
                                        {
                                            case 0: payoffFactor = 0; break; // Bell
                                            case 1: payoffFactor = 0; break; // Grape
                                            case 2: payoffFactor = 0; break; // Triangle
                                            case 3: payoffFactor = 0; break; // Wildcard
                                            default: System.out.println("3,1");
                                        }
                                        break;
                                case 2: switch(slot3Result) // Triangle
                                        {
                                            case 0: payoffFactor = 0; break; // Bell
                                            case 1: payoffFactor = 0; break; // Grape
                                            case 2: payoffFactor = 5; break; // Triangle
                                            case 3: payoffFactor = 0; break; // Wildcard
                                            default: System.out.println("3,2");
                                        }
                                        break;
                                case 3: switch(slot3Result) // Wildcard
                                        {
                                            case 0: payoffFactor = 0; break; // Bell
                                            case 1: payoffFactor = 0; break; // Grape
                                            case 2: payoffFactor = 0; break; // Triangle
                                            case 3: payoffFactor = 0; break; // Wildcard
                                            default: System.out.println("3,3");
                                        }
                                        break;
                            }
                            break;
                    case 3: switch(slot2Result) // Wildcard 
                            {
                                case 0: switch(slot3Result) // Bell 
                                        {
                                            case 0: payoffFactor = 3; break; // Bell
                                            case 1: payoffFactor = 0; break; // Grape
                                            case 2: payoffFactor = 0; break; // Triangle
                                            case 3: payoffFactor = 0; break; // Wildcard
                                            default: System.out.println("4,0");
                                        }
                                        break;
                                case 1: switch(slot3Result) // Grape
                                        {
                                            case 0: payoffFactor = 0; break; // Bell
                                            case 1: payoffFactor = 1; break; // Grape
                                            case 2: payoffFactor = 0; break; // Triangle
                                            case 3: payoffFactor = 0; break; // Wildcard
                                            default: System.out.println("4,1");
                                        }
                                        break;
                                case 2: switch(slot3Result) // Triangle
                                        {
                                            case 0: payoffFactor = 0; break; // Bell
                                            case 1: payoffFactor = 0; break; // Grape
                                            case 2: payoffFactor = 0; break; // Triangle
                                            case 3: payoffFactor = 0; break; // Wildcard
                                            default: System.out.println("4,2");
                                        }
                                        break;
                                case 3: switch(slot3Result) // Wildcard
                                        {
                                            case 0: payoffFactor = 0; break; // Bell
                                            case 1: payoffFactor = 0; break; // Grape
                                            case 2: payoffFactor = 0; break; // Triangle
                                            case 3: payoffFactor = 0; break; // Wildcard
                                            default: System.out.println("4,3");
                                        }
                                        break;
                            }
                            break;
                    default: System.out.println("Logic error");
                } // End Main Switch
                winnings = payoffFactor * bet;
                numOfCoins += winnings;
                balance = numOfCoins * 25.0 / 100.0;
                
                System.out.print("Payoff Factor: " + payoffFactor);
                System.out.print("\tBalance: $" + balance);
                System.out.println("\tWinnings: $" + winnings *25.0 /100.0);
            } // End Main If
            
            
            
            
           
            
            
        } while(bet != 0 && numOfCoins > 0);
        totalWinnings = numOfCoins - originalNumOfCoins;
        System.out.println("Thanks for playing with us!!");
        if(totalWinnings < 0)
            System.out.println("Total Loss:\t$" + totalWinnings*25.0/100);
        else if(totalWinnings > 0)
            System.out.println("Total Winnings:\t$" + totalWinnings*25.0/100);
        
        System.out.println("Please enter more coins to play again.");
        } // Main While
    }
}
 



Is This A Good Question/Topic? 0
  • +

Replies To: Who want's to gamble! (Virtually!)

#2 zehawk  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 213
  • Joined: 23-February 12

Re: Who want's to gamble! (Virtually!)

Posted 24 February 2012 - 03:32 PM

Yes it pretty cool, any questions or is this just a share a project cause theres a forum for that...:)
Was This Post Helpful? 0
  • +
  • -

#3 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,682
  • Joined: 13-March 10

Re: Who want's to gamble! (Virtually!)

Posted 24 February 2012 - 03:47 PM

you should invest in methods.
Was This Post Helpful? 0
  • +
  • -

#4 Dorito_125  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 88
  • Joined: 13-December 08

Re: Who want's to gamble! (Virtually!)

Posted 24 February 2012 - 03:54 PM

What do you think about the nested switch for logical flow of the payoff factor?

I'll explain why I chose that but I don't want to influence what you have to say about it.

If you read the OP we can't use methods yet. I don't like it either, makes for super messy code >.<
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Who want's to gamble! (Virtually!)

Posted 24 February 2012 - 04:10 PM

Quote

I don't like it either, makes for super messy code >.<

Methods are there to give readability and organizing code by putting related tasks together. The use of methods makes code clean and easy to understand or debug. Also in the OO life, we prefer to make the main() method as short as possible, it is there to start the program and initialize starting tasks.

Quote

What do you think about the nested switch for logical flow of the payoff factor?

For me it looks messier, I cant follow it but I believe it can simplified to something readable
Was This Post Helpful? 0
  • +
  • -

#6 Dorito_125  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 88
  • Joined: 13-December 08

Re: Who want's to gamble! (Virtually!)

Posted 24 February 2012 - 05:28 PM

View PostDorito_125, on 24 February 2012 - 03:19 PM, said:

... I can't use custom methods and I'll probably have to take out my array and use something else because we haven't covered them yet.
Unfortunately :ph34r:



How do you think it could be simplified?
Was This Post Helpful? 0
  • +
  • -

#7 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Who want's to gamble! (Virtually!)

Posted 24 February 2012 - 08:00 PM

View PostDorito_125, on 24 February 2012 - 07:28 PM, said:

How do you think it could be simplified?
I would make a new class and delegate the responsibility of deciding the payoffFactor to that class.
import java.util.HashMap;

public class PayoutDecider {
	private HashMap<Integer, Integer> payouts;

	public PayoutDecider() {
		payouts = new HashMap<Integer, Integer>();
	}

	public void registerPayout(int firstSlot, int secondSlot, int thirdSlot, int payoffFactor) {
		int slotResults = (firstSlot << 4) + (secondSlot << 2) + thirdSlot;
		payouts.put(slotResults, payoffFactor);
	}

	public int getPayoffFactor(int firstSlot, int secondSlot, int thirdSlot) {
		int payoffFactor = 0;
		int slotResults = (firstSlot << 4) + (secondSlot << 2) + thirdSlot;

		if (payouts.containsKey(slotResults)) {
			payoffFactor = payouts.get(slotResults);
		}

		return payoffFactor;
	}
}
In your code, you could create a new PayoutDecider and register the ones that will win. Then instead of a big long switch statement, you could call getPayoffFactor().
// Move these final ints to the appropriate scope
final int BELL = 0;
final int GRAPE = 1;
final int TRIANGLE = 2;
final int WILD = 3;

PayoutDecider payouts = new PayoutDecider();
payouts.registerPayout(BELL, BELL, BELL, 10);
payouts.registerPayout(BELL, BELL, WILD, 3);
payouts.registerPayout(BELL, WILD, BELL, 3);
payouts.registerPayout(WILD, BELL, BELL, 3);

payouts.registerPayout(GRAPE, GRAPE, GRAPE, 7);
payouts.registerPayout(GRAPE, GRAPE, WILD, 1);
payouts.registerPayout(GRAPE, WILD, GRAPE, 1);
payouts.registerPayout(WILD, GRAPE, GRAPE, 1);

// etc...

payoffFactor = payouts.getPayoffFactor(slot1Result, slot2Result, slot3Result);
Of course, my solution could be improved and only allows for 4 possibilities, but you get the gist of it. The new class handles all the messy decision making and you lose a ~160 line switch statement that is hard to read. You could even expand the class to allow for more than 4 possibilites, have it have methods that allow to register a payout that doesn't matter what order the inputs are in. Sounds kinda fun. Anyway, good job on what you have so far! :)
Was This Post Helpful? 0
  • +
  • -

#8 Dorito_125  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 88
  • Joined: 13-December 08

Re: Who want's to gamble! (Virtually!)

Posted 25 February 2012 - 09:56 PM

167 lines of code to 86!

I couldn't take it anymore, I had to make a custom method. Even with less lines of code there's more error checking now. I also misread the instructions. There's no wildcard! :stupid: The table given included a asterisk, but that was to represent the other two options. so Bell Bell Triangle would give a payoff of 3. Much better odd's now.

Version 2.0!
/**
 * Slot Machine
 * 
 * @author Andrew Hood
 * @version 02/23/2012
 */
import java.util.*;
public class SlotMachine
{
    static int getPayoff(int slot1Result, int slot2Result, int slot3Result)
    {
        int payoffFactor = 0;
        int slotCombination = slot1Result*slot1Result + slot2Result*slot2Result + slot3Result*slot3Result;
        switch(slotCombination)
        {
            case 0: payoffFactor = 10; break;
            case 3: payoffFactor = 7; break;
            case 12: payoffFactor = 5; break;
            case 4:
            case 1: payoffFactor = 3; break;
            case 2: 
            case 6: payoffFactor = 1; break;
            case 9:
            case 8:
            case 5: payoffFactor = 0; break;
            default: System.out.println("Logic error");
        } // End Main Switch
        return payoffFactor;
    } // End getPayoff();
    
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        Random randNum = new Random();
        String[] slots = {"Bell", "Grape", "Triangle"};
        int numOfCoins, originalNumOfCoins, payoffFactor, slot1Result, slot2Result, slot3Result, winnings, bet = 1;
            
        do
        {
            System.out.print("Enter the number of coins you are depositing: ");
            try 
            {
                originalNumOfCoins = input.nextInt();
                numOfCoins = originalNumOfCoins;
            }
            catch(InputMismatchException ex)
            { 
                System.err.println("That was not the droid we were looking for.");
                System.err.println("Invalid input program will now exit.");
                numOfCoins = 5;
                System.exit(0);
            }  
        } while(numOfCoins <= 0);
        
        while(bet != 0 && numOfCoins > 0)
        {
            System.out.print("Enter your bet (1-4), enter 0 to exit: ");
            try { bet = input.nextInt(); }
            catch(InputMismatchException ex)
            {
                System.err.println("That was not the droid we were looking for.");
                System.err.println("Invalid input program will now exit.");
                System.exit(0);
            }
            
            if(bet > 0 && bet <= 4 && bet < numOfCoins)  // Skip this if user want's to exit program.
            {
                numOfCoins -= bet;
                slot1Result = randNum.nextInt(3);
                slot2Result = randNum.nextInt(3);
                slot3Result = randNum.nextInt(3);
                System.out.println("\nGenerating new combination...\n");
                System.out.print(slots[slot1Result] + "\t");
                System.out.print(slots[slot2Result] + "\t");
                System.out.println(slots[slot3Result] + "\n");
                payoffFactor = getPayoff(slot1Result, slot2Result, slot3Result);
                winnings = payoffFactor * bet;
                numOfCoins += winnings;
                System.out.print("Payoff Factor: " + payoffFactor);
                System.out.print("\tBalance: $" + numOfCoins * 25.0 / 100.0);
                System.out.println("\tWinnings: $" + winnings*25.0 /100.0);
            } // End Main If     
        } // End Main While Loop
        System.out.println("Thanks for playing with us!!");
    } // End public static void main
} // End SlotMachine
 


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1