//CS5-HMC-Sp.,00-Lab5 //A. Khakpour, Mar., 00 import java.io.*; import java.util.*; class Roulette1 { public static void main (String args[]) throws IOException { Player player = new Player (100); boolean done = false; System.out.println ("Cash available: " + player.cash()); System.out.println(); while (!done) { player.make_bet(); Wheel.spin(); player.payment(); System.out.println ("Cash available: " + player.cash()); done = !player.spin_again(); System.out.println(); } } } class Player { private int bet, money; private BufferedReader stdin; private int bet_type; private int number; public Player (int initial_money) { money = initial_money; stdin = new BufferedReader (new InputStreamReader(System.in)); } public int cash() { return money; } public void make_bet() throws IOException { bet_type = Wheel.bet_options (stdin); if (bet_type == Wheel.NUMBER) { System.out.print("Enter a number from 0 to 36 to bet on: "); number = Integer.parseInt (stdin.readLine()); } System.out.print ("How much to bet: "); bet = Integer.parseInt (stdin.readLine()); if (money >= bet) money -= bet; else { money = 0; System.out.println ("Betting it all!"); } } public boolean spin_again() throws IOException { String answer; System.out.print ("Spin again [y/n]? "); answer = stdin.readLine(); return (answer.equals("y") || answer.equals("Y")); } public void payment() { int a = Wheel.payoff(bet, bet_type, number); System.out.println("The payoff amount is: " + a); money += a; } } class Wheel { final static int RED = 1; final static int BLACK = 2; final static int NUMBER = 3; private static int ball_position; private static int color; private final static int POSITIONS = 37; public static int bet_options (BufferedReader stdin) throws IOException { 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 Integer.parseInt (stdin.readLine()); } public static void spin() { ball_position = (int) (Math.random() * POSITIONS); color = ball_position % 2; if (color == 0) System.out.println("The color is RED"); else System.out.println("The color is BLACK"); System.out.println("The ball position is: " + ball_position); return; } public static int payoff (int bet_amount, int bet_type, int number_bet) { int amount_won; if (bet_type == NUMBER) amount_won = number_payoff(bet_amount, number_bet); else amount_won = red_black_payoff(bet_amount, bet_type); return amount_won; } private static int number_payoff(int bet_amount, int number_bet) { int payoff_amount; if (number_bet == ball_position) payoff_amount = 36 * bet_amount; else payoff_amount = 0; return payoff_amount; } private static int red_black_payoff(int bet_amount, int bet_type) { int payoff_amount; if (color == bet_type - 1) payoff_amount = 2 * bet_amount; else payoff_amount = 0; return payoff_amount; } }