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 }