-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPlayer.java
72 lines (53 loc) · 1.43 KB
/
MPlayer.java
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
class MPlayer extends Player {
MPlayer(String name) { super(name); }
// ------------------------------------------------------------------
private IDisplay io;
public void registerDisplay(IDisplay d) {
this.io = d;
}
public void inform(String msg) {
io.msgDisplay(msg);
}
public void turn(Turn t) {
io.msgDisplay("It's your turn.");
if (sum >= 19) {
t.hold();
} else if (sum < 19) {
sum += t.roll();
}
io.sumDisplay(sum);
if (sum <= 21) {
io.msgDisplay("Turn's up");
} else {
io.msgDisplay("Oops, you risked all and lost. Bust!");
}
}
// ------------------------------------------------------------------
// Examples:
static MPlayer m1;
static SPlayer s1;
static Turn t1;
static Turn t2;
public static void createExamples() {
if (m1 == null) {
m1 = new MPlayer("test1");
s1 = new SPlayer(m1);
t1 = new Turn(s1, new Die());
t2 = new Turn(s1, new Die());
}
}
// ------------------------------------------------------------------
// Tests:
public static void main(String argv[]) {
m1.registerDisplay(TestIDisplay.testIDisplay);
m1.inform("welcome to player 1");
m1.turn(t1);
Tester.check(!s1.done, "turn 1");
m1.sum = 20;
m1.turn(t2);
Tester.check(s1.done, "turn 2");
m1.turn(t2);
Tester.check(s1.done, "cheat 3");
Tester.check(s1.sum == 0, "cheat 4");
}
}