package Q4; import java.io.*; import java.util.*; public class RandomSumGame { private boolean start; private int d1 = 0; private int d2 = 0; private int sum = 0; private int valuePoint = 0; private int wins = 0; private String status; private PrintStream out; public RandomSumGame(PrintStream out) { this.out = out; } public void play() { this.rollDice(); this.play(this.d1, this.d2); } public void play(int d1, int d2) { int total = d1 + d2; this.puts("You rolled: %d", total); if (!hasValuePoint()) { this.firstPlay(total); } else { this.subsequentPlay(total); } } public void rollDice() { this.d1 = this.roll(); this.d2 = this.roll(); this.sum = this.d1 + this.d2; } private int roll() { return new Random().nextInt(5) + 1; } private void firstPlay(int total) { switch (total) { case 2: case 3: case 12: this.lose("Craps! You lose."); break; case 7: case 11: this.win("Natural! You win!"); break; default: this.puts("Value point established: %d", total); this.valuePoint = total; play(); break; } } private void subsequentPlay(int total) { if (total == this.valuePoint) this.win("You win!"); else if (total == 7) this.lose("You lose."); else play(); } private void win(String message) { this.wins += 1; this.puts(message); this.reset(); } private void lose(String message) { this.puts(message); this.reset(); } private void puts(String format, Object... args) { this.out.println(String.format(format, args)); } private boolean hasValuePoint() { return this.valuePoint > 0; } private void reset() { this.valuePoint = this.d1 = this.d2 = this.sum = 0; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Welcome to Craps"); RandomSumGame game = new RandomSumGame(System.out); game.play(); } }