//An instance of this class represent a single-player // game of Craps played with a pair of 6-sided dice. // //Come-Out Roll // 2 or 3 or 12: Player wins // 7 or 11 : Player loses - Craps Out! // other : Establishes Player's point //Match-Point Roll // point : Player wins // 7 : Player loses - Sevens Out! // other : Player takes another Match-Point roll // public class CrapsGame { //Instance Variables private final SixSidedDie dieOne; //1st of 2 6-sided dice private final SixSidedDie dieTwo; //2nd of 2 6-sided dice private String record; //Trace of the dice rolls private boolean winner; //True only when player wins private int point; //Score on the Come-Out roll //Constructor public CrapsGame() { this.dieOne = new SixSidedDie(); this.dieTwo = new SixSidedDie(); this.record = "CRAPS"; this.winner = false; this.play(); } //Accessor: Returns a record of this CrapsGame public String getRecord() { return this.record; } //Accessor: returns whether the player has won this CrapsGame public boolean playerWins() { return this.winner; } //Mutator: Plays this CrapsGame to completion public void play() { this.comeOutRoll(); switch ( this.point ) { case 2 : case 3 : case 12 : this.winner = true; break; case 7 : case 11 : this.winner = false; break; default : this.matchPointRoll(); } this.record += " " + (this.winner ? "WINNER" : "LOSER"); } //Helper: The players initial throw of the dice private void comeOutRoll() { this.point = this.shootDice(); this.record += " Point: " + field2(this.point); } //Helper: Player throws repeatedly until one of two events occurs // WIN - throw matches the point, or LOSS - throws 7 private void matchPointRoll() { this.record += " Matching:"; int score; do { score = this.shootDice(); this.record += " " + field2(score); } while ( score != 7 && score != this.point ); this.winner = (score == point); } //Helper: Tossing the pair of dice private int shootDice() { this.dieOne.toss(); this.dieTwo.toss(); return this.dieOne.getTopFace() + this.dieTwo.getTopFace(); } private String field2(int number) { return (number < 10 ? " " : "") + number; } }