# Need some help with my Roulette game please.

• April 15th, 2012, 12:47 AM
iDizzle
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
Code :

```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
Code :

```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
Code :

``` 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 }```
• April 15th, 2012, 05:35 AM
KucerakJM
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):
Code java:

```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.
• April 15th, 2012, 02:34 PM
iDizzle
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?
• April 15th, 2012, 03:59 PM
KucerakJM
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.
• April 15th, 2012, 05:35 PM
iDizzle
Re: Need some help with my Roulette game please.
Quote:

Originally Posted by KucerakJM
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.