Roulette Program


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Roulette Program

Hybrid View

  1. #1
    Join Date
    Jan 2006
    Posts
    2

    Roulette Program

    I've been working on this program for a spot of fun lately. I'm not a super experienced coder with Java, but don't worry about dumbing things down. This program isn't very complex in itself, but for some reason it isn't doing what I want it to!

    Code:
    import java.io.*;
    import java.util.Random;
    import java.text.DecimalFormat;
    
    public class Roulette
    {
    	public static void main (String args[]) throws IOException
    	{
    		BufferedReader input = new BufferedReader (new InputStreamReader (System.in));
    		Random generator = new Random();
    		DecimalFormat dollarOutput = new DecimalFormat ("$00.00");
    		
    		char userInput;
    		char userInput2;
    		char betType;
    		char spinSuccess;
    		char successType;
    		char eOSuccessType;
    		int betNum;
    		int rouletteSpinNum;
    		int quadrant;
    		double userMoney = 100.0;
    		double betAmount;
    		
    		System.out.println("Welcome to Thomas' Casino of Luck!");
    		System.out.println("How about spin of Roulette?");
    		
    		do{
    			System.out.print("\nType in 'y' to play, 'n' to quit or 'r' for the rules.\t");		
    			userInput = (input.readLine()).charAt(0);
    			
    			if (userInput == 'y')
    				{
    					System.out.println("\nYou have\t\t\t\t" + dollarOutput.format(userMoney) + ".");
    					System.out.print("How much do you want to bet?\t\t");
    					betAmount = Double.parseDouble(input.readLine());
    					if (betAmount > userMoney)
    						{
    							System.out.println("You can't bet what you don't have!");
    						}
    					else{					
    						System.out.print("\nWould you like to bet on:\n\t'e' evens\n\t'o' odds\n\t'q' a quadrant\n\t's' for a specific number\t");
    						betType = (input.readLine()).charAt(0);
    					
    						rouletteSpinNum = generator.nextInt(100) + 1;
    					
    						if (rouletteSpinNum % 2 == 0)
    							eOSuccessType = 'e';
    						else
    							eOSuccessType = 'o';
    						
    						if (rouletteSpinNum <=25)
    							quadrant = 1;
    						else if (rouletteSpinNum <= 50)
    							quadrant = 2;
    						else if (rouletteSpinNum <= 75)
    							quadrant = 3;
    						else
    							quadrant = 4;
    							
    						System.out.println("");
    					
    						if (betType == 's')
    							{	
    								System.out.print ("\nWhat number would you like to bet on?\t");
    								betNum = Integer.parseInt(input.readLine());
    					
    								if (betNum == rouletteSpinNum)
    								{
    									System.out.println("\nThe roulette wheel stopped on " + rouletteSpinNum + "\n");
    									System.out.println("*********************************\nYou've just quadrupuled your bet!\n*********************************\n");
    									userMoney = userMoney + (betAmount * 4.0);
    									System.out.println("You now have:\t" + dollarOutput.format(userMoney));
    								}
    								else
    								{
    									System.out.println("\nUnfortunately, the roulette wheel stopped on " + rouletteSpinNum);
    									System.out.println("You guessed " + betNum);
    									userMoney = userMoney - betAmount;
    									System.out.println("You now have:\t" + dollarOutput.format(userMoney));
    									System.out.println("\nBetter luck next time!\n");
    								}
    							}
    						else if (betType == 'e' || betType == 'o')
    						{
    							if (eOSuccessType == 'e')
    							{
    								System.out.println("\nThe roulette wheel stopped on " + rouletteSpinNum + "\n");
    								System.out.println("*******************************\nYou've just doubled your bet!\n*******************************\n");
    								userMoney = userMoney + (betAmount * 2.0);
    								System.out.println("You now have:\t" + dollarOutput.format(userMoney));
    							}
    							else if (eOSuccessType == 'o')
    							{
    								System.out.println("\nThe roulette wheel stopped on " + rouletteSpinNum + "\n");
    								System.out.println("*******************************\nYou've just doubled your bet!\n*******************************\n");
    								userMoney = userMoney + (betAmount * 2.0);
    								System.out.println("You now have:\t" + dollarOutput.format(userMoney));
    							}
    							
    							else
    							{
    								System.out.println("\nUnfortunately the Roulette wheel came to a stop on " + rouletteSpinNum);
    								userMoney = userMoney - betAmount;
    								System.out.println("You now have:\t" + dollarOutput.format(userMoney));
    								System.out.println("\nBetter luck next time!\n");
    							}
    						}
    					}
    				}
    				else if (userInput == 'r')
    					{
    						do{
    							System.out.println("\n******************************************************************************");
    							System.out.println("The Rules of Roulette!");
    							System.out.println("1.) You can bet any increment of money you have.");
    							System.out.println("2.) If you lose your bet you lose the money.");
    							System.out.println("3.) You can bet on any whole number from 1 to 100 (inclusive).");
    							System.out.println("4.) If you bet your money on an even or odd and win your bet is doubled.");
    							System.out.println("5.) If you bet your money on a quadrant and win your bet is tripled.");
    							System.out.println("5.) If you bet your money on a specific number and win, the bet is quadrupled.");
    							System.out.println("******************************************************************************");
    							System.out.print("\nHit 'm' to read more, or 'd' if you're done.\t\t");
    						
    							userInput2 = (input.readLine()).charAt(0);
    						
    							if (userInput2 == 'm')
    							{
    								System.out.println("\nHere is a sample round:");
    								System.out.println("\tYou opt to bet $20.00.");
    								System.out.println("\tYou then opt to bet on Even numbers.");
    								System.out.println("\tWhen the Rouette Wheel finishes it lands on a 44.");
    								System.out.println("\tYou've just doubled your bet and made $40.00.");
    								System.out.println("If you're still confused just play a few rounds to understand more.\n");
    								
    								userInput2 = 'd';
    							}
    							else if (userInput2 == 'd')
    								System.out.println("\nBack to the beginning!");
    							else
    								System.out.println("\nThat's not a valid input, try again!");
    						}while (userInput2 != 'd');
    					}
    				else if (userInput == 'n')
    					System.out.println("\nThanks for playing!");
    				else
    					{
    					System.out.println("\nI'm sorry, but that's not a valid option, try again!\n");
    					}
    		}while (userInput != 'n');	
    		
    		System.out.println("You started with:\t\t\t$100.00");
    		System.out.println("You finished with:\t\t\t" + dollarOutput.format(userMoney));
    		System.out.println("You made, in total:\t\t\t" + dollarOutput.format((userMoney - 100)) + "\n");	
    	}
    }
    It's a simple game of roulette, though be warned as its under construction, quadrants don't function. Only snippets of the code for it are there.

    Here's my problem: If you opt to bet on evens or odds you always win. Even if it rolls a 97 it will happily tell you, you got a 97 and bet on evens and then give you your money back!

    I've become clueless, staring at the code so much gets to meh.

    Thanks!

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Code:
    else if (betType == 'e' || betType == 'o') {
        if (eOSuccessType == 'e') {
            System.out.println("\nThe roulette wheel stopped on " +
                rouletteSpinNum + "\n");
            System.out.println("*******************************\n" +
                "You've just doubled your bet!" +
                "\n*******************************\n");
            userMoney = userMoney + (betAmount * 2.0);
            System.out.println("You now have:\t" + 
                dollarOutput.format(userMoney));
        } else if (eOSuccessType == 'o') {
            System.out.println("\nThe roulette wheel stopped on " +
                rouletteSpinNum + "\n");
            System.out.println("*******************************\n" +
                "You've just doubled your bet!" +
                "\n*******************************\n");
            userMoney = userMoney + (betAmount * 2.0);
            System.out.println("You now have:\t" + 
                dollarOutput.format(userMoney));
        }
    //...
    You're saying that if they enter 'e' or 'o', they win, because if they do enter 'e' or 'o', eOSuccessType will be either 'e' or 'o', and it says right there in your code to tell them they win if that is the case. Change that block of code to this and it should work.
    Code:
    if (betType == eOSuccessType) {
            System.out.println("\nThe roulette wheel stopped on " +
                rouletteSpinNum + "\n");
            System.out.println("*******************************\n" +
                "You've just doubled your bet!" +
                "\n*******************************\n");
            userMoney = userMoney + (betAmount * 2.0);
            System.out.println("You now have:\t" + 
                dollarOutput.format(userMoney));
    }

  3. #3
    Join Date
    Jan 2006
    Posts
    2
    The issue aren't words saying they won, it's the actual variable. It still gives them the money.

    I fixed the solution by getting rid of the pipes and just spreading it out over a few ifs.

  4. #4
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    If you made the change that i suggested, it would have worked. You were
    not only displaying the words put changing the varialbe there as well.

Similar Threads

  1. Help with a program
    By stormswimmer in forum Java
    Replies: 2
    Last Post: 01-02-2006, 03:17 PM
  2. problem in program in c++
    By mheasen in forum Architecture and Design
    Replies: 0
    Last Post: 03-20-2002, 09:24 AM
  3. Please help with running executable vb program.
    By Gordon Reichhardt in forum VB Classic
    Replies: 2
    Last Post: 01-08-2002, 10:06 AM
  4. How can I build copy protection into my program
    By W.Pierce in forum VB Classic
    Replies: 1
    Last Post: 12-11-2001, 08:28 AM
  5. Replies: 0
    Last Post: 12-15-2000, 10:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center