Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Need some help with my Roulette game please.

  1. #1
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Unhappy Need some help with my Roulette game please.

    Hi everyone, I'm working on a roulette game and am a pretty confused. I have a sheet with instructions on what to do with 2 classes and the main program which were provided in 3 files. I'm stuck on step 7 and 8.

    Step 7 says to create a method called spin in the Wheel class that determines and sets the ballPosition variable. Insert a call to the spin method in the main loop after the player makes their bet.

    So in the Wheel class below you can see that I created the Spin method and implemented it into the main program called Roulette. But when I run the program the output I'm getting is "You spun a: Wheel@10b61fd1". Am I doing something wrong when I use the Math class to generate the number for the wheel spin?

    Step 8 says, In the spin method, use the remainder operator % to determine the color of the current ball position, and set the color variable of the Wheel class. Make it correspond to the RED, BLACK or GREEN constants already defined in the Wheel class.

    So I'm totally confused on step 8. How am I supposed to use the modulus operator to do this? I know that the modulus operator divides and only returns the remainder. Am I supposed to divide one of these values and use the remainder to determine the color whether it be 0(GREEN), 1(RED) or 2(BLACK)?

    Help would be very much appreciated, thanks in advance!

    The main program, Roulette
    import java.util.*;
     
    //************************************************************************
    //   Class Roulette contains the main driver for a roulette betting game.
    //************************************************************************
    public class Roulette
    {
       	//=====================================================================
       	//  Contains the main processing loop for the roulette game.
       	//=====================================================================
       	public static void main (String[] args)
       	{
    	  	Wheel wheel1 = new Wheel();
          	Player player1 = new Player (100);   // $100 to start
          	Player player2 = new Player (50);   // $50 to start
          	boolean done = false;
     
          	while (!done)
          	{
          		player1.makeBet(wheel1);
     
          		wheel1.Spin();
          		//System.out.println("You spun a: " + wheel1);
     
             	System.out.println ("Money available: " + player1.getMoney());
             	done = !player1.playAgain();
             	System.out.println();
     
          	}
      	}
    }

    The class, Wheel
    import java.util.*;
     
    //************************************************************************
    //   Class Wheel represents a roulette wheel and its operations.
    //************************************************************************
    public class Wheel
    {
       	public  final int GREEN     =  0;
       	public  final int RED       =  1;
       	public  final int BLACK     =  2;
       	public  final int NUMBER    =  3;
       	private final int POSITIONS = 38;
       	private int ballPosition;
       	private int color;
     
       	//=====================================================================
       	//  Presents betting options and reads player choice.
       	//=====================================================================
       	public int betOptions (Scanner scan)
       	{
          	System.out.println ("1. Bet on red");
          	System.out.println ("2. Bet on black");
          	System.out.println ("3. Bet on a particular number");
          	System.out.print   ("Your choice? ");
          	return scan.nextInt();
       	}
       	public int Spin()
       	{
    	ballPosition=(int)(Math.random()*38+1);
     
       	return ballPosition;
     
    	}
    }

    The class, Player
      	public int getMoney()
       	{
          	return money;
       	}  // method getMoney
     
     
       	//=====================================================================
       	//  Prompts the user and reads betting information.
       	//=====================================================================
       	public void makeBet(Wheel wheel)
       	{
          	System.out.print ("How much to bet: ");
          	bet = scan.nextInt();
          	if(bet > money)
          	{
          		bet=money;
          		System.out.println("All in!");
          	}
          			money = money - bet;
          			betType=wheel.betOptions(scan);
          			if(betType == 3)
          			{
          				System.out.println("Enter the number you wish to bet on");
          				betType=scan.nextInt();
     
          			}
     
       	} 
     
       	//=====================================================================
       	//  Determines if the player wants to play again.
       	//=====================================================================
       	public boolean playAgain()
       	{
          	String answer;
     
          	System.out.print ("Play again [y/n]? ");
          	answer = scan.next();
          	return (answer.equals("y") || answer.equals("Y"));
       	}  // method playAgain
    }


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Need some help with my Roulette game please.

    Hello iDizzle,
    Let's start with your first issue in step 7. Technically, the printout is correct because you have supplied System.out with an object that can not be implicitly converted to a String, and thus it is giving the String representation of that object. If you want to print the position then you could always create a get method in the Wheel class for ballPosition which returns the value and then utilize that method in the main program. Check this out if that doesn't make sense: Adding Setter and Getter Methods - The Java EE 6 Tutorial
    As for step 8, after looking at a roulette betting table the pattern for red and black numbers can be broken down as follows:
    For numbers 1-10; evens are black | odds are red
    For numbers 11-18; odds are black | evens are red
    For numbers 19-28; evens are black | odds are red
    For numbers 29-36; odds are black | evens are red
    Obviously only 0 is green.
    So basically there are 4 groupings of numbers. Knowing that makes things much easier since you can now figure which group the ballPosition belongs in and assign a color accordingly.
    Here is a simple example for modulus (% operator):
    public class modulusTest {
        public static void main(String[] args) {
    	int[] testIntArray = {2045, 18, 30, 21, 0};
     
    	for(int i = 0; i < testIntArray.length; i++) {
    		if(testIntArray[i] == 0) {// If the test integer is 0 don't bother with modulus since 0%[anynumber] = 0
    			System.out.println("The number was 0");
    		} else if(testIntArray[i] % 5 == 0) { // Check if the number is a multiple of 5
    			System.out.println("The number was a multiple of 5");
    		} else {
    			System.out.println("The number was not a multiple of 5");
    		}
    	}
        }
    }

    Hopefully this helps you out.

  3. #3
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need some help with my Roulette game please.

    Ok Thank you. For Step 7 I believe the output should be cleared up later on in the instructions since there are 12 steps to the assignment. If not than I will create a "get" method.

    So for Step 8 it does make it easier to be able to visualize the groupings of the numbers but what number should I divide them by with the modulus operator to insure that the remainder is 0-2?

  4. #4
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Need some help with my Roulette game please.

    I don't know about ensuring the remainder is 0-2, but you can figure out if the number is even if [number]%2 is equal to 0.

  5. #5
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need some help with my Roulette game please.

    Quote Originally Posted by KucerakJM View Post
    I don't know about ensuring the remainder is 0-2, but you can figure out if the number is even if [number]%2 is equal to 0.
    OOhh okay. That makes sense. Thank you! I will work at it.

Similar Threads

  1. Simple game that requires me to load game settings from a file
    By 14fenix in forum Java Theory & Questions
    Replies: 5
    Last Post: December 1st, 2011, 08:21 PM
  2. TIC-TAC-TOE GAME
    By umerahmad in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 29th, 2011, 12:15 PM
  3. Need Help with Game
    By Shivam24 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 18th, 2011, 03:58 PM
  4. Game !
    By Ahmed.Ibrahem in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2011, 07:01 PM
  5. Game 3x3
    By Koren3 in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 20th, 2009, 07:43 PM