import java.util.*; interface Die { Die roll(); Integer valueOf(); } interface CarnivalGame { CarnivalGame recordBets(String[] bets); void announceWinners(); } interface DiceGame { DiceGame analyzeDice(); DiceGame rollDice(); DiceGame showDice(); } abstract class AbstractDie implements Die { private Integer value; protected AbstractDie() { value = nextValue(); } public abstract int numberOfSides(); public final Die roll() { value = nextValue(); return this; } public String toString() { return value.toString(); } public final Integer valueOf() { return value; } protected abstract int nextValue(); } abstract class AbstractFairDie extends AbstractDie { protected final int nextValue() { return (int)(1 + Math.random() * numberOfSides()); } } class Dodecahedron extends AbstractFairDie { public int numberOfSides() { return 12; } } class FairSixSidedAsciiDie extends Dodecahedron { private static final String[] sides = { "---------\n" + "| |\n" + "| 1 |\n" + "| |\n" + "---------" , "---------\n" + "| |\n" + "| 2 |\n" + "| |\n" + "---------" , "---------\n" + "| |\n" + "| 3 |\n" + "| |\n" + "---------" , "---------\n" + "| |\n" + "| 4 |\n" + "| |\n" + "---------" , "---------\n" + "| |\n" + "| 5 |\n" + "| |\n" + "---------" , "---------\n" + "| |\n" + "| 6 |\n" + "| |\n" + "---------" , "---------\n" + "| |\n" + "| 7 |\n" + "| |\n" + "---------" , "---------\n" + "| |\n" + "| 8 |\n" + "| |\n" + "---------" , "---------\n" + "| |\n" + "| 9 |\n" + "| |\n" + "---------" , "---------\n" + "| |\n" + "| 10 |\n" + "| |\n" + "---------" , "---------\n" + "| |\n" + "| 11 |\n" + "| |\n" + "---------" , "---------\n" + "| |\n" + "| 12 |\n" + "| |\n" + "---------" }; public String toString() { return sides[valueOf() - 1]; } } class Bread implements DiceGame, CarnivalGame{ protected Die[] RedDie, BlackDie; protected String[] Bets = {"Red", "Black", "Bread"}; protected int Wager; public String Winner; protected int RedDieSum, BlackDieSum; public Bread (){ rollDice(); showDice(); sumDice(); analyzeDice(); announceWinners(); } public DiceGame showDice(){ System.out.print("Red Dice: \n"); for(int i = 0; i < RedDie.length; i++){ System.out.print(RedDie[i] + "\n"); } System.out.print("\nBlack Dice: \n"); for(int i = 0; i < BlackDie.length; i++){ System.out.print(BlackDie[i] + "\n"); } return this; } public DiceGame rollDice(){ Die[] RedDice = new Die[3]; for(int i = 0; i < 3; i++){ RedDice[i] = new FairSixSidedAsciiDie(); } this.RedDie = RedDice; Die[] BlackDice = new Die[3]; for(int j = 0; j < 3; j++){ BlackDice[j] = new FairSixSidedAsciiDie(); } this.BlackDie = BlackDice; return this; } public DiceGame analyzeDice(){ if(RedDieSum > BlackDieSum){ this.Winner = Bets[0]; } else if(BlackDieSum > RedDieSum){ this.Winner = Bets[1]; } else{ this.Winner = Bets[2]; } return this; } public DiceGame sumDice(){ int RedDieSum = 0; int BlackDieSum = 0; for(int i = 0; i < RedDie.length; i++){ RedDieSum = RedDie[i].valueOf() + RedDieSum; BlackDieSum = BlackDie[i].valueOf() + BlackDieSum; } this.RedDieSum = RedDieSum; this.BlackDieSum = BlackDieSum; return this; } public void announceWinners(){ System.out.print("\n" + Winner + " won!"); } public CarnivalGame recordBets(String[] bets){ return this; } public void handleWager(int wager, String guess){ if(guess.equals(Winner)){ if(Winner.equals(Bets[2])){ System.out.println("\nYou Won " + wager*20 + " Chash M0ney Dollars"); }else{ System.out.println("\nYou Won " + wager*2 + " Chash M0ney Dollars"); } }else{ System.out.println("\nYou Lost"); } } } /** Chuck-a-luck is played with three standard dice that are kept in a device * shaped somewhat like an hourglass that resembles a wire-frame bird cage * and pivots about its centre. The dealer rotates the cage end over end, * with the dice landing on the bottom. Wagers are placed based on possible * combinations that can appear on the three dice. * * Source: https://en.wikipedia.org/wiki/Chuck-a-luck * * WAGERS * * Single: A specific number will appear. * Triple: Any of the triples (all three dice show the same number) will appear. * Big: The total score will be 11 (sometimes 12) or higher and not a triple. * Small: The total score will be 10 (sometimes 9) or lower and not a triple. * Field: The total score will be outside the range of 8 to 12 (inclusive). */ class ChuckALuck implements DiceGame, CarnivalGame { public static final String Triple = "Triple", Big = "Big", Small = "Small", Field = "Field"; protected final Die[] dice; protected final List<String> results = new LinkedList<String>(); protected String[] bets; public ChuckALuck(Dodecahedron[] dice) { if (dice.length != 3) { throw new IllegalArgumentException("Wrong number of dice."); } this.dice = dice; } public ChuckALuck analyzeDice() { final Integer v1 = dice[0].valueOf(), v2 = dice[1].valueOf(), v3 = dice[2].valueOf(); final int sum = v1 + v2 + v3; results.clear(); if (v1 == v2 && v2 == v3) { results.add(Triple); } else if (sum >= 11) { results.add(Big); } else if (sum <= 10 ) { results.add(Small); } if (sum < 8 || sum > 12) { results.add(Field); } return this; } public void announceWinners() { for (int i = 0; i < bets.length; i++) { String bet = bets[i]; if (results.indexOf(bet) >= 0) { System.out.println("Bet " + (i + 1) + " (" + bet + ") wins!"); } } } public ChuckALuck recordBets(String[] bets) { this.bets = bets; return this; } public ChuckALuck rollDice() { for (Die die : dice) die.roll(); return this; } public ChuckALuck showDice() { for (Die die : dice) System.out.println(die); return this; } public String toString() { return "Chuck-a-luck"; } } class AlternateChuckALuck extends ChuckALuck { public AlternateChuckALuck(Dodecahedron[] dice){ super(dice); } public ChuckALuck analyzeDice() { final Integer v1 = dice[0].valueOf(), v2 = dice[1].valueOf(), v3 = dice[2].valueOf(); final int sum = v1 + v2 + v3; results.clear(); if (v1 == v2 && v2 == v3) { results.add(Triple); } else if (sum >= 12) { results.add(Big); } else if (sum <= 9 ) { results.add(Small); } if (sum < 8 || sum > 12) { results.add(Field); } return this; } } final class Carny { public static void acceptBets(CarnivalGame game, String[] bets) { game.recordBets(bets); } public static void payWinners(CarnivalGame game) { game.announceWinners(); } public static void playGame(DiceGame game) { game.rollDice(); game.showDice(); game.analyzeDice(); } } public class Dealer { private static final FairSixSidedAsciiDie[] diceCage = { new FairSixSidedAsciiDie(), new FairSixSidedAsciiDie(), new FairSixSidedAsciiDie() }; public static void main(String args[]) { final Bread game = new Bread(); game.handleWager(Integer.parseInt(args[1]), args[0]); //First commandLine argument is the guess, second is the wager. } }