-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTurn.java
93 lines (73 loc) · 1.92 KB
/
Turn.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
// the turn that is handed to player so that it can play
class Turn implements ITurn {
SPlayer player;
Die d;
Action actionTaken;
Turn(SPlayer player, Die d) {
this.player = player;
this.d = d;
}
public static enum Action {
ROLL,
SKIP,
HOLD,
ROLLTWICE
}
// ------------------------------------------------------------------
private boolean turnTaken = false;
// check whether the player already performed an action
// effect: set done
private void okayP() {
if (turnTaken)
player.cheat();
turnTaken = true;
}
public int roll() {
okayP();
actionTaken = Action.ROLL;
int result = d.throw_die();
player.record(result);
return result;
}
public void skip() {
okayP();
actionTaken = Action.SKIP;
}
public void hold() {
okayP();
actionTaken = Action.HOLD;
player.done = true;
}
public int rollTwice() {
okayP();
actionTaken = Action.ROLLTWICE;
int result = d.throw_die() + d.throw_die();
player.record(result);
return result;
}
// ------------------------------------------------------------------
// Examples:
// ------------------------------------------------------------------
// 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[]) {
SPlayer s1 = t1.player;
m1.registerDisplay(TestIDisplay.testIDisplay);
int r1 = t1.roll();
Tester.check(1 <= r1 && r1 <= 6,"roll 1");
Tester.check(r1 == s1.sum,"roll 2");
}
}