import java.util.*;
/***************/
/* THE PROGRAM */
/***************/
/** A place to play a game involving a single roll of one or more sets of dice.
*
* In North America "joint" refers to any kind of carnival stand. In other parts of
* the world, it is a slang term for a gathering place. On the fairground it is used
* as a generic term for any form of portable sideshow of the ground booth variety.
* Walkup shows, which have raised platforms for the performers, are not so described.
*
* Source: https://conklinshows.com/info/dictionary/j.html
*/
public class CarnivalJoint
{
/** Gets and oversees (begins, conducts, ends) a game.
* If no game is specified, display help message.
* If an invalid game is specified, an exception is thrown.
*/
public static void main(String args[]) {
if (args.length == 0) {
System.out.println(help);
return;
}
final String gameKey = args[0];
final CarnivalDiceGame game = getDiceGame(gameKey);
final Croupier employee = new Carny(game);
System.out.println(gameKey + ": " + game);
employee.beginGame(args).conductGame().endGame();
}
private static String help =
"Play a single round of a dice game that accepts wagers.\n" +
"\n" +
"COMMAND LINE ARGUMENTS\n" +
"\n" +
"args[0]: Game key; selects a game; default is Chuck-a-luck.\n" +
"args[i] (i > 0): Zero or more game-specific wagers.\n" +
"\n" +
"GAME KEY EXAMPLES\n" +
"\n" +
"CAL, CAL6, CAL-6: Typical chuck-a-luck using regular six-sided dice.\n" +
"ACAL, ACAL-6: Alternate chuck-a-luck (six-sided dice).\n" +
"CAL12, ACAL-12: Chuck-a-luck using 12-sided dice (dodecahedrons).\n" +
"HR, play a fun game of high roll with your mates! this includes 3 6-sided die!" +
"\n" +
"COMMAND LINE ARGUMENTS EXAMPLES\n" +
"\n" +
"CAL-6 Big Triple 12 24\n" +
"\n" +
"Typical chuck-a-luck using six-sided dice, wagering that the sum of the\n" +
"values shown on each die will be a 'Big' value or a 'Triple' or 12 or 24.\n" +
"\n" +
"Risk 3 2\n" +
"\n" +
"Attacker rolls 3; defender rolls 2; outputs number of armies lost by whom.\n" +
"\n" +
"RisiKo! 3 3\n" +
"\n" +
"Attacker rolls 3; defender rolls 3; outputs number of armies lost by whom.";
/* The joint's dice sets. */
private static final Die[] twelveSidedDiceCage = {
new Dodecahedron(),
new Dodecahedron(),
new Dodecahedron()
};
private static final Die[] sixSidedDiceCage = {
new FairSixSidedAsciiDie(),
new FairSixSidedAsciiDie(),
new FairSixSidedAsciiDie()
};
private static final Die[] redRiskDice = {
new FairSixSidedAsciiDie(),
new FairSixSidedAsciiDie(),
new FairSixSidedAsciiDie()
};
private static final Die[] whiteRiskDice = {
new FairSixSidedAsciiDie(),
new FairSixSidedAsciiDie(),
new FairSixSidedAsciiDie()
};
private static final Die[] highRollDice = {
new FairSixSidedAsciiDie(),
new FairSixSidedAsciiDie(),
new FairSixSidedAsciiDie()
};
/** Given a game key, returns a game or throws an exception. */
private static CarnivalDiceGame getDiceGame(String key) {
// Standard Risk or RisiKo!
if (key.indexOf("Risk") >=0)
return new StandardRisk(redRiskDice, whiteRiskDice);
if (key.indexOf("RisiKo!") >= 0)
return new RisiKo(redRiskDice, whiteRiskDice);
// Chuck-a-luck or Alternate Chuck-a-luck
final Die[] diceCage = (key.indexOf("12") >= 0) ?
twelveSidedDiceCage : sixSidedDiceCage;
if (key.indexOf("ACAL") >= 0)
return new AlternateChuckALuck(diceCage);
if (key.indexOf("CAL") >= 0)
return new ChuckALuck(diceCage);
if (key.indexOf("HR") >= 0)
return new HighRoll(sixSidedDiceCage);
// Bad game key.
throw new IllegalArgumentException("Invalid game key:" + key);
}
}
/**************/
/* INTERFACES */
/**************/
/** An object that can begin a game (with inputs), conduct a game, and end a game.
*
* A croupier is someone appointed at a gambling table to assist in the conduct of the game,
* especially in the distribution of bets and payouts. Croupiers are typically employed by
* casinos. Originally a "croupier" meant one who stood behind a gambler, with extra reserves
* of cash to back him up during a gambling session. The word derived from "croup" (the rump
* of a horse) and was by way of analogy to one who rode behind on horseback. It later came to
* refer to one who was employed to collect the money from a gaming-table.
*
* Source: https://en.wikipedia.org/wiki/Croupier
*/
interface Croupier
{
Croupier beginGame(String[] inputs);
Croupier conductGame();
Croupier endGame();
}
interface CarnivalGame
{
CarnivalGame processInputs(String[] inputs);
CarnivalGame printResults();
}
interface DiceGame
{
DiceGame analyzeDice();
DiceGame rollDice();
DiceGame showDice();
}
interface CarnivalDiceGame extends CarnivalGame, DiceGame
{
}
interface Die
{
Die roll();
Integer valueOf();
}
/********************/
/* CARNIVAL WORKERS */
/********************/
/** A Croupier that oversees a game.
*
* Carny, also spelled carnie, is an informal term used in North America
* for a traveling carnival employee, and the language they use, particularly
* when the employee plays a game ("joint"), food stand ("grab" or "popper"),
* or ride at a carnival. The term "showie" is used synonymously in Australia.
* Carny is thought to have become popularized around 1931 in North America,
* when it was first colloquially used to describe one who works at a carnival.
* The word carnival, originally meaning a "time of merrymaking before Lent,"
* came into use circa 1549.
*
* Source: https://en.wikipedia.org/wiki/Carny
*/
final class Carny implements Croupier
{
private CarnivalDiceGame game;
public Carny(CarnivalDiceGame game) {
this.game = game;
}
public Carny beginGame(String[] inputs) {
game.processInputs(inputs);
return this;
}
public Carny endGame() {
game.printResults();
return this;
}
public Carny conductGame() {
game.rollDice().showDice().analyzeDice();
return this;
}
}
/*********/
/* GAMES */
/*********/
/** A game of chance involving a single roll of two sets of dice similar to a roll used to
* determine the units lost in round of a battle in variations of the board game Risk.
* Inputs are the numbers of dice rolled by an attacker and a defender. The output is the
* dice rolled grouped by attacker and defender, sorted by value, and the number of armies
* lost and by either the attacker or the defender or both.
*
* Risk is a strategy board game of diplomacy, conflict and conquest for two to six players.
* The standard version is played on a board depicting a political map of Earth, divided into
* forty-two territories, which are grouped into six continents. Turn rotates among players
* who control armies of playing pieces with which they attempt to capture territories from
* other players, with results determined by dice rolls.
*
* See: https://en.wikipedia.org/wiki/Risk_(game)
*/
abstract class AbstractRisk implements CarnivalDiceGame
{
private final int maxRed;
private final int maxWhite;
private final Die[] redDice;
private final Die[] whiteDice;
protected final List<Die> attackingDice = new LinkedList<Die>();
protected final List<Die> defendingDice = new LinkedList<Die>();
protected int offensiveCasualties = 0;
protected int defensiveCasualties = 0;
protected AbstractRisk(Die[] redDice, int maxRed, Die[] whiteDice, int maxWhite) {
if (redDice.length < maxRed) {
throw new IllegalArgumentException("Too few red dice.");
}
if (whiteDice.length < maxWhite) {
throw new IllegalArgumentException("Too few white dice.");
}
this.redDice = redDice;
this.whiteDice = whiteDice;
this.maxRed = maxRed;
this.maxWhite = maxWhite;
}
public abstract AbstractRisk analyzeDice();
public AbstractRisk printResults() {
reportCasualties("Attacker", offensiveCasualties);
reportCasualties("Defender", defensiveCasualties);
return this;
}
public AbstractRisk processInputs(String[] inputs) {
if (inputs.length < 3) {
throw new IllegalArgumentException("Too few inputs.");
}
selectDice(Integer.parseInt(inputs[1]), Integer.parseInt(inputs[2]));
return this;
}
public AbstractRisk rollDice() {
for (Die redDie : attackingDice) {
redDie.roll();
}
Collections.sort(attackingDice, AbstractDie.descendingComparator);
for (Die whiteDie : defendingDice) {
whiteDie.roll();
}
Collections.sort(defendingDice, AbstractDie.descendingComparator);
return this;
}
public AbstractRisk showDice() {
System.out.println("Attacking (Red)");
for (Die redDie : attackingDice) {
System.out.println(redDie);
}
System.out.println("Defending (White)");
for (Die whiteDie : defendingDice) {
System.out.println(whiteDie);
}
return this;
}
public String toString() {
return "A round of a battle in a game of Risk.";
}
private void selectDice(final int attackWager, final int defendWager) {
verifyWagers(attackWager, defendWager);
attackingDice.clear();
for (int i = 0; i < attackWager; i++) {
attackingDice.add(redDice[i]);
}
defendingDice.clear();
for (int i = 0; i < defendWager; i++) {
defendingDice.add(whiteDice[i]);
}
}
private void verifyWagers(final int attackWager, final int defendWager) {
if (attackWager < 1 || attackWager > maxRed) {
throw new IllegalArgumentException("Offensive wager out of range: " + attackWager);
}
if (defendWager < 1 || defendWager > maxWhite) {
throw new IllegalArgumentException("Defensive wager out of range: " + defendWager);
}
}
private static void reportCasualties(final String who, final int count) {
if (count == 0) return;
System.out.print(who + " loses " + count + " arm");
System.out.println(count == 1 ? "y." : "ies.");
}
}
/** An instance of an AbstractRisk game that adopts rules of a standard game of Risk.
*
* Defenders always win ties when dice are rolled. This gives the defending player the advantage
* in "one-on-one" fights, but the attacker's ability to use more dice offsets this advantage.
* It is always advantageous to roll the maximum number of dice, unless an attacker wishes to
* avoid moving men into a 'dead-end' territory, in which case they may choose to roll fewer than
* three. Thus when rolling three dice against two, three against one, or two against one, the
* attacker has a slight advantage, otherwise the defender has an advantage.
*
* See also: https://en.wikipedia.org/wiki/Risk_(game)
*/
class StandardRisk extends AbstractRisk
{
protected StandardRisk(Die[] redDice, int maxRed, Die[] whiteDice, int maxWhite) {
super(redDice, maxRed, whiteDice, maxWhite);
}
public StandardRisk(Die[] redDice, Die[] whiteDice) {
this(redDice, 3, whiteDice, 2);
}
/**
* Precondition: Wagers have been made, dice have been rolled, and the
* attacking and defending dice have been arranged in order from largest
* to smallest value.
*/
public StandardRisk analyzeDice() {
defensiveCasualties = 0;
offensiveCasualties = 0;
final int n = attackingDice.size();
final int m = defendingDice.size();
for (int i = 0; i < n && i < m; i++) {
if (attackingDice.get(i).valueOf() > defendingDice.get(i).valueOf()) {
defensiveCasualties++;
}
else {
offensiveCasualties++;
}
}
return this;
}
}
/** An instance of an AbstractRisk game that adopts rules of RisiKo! (a variation of Risk).
*
* RisiKo! derives from the 1957 French game La ConquĂȘte du Monde, better known worldwide as Risk.
* RisiKo! is a variant of the game released in Italy, in which the defender is allowed to roll up
* to three dice to defend. This variation dramatically shifts the balance of power towards defense.
*
* See: https://en.wikipedia.org/wiki/RisiKo!
* See also: https://en.wikipedia.org/wiki/Risk_(game)
*/
class RisiKo extends StandardRisk
{
public RisiKo(Die[] redDice, Die[] whiteDice) {
super(redDice, 3, whiteDice, 3);
}
public String toString() {
return "A round of battle in a game of RisiKo!";
}
}
/** A partial simulation of the dice game Chuck-a-luck that determines winners
* but does not compute gains or losses based on odds.
*
* Chuck-a-luck, also known as birdcage, is a game of chance played with three
* dice. It is derived from grand hazard and both can be considered a variant
* of sic bo, which is a popular casino game, although chuck-a-luck is more of
* a carnival game than a true casino game. The game is sometimes used as a
* fundraiser for charity.
*
* 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.
*
* WAGER DESCRIPTION
* n A specific number will appear. (Known as "Single".)
* Triple Any of the triples (all three dice show the same number) will appear.
* Big The total score will be 11 (alternatively 12) or higher and not a triple.
* Small The total score will be 10 (alternatively 9) or lower and not a triple.
* Field The total score will be outside the range of 8 to 12 (inclusive).
*
* Source: https://en.wikipedia.org/wiki/Chuck-a-luck
*/
abstract class AbstractChuckALuck implements CarnivalDiceGame
{
public static final String
Triple = "Triple", Big = "Big", Small = "Small", Field = "Field";
protected final Die[] dice;
protected final List<String> results = new LinkedList<String>();
private String[] inputs;
public AbstractChuckALuck(Die[] dice) {
if (dice.length != 3) {
throw new IllegalArgumentException("Wrong number of dice.");
}
this.dice = dice;
}
public abstract AbstractChuckALuck analyzeDice();
public AbstractChuckALuck printResults() {
for (int i = 1; i < inputs.length; i++) {
String bet = inputs[i];
if (results.indexOf(bet) >= 0) {
System.out.println("Number " + i + " wagered " + bet + " and wins!");
}
}
return this;
}
public AbstractChuckALuck processInputs(String[] inputs) {
this.inputs = inputs;
return this;
}
public AbstractChuckALuck rollDice() {
for (Die die : dice) die.roll();
return this;
}
public AbstractChuckALuck showDice() {
for (Die die : dice) System.out.println(die);
return this;
}
}
/** An instance of AbstractChuckALuck that uses typical rules.
*
* WAGER DESCRIPTION
* Big The total score will be 11 or higher and not a triple.
* Small The total score will be 10 or lower and not a triple.
*/
class ChuckALuck extends AbstractChuckALuck {
public ChuckALuck(Die[] 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();
// For "Single" wagers.
results.add(v1.toString());
results.add(v2.toString());
results.add(v3.toString());
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 String toString() {
return "Chuck-a-Luck";
}
}
/** An instance of AbstractChuckALuck that uses alternate rules.
*
* WAGER DESCRIPTION
* Big The total score will be 12 or higher and not a triple.
* Small The total score will be 9 or lower and not a triple.
*/
class AlternateChuckALuck extends AbstractChuckALuck {
public AlternateChuckALuck(Die[] dice) {
super(dice);
}
public AlternateChuckALuck analyzeDice() {
final Integer
v1 = dice[0].valueOf(),
v2 = dice[1].valueOf(),
v3 = dice[2].valueOf();
final int sum = v1 + v2 + v3;
results.clear();
// For "Single" wagers.
results.add(v1.toString());
results.add(v2.toString());
results.add(v3.toString());
if (v1 == v2 && v2 == v3) {
results.add(Triple);
}
else if (sum >= 12) { // ALTERNATE RULE
results.add(Big);
}
else if (sum <= 9 ) { // ALTERNATE RULE
results.add(Small);
}
if (sum < 8 || sum > 12) {
results.add(Field);
}
return this;
}
public String toString() {
return "Alternate Chuck-a-Luck";
}
}
/********/
/* DICE */
/********/
class AscendingDiceComparator implements Comparator<Die>
{
public final int compare(Die d1, Die d2) { return d1.valueOf().compareTo(d2.valueOf()); }
}
class DescendingDiceComparator implements Comparator<Die>
{
public final int compare(Die d1, Die d2) { return -1 * d1.valueOf().compareTo(d2.valueOf()); }
}
class AsciiDie3x7 {
public static final String[] sides = {
"---------\n" +
"| |\n" +
"| * |\n" +
"| |\n" +
"---------"
,
"---------\n" +
"| * |\n" +
"| |\n" +
"| * |\n" +
"---------"
,
"---------\n" +
"| * |\n" +
"| * |\n" +
"| * |\n" +
"---------"
,
"---------\n" +
"| * * |\n" +
"| |\n" +
"| * * |\n" +
"---------"
,
"---------\n" +
"| * * |\n" +
"| * |\n" +
"| * * |\n" +
"---------"
,
"---------\n" +
"| * * |\n" +
"| * * |\n" +
"| * * |\n" +
"---------"
,
"---------\n" +
"| * * |\n" +
"| * * * |\n" +
"| * * |\n" +
"---------"
,
"---------\n" +
"| * * * |\n" +
"| * * |\n" +
"| * * * |\n" +
"---------"
,
"---------\n" +
"| * * * |\n" +
"| * * * |\n" +
"| * * * |\n" +
"---------"
,
"---------\n" +
"| ***** |\n" +
"| |\n" +
"| ***** |\n" +
"---------"
,
"---------\n" +
"| ***** |\n" +
"| * |\n" +
"| ***** |\n" +
"---------"
,
"---------\n" +
"| ***** |\n" +
"| * * |\n" +
"| ***** |\n" +
"---------"
,
"---------\n" +
"| ***** |\n" +
"| * * * |\n" +
"| ***** |\n" +
"---------"
,
"---------\n" +
"|*******|\n" +
"| |\n" +
"|*******|\n" +
"---------"
,
"---------\n" +
"|*******|\n" +
"| * |\n" +
"|*******|\n" +
"---------"
,
"---------\n" +
"|*******|\n" +
"|* *|\n" +
"|*******|\n" +
"---------"
,
"---------\n" +
"|*******|\n" +
"|* * *|\n" +
"|*******|\n" +
"---------"
,
"---------\n" +
"|*** ***|\n" +
"|*** ***|\n" +
"|*** ***|\n" +
"---------"
,
"---------\n" +
"|*** ***|\n" +
"|*******|\n" +
"|*** ***|\n" +
"---------"
,
"---------\n" +
"|*******|\n" +
"|*** ***|\n" +
"|*******|\n" +
"---------"
,
"---------\n" +
"|*******|\n" +
"|*******|\n" +
"|*******|\n" +
"---------"
};
}
abstract class AbstractDie implements Die
{
public static Comparator<Die> ascendingComparator = new AscendingDiceComparator();
public static Comparator<Die> descendingComparator = new DescendingDiceComparator();
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 FairSixSidedDie extends AbstractFairDie
{
public int numberOfSides() { return 6; }
}
class FairSixSidedAsciiDie extends FairSixSidedDie
{
public String toString() {
return AsciiDie3x7.sides[valueOf() - 1];
}
}
class Dodecahedron extends AbstractFairDie
{
public int numberOfSides() { return 12; }
public String toString() {
return AsciiDie3x7.sides[valueOf() - 1];
}
}
class HighRoll implements CarnivalDiceGame
{
public int sum;
private int maxValue;
private int[] Dice;
private int highDice;
public HighRoll(int maxValue, int[] Dice, int highDice) {
this.maxValue = maxValue;
this.Dice = Dice;
this.highDice = highDice;
}
public HighRoll(Die[] highRollDice) {
}
public HighRoll analyzeDice() {
return this;
}
public HighRoll rollDice() {
int three = 3;
int dice1 = (int)(Math.random()* 6);
int dice2 = (int)(Math.random()* 6);
int dice3 = (int)(Math.random()* 6);
int[] dice = {dice1, dice2, dice3};
int max1 = findMax(dice);
int max2 = findMax(dice);
int max3 = findMax(dice);
sum = max1 + max2 + max3;
System.out.println("sum is " + sum);
System.out.print("eeeeeeeeeeeeee " + sum);
return this;
}
public int findMax(int[] dice) {
int max = dice[0];
for(int i = 0; i < dice.length; i++) {
if(dice[i] > max) {
max = dice[i];
}
}
return max;
}
public HighRoll showDice() {
return this;
}
public HighRoll printResults() {
return this;
}
public HighRoll processInputs(String[] inputs) {
String inputs1 = inputs[1];
int intInput1 = Integer.parseInt(inputs1);
String inputs2 = inputs[2];
int intInput2 = Integer.parseInt(inputs2);
System.out.println("input 1 is " + inputs1);
System.out.println("input 2 is " + inputs2);
System.out.println("sum is " + sum);
if(intInput1 <= 15) {
if(intInput2 >= (sum - 2) && intInput2 <= (sum + 2)) { //|| intInput2 <= (sum + 2) && intInput2 >= (sum)))
System.out.print("you won " + (intInput1 * 2) + "Dollars!!!");
}
else {
System.out.print("haha loser!!!");
}
}
else {
System.out.print("too much money!");
}
return this;
}
}