Your goal is to write a ruby program that simulates the spining of a roulette wh
ID: 3751881 • Letter: Y
Question
Your goal is to write a ruby program that simulates the spining
of a roulette wheel (for which you
will use a random generator), the betting on one color, and the decision of where there is a payoff
or not.
You will also simulate the double of the losing bet, and resetting back to the base bet after
winning. You also
need to keep track of the total amount of money you have (which will call your
bank) to determine if you
can make the next bet or not.
Then you want to see if you can double
your bank of
money by using this strategy.
You also need to have the correct odds
(the correct
number of slots and correct color) as well as the correct pay off.
When you lose, you lose your
bet.
When you win, you keep your bet, plus earn the same as your bet (i.e. double your bet).
Explanation / Answer
import java.util.*;
class Roulette
{
// declare the required variables
private static int houseMoney=0;
// main method to process the roulette game
public static void main (String[] args)
{
// create object for wheel class
Wheel wl1 = new Wheel();
Scanner scan = new Scanner(System.in);
// create object for the player class
Player pl1 = new Player ("John", 100);
Player pl2 = new Player("Mary", 100);
Player pl3 = new Player("David", 100);
boolean bl1 = true;
boolean bl2 = true;
boolean bl3 = true;
Wheel.welcomeMessage();
//loop for iteration
while (bl1 || bl2 || bl3)
{
// initiate the values
if(bl1)pl1.makeBet(wl1);
if(bl2)pl2.makeBet(wl1);
if(bl3)pl3.makeBet(wl1);
wl1.methSpin();
// check the boolean value
if(bl1)
{
//initiate the payment
pl1.payment(wl1);
System.out.println ("Money available for " + pl1.getName() + ": " + pl1.getMoney());
// check weather play the game again
if(!pl1.playAgain(scan))
{
bl1 = false;
System.out.println(pl1.getName()+" total
wins/losses: " + pl1.getNet());
}
}
// check the boolean value
if(bl2)
{
//initiate the payment
pl2.payment(wl1);
System.out.println ("Money available for " +
pl2.getName() + ": " + pl2.getMoney());
// check weather play the game again
if(!pl2.playAgain(scan))
{
bl2 = false;
System.out.println(pl2.getName()+ "
total wins/losses: " + pl2.getNet());
}
}
// check the boolean value
if(bl3)
{
pl3.payment(wl1);
System.out.println ("Money available for " +
pl3.getName() + ": " + pl3.getMoney());
// check weather play the game again
if(!pl3.playAgain(scan))
{
bl3 = false;
System.out.println(pl3.getName()+"
total wins/losses: " + pl3.getNet());
}
}
System.out.println();
}
System.out.printf("House %s: $%d", houseMoney > 0 ?
"Wins" : "Losses", houseMoney);
System.out.println();
System.out.println ("Game over! Thanks for
playing.");
}
public static void setHouseWins(int Winnings)
{
houseMoney += Winnings;
}
}
// Class to represents a roulette wheel
class Wheel
{
// public name constants -- accessible to others
public final static int BLACK = 0; // even numbers
public final static int RED = 1; // odd numbers
public final static int GREEN = 2; // 00 OR 0
public final static int NUMBER = 3; // number bet
public final static int MIN_NUM = 1; // smallest
number to bet
public final static int MAX_NUM = 36; // largest
number to bet
// private name constants -- internal use only
private final static int MAX_POSITIONS = 38; // number of
positions on wheel
private final static int NUMBER_PAYOFF = 35; // payoff
for number bet
private final static int COLOR_PAYOFF = 2; // payoff
for color bet
// private variables -- internal use only
private static int ballPosition; // 00, 0, 1
.. 36
private static int color; // GREEN,
RED, OR BLACK
//=========================================================
// Presents welcome message
//=========================================================
public static void welcomeMessage()
{
System.out.println("Welcome to a simple version of
roulette game.");
System.out.println("You can place a bet on black, red,
or a number.");
System.out.println("A color bet is paid " + COLOR_PAYOFF
+ " the bet amount.");
System.out.println("A number bet is paid " +
NUMBER_PAYOFF + " the bet amount.");
System.out.println("Have fun and good luck! ");
}
//=========================================================
// Presents betting options
//=========================================================
public static void betOptions()
{
System.out.println("Betting Options:");
System.out.println(" 1. Bet on black");
System.out.println(" 2. Bet on red");
System.out.println(" 3. Bet on a number between " +
MIN_NUM + " and " + MAX_NUM);
System.out.println();
}
// method for spin the wheel
public int methSpin()
{
ballPosition= (int)(Math.random() * 38 + 1);
if(ballPosition < 37)
{
if(ballPosition % 2 == 0)
{
color = BLACK;
}
else
{
color = RED;
}
}
else
{
color = GREEN;
}
System.out.println("Result: "+ ((color < 1) ? "Black " :
"Red ") + ballPosition);
return ballPosition;
}
// method for payoff
public int Payoff(int btAmt, int btType, int btNmbt)
{
if(color == GREEN)
{
return 0;
}
else if(btType == 1 && color == BLACK)
{
return btAmt * COLOR_PAYOFF;
}
else if( btType == 2 && color == RED)
{
return btAmt * COLOR_PAYOFF;
}
else if (btType == 3 && btNmbt == ballPosition)
{
return btAmt * NUMBER_PAYOFF;
}
else
{
return 0;
}
}
}
// Class to represents one roulette player
class Player
{
private int bet, money, betType, betNumber, betNet;
private static int houseWins;
private String name;
private Scanner scan = new Scanner(System.in);
//=========================================================
// The Player constructor sets up name and initial available money.
//=========================================================
public Player (String playerName, int initialMoney)
{
name = playerName;
money = initialMoney;
} // constructor Player
//=========================================================
// Returns this player's name.
//=========================================================
public String getName()
{
return name;
} // method getName
//=========================================================
// Returns this player's current available money.
//==========================================================
public int getMoney()
{
return money;
} // method getMoney
public int getNet()
{
return betNet;
}
//=========================================================
// Prompts the user and reads betting information.
//==========================================================
public void makeBet(Wheel wheel)
{
System.out.print("How much to bet for " + name + ": "
);
bet = scan.nextInt();
if(bet >= money)
{
bet = money;
System.out.println("Betting it all in! ");
}
money = money - bet;
wheel.betOptions();
betType = scan.nextInt();
if(betType == 3){
while(true){
System.out.println("Please enter the number you
wish to bet on from 1 to 36: ");
betNumber = scan.nextInt();
if(betNumber <1 || betNumber > 36){
System.out.println("This Number is not in
the desired range.");
}else
{
break;
}
}
}
} // method makeBet
//==========================================================
// Determines if the player wants to play again.
//===========================================================
public boolean playAgain(Scanner scan)
{
String answer;
System.out.println (name + " Play again [Y/N]? ");
answer = scan.nextLine();
return (answer.equals("y") || answer.equals("Y"));
} // method playAgain
public void payment(Wheel wheel2)
{
int winnings = wheel2.Payoff(bet, betType, betNumber);
money += winnings;
if(winnings > 0)
{
betNet += (winnings-bet);
Roulette.setHouseWins(-(winnings-bet));
}
else
{
betNet -= bet;
Roulette.setHouseWins((bet));
}
if(money < 1)
{
Scanner scan = new Scanner(System.in);
System.out.println(name + ", you're out of money. Do
you want to bet more? [Y/N] ");
String answer= scan.nextLine().toLowerCase();
if(answer.equals("y"))
{
System.out.println("How much would you like to
add?");
int amount= scan.nextInt();
money += amount;
}
}
//setHouseWins(betNet);
System.out.println("Testing to see how much is betNet
keeping track of: " + betNet);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.