/** * Class for repeatedly simulating a roulette game and determining odds of winning. * * @author Dave Reed * @version 11/04/09 */ public class RouletteTester { private static final int NUM_GAMES = 100000; // number of games to play private static final int NUMBER_DEFAULT = 7; // default for a number bet private static final String COLOR_DEFAULT = "red"; // default for a color bet /** * Constructor for objects of class RouletteTester */ public RouletteTester() { // currently does nothing } /** * Simulates a single roulette game, playing until the player doubles * the initial bankroll or else runs out of credits. * @param startAmount the player's initial bankroll (assume it's > 0) * @param betAmount the amount for each successive bet (assume it's > 0) * @param betType either "number" or "color" * @return the final bankroll after playing the game */ public int singleGame(int startAmount, int betAmount, String betType) { RouletteGame game = new RouletteGame(); game.addCredits(startAmount); while (game.checkCredits() > 0 && game.checkCredits() < 2*startAmount) { int nextBet = Math.min(betAmount, game.checkCredits()); if (betType.equals("color")) { game.makeBet(nextBet, RouletteTester.COLOR_DEFAULT); } else if (betType.equals("number")) { game.makeBet(nextBet, RouletteTester.NUMBER_DEFAULT); } } return game.checkCredits(); } /** * Simulates repeated roulette games, playing each game until the player doubles * the initial bankroll or else runs out of credits. * @param startAmount the player's initial bankroll (assume it's > 0) * @param betAmount the amount for each successive bet (assume it's > 0) * @param betType either "number" or "color" * @return the percentage of wins out of RouletteTester.NUM_GAMES games */ public double repeatedGames(int startAmount, int betAmount, String betType) { int numWins = 0; for (int i = 0; i < RouletteTester.NUM_GAMES; i++) { int finalWinnings = this.singleGame(startAmount, betAmount, betType); if (finalWinnings > 0) { numWins++; } } return 100.0*numWins / RouletteTester.NUM_GAMES; } }