-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPlayer.java
94 lines (73 loc) · 1.89 KB
/
SPlayer.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Server internal representation of a player
class SPlayer {
protected boolean done = false;
protected int sum = 0; /* <= 21 */
private IPlayer player;
SPlayer(IPlayer player) { this.player = player; }
// ------------------------------------------------------------------
// allow player to take a turn
public boolean turn(Turn t) {
if (!done) {
player.turn(t);
}
return done;
}
// add in result to sum and make sure it's not over 21
// pre: !done
public void record(int /* 1 .. 6 */ result) {
if (done)
sum = 0;
else
sum += result;
if (21 == sum)
done = true;
else if (21 < sum) {
done = true;
sum = 0;
};
}
// record the player's attempt to cheat
public void cheat() {
done = true;
sum = 0;
player.inform("You cheated. You're out.");
}
public String name() {
return player.name();
}
public void inform(String s) {
player.inform(s);
}
public IPlayer getPlayer() {
return player;
}
// ------------------------------------------------------------------
// Examples:
static MPlayer m1;
static SPlayer s1;
static public void createExamples() {
if (m1 == null) {
m1 = new MPlayer("machine play for test of splayer");
s1 = new SPlayer(m1);
}
}
// ------------------------------------------------------------------
// Test
public static void main(String argv[]) {
createExamples();
m1.registerDisplay(TestIDisplay.testIDisplay);
s1.record(6);
Tester.check(s1.sum == 6,"record 6");
s1.record(3);
Tester.check(s1.sum == 9,"record 9");
s1.record(6);
s1.record(6);
Tester.check(s1.done,"done 21");
// calling record in improper context
s1.record(3);
Tester.check(s1.sum == 0,"done 21");
s1.cheat();
Tester.check(s1.done,"cheat 1");
Tester.check(s1.sum == 0,"cheat 2");
}
}