blob: aa0aa007df4317d7e600db6e3764f4cc30e8b012 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
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 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()) {
switch (total) {
case 2:
case 3:
case 12:
this.puts("Craps! You lose.");
break;
case 7:
case 11:
this.puts("Natural! You win!");
break;
default:
this.puts("Value point established: %d", total);
this.valuePoint = total;
play();
break;
}
} else {
if (total == this.valuePoint) {
this.puts("You win!");
this.reset();
return;
} else if (total == 7) {
this.puts("You lose.");
this.reset();
return;
} else {
play();
}
}
}
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 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();
}
}
|