-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHView.java
197 lines (163 loc) · 4.43 KB
/
HView.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// the Views: determine ordering of button and label with boolean
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class HView extends HLayout implements IHDisplay {
public HView(String name) {
super(name);
// adapt IListener to ActionListener
doneb.addActionListener
(new ActionListener() {
// pre: (null != l)
// post: (null == l)
public void actionPerformed(ActionEvent e) {
l.done();
l = null;
}
});
rollb.addActionListener
(new ActionListener() {
// pre: (null != l)
// post: (null == l)
public void actionPerformed(ActionEvent e) {
sump.setText(sums(l.roll()));
l = null;
}
});
skipb.addActionListener
(new ActionListener() {
// pre: (null != l)
// post: (null == l)
public void actionPerformed(ActionEvent e) {
l.skip();
l = null;
}
});
roll2xb.addActionListener
(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sump.setText(sums(l.rollTwice()));
l = null;
}
});
}
// ------------------------------------------------------------------
// TODO: allocate the right number of booleans programmatically
private boolean[] actionEnabled = { true, true, true, true };
private IListener l;
public void listen(IListener ll) {
int x=0;
l = ll;
for (Turn.Action action : Turn.Action.values()) {
if (actionEnabled[action.ordinal()]) {
setActionButtonEnabled(action, true);
}
}
// wait until one of the buttons has been clicked
while (l != null) {
System.out.print("");
}
setEnabledAll(false);
}
public void sumDisplay(int s) {
sump.setText(sums(s));
}
public void msgDisplay(String s) {
msgp.setText(s);
}
public void setActionEnabled(Turn.Action action, boolean enabled) {
String actionString;
if (action == Turn.Action.SKIP) actionString = "skip";
else if (action == Turn.Action.HOLD) actionString = "hold";
else if (action == Turn.Action.ROLLTWICE) actionString = "roll twice";
else actionString = "roll";
System.out.println("Setting action " + actionString + " to be " + (enabled ? "enabled" : "disabled"));
actionEnabled[action.ordinal()] = false;
}
private void setActionButtonEnabled(Turn.Action action, boolean enabled) {
if (action == Turn.Action.SKIP) {
skipb.setEnabled(enabled);
} else if (action == Turn.Action.HOLD) {
doneb.setEnabled(enabled);
} else if (action == Turn.Action.ROLL) {
rollb.setEnabled(enabled);
} else if (action == Turn.Action.ROLLTWICE) {
roll2xb.setEnabled(enabled);
}
}
// ------------------------------------------------------------------
// Examples
static HView v;
// the simulated Human player
static boolean endtest;
static int x;
static int y;
static Thread human;
// for testing:
// Java doesn't understand real closures:
static int skip;
static int done;
static int roll;
static int roll2x;
static public void createExamples() {
v = new HView("test hview");
endtest = false;
x = 0;
y = 0;
skip = 0;
done = 0;
roll = 0;
roll2x = 0;
human = new Thread () {
public void run() {
Robot r;
try {
r = new Robot();
r.setAutoDelay(500);
while (!endtest) {
press(r,x,y);
}
v.quit(r);
} catch (AWTException e) {
System.out.println("awt exception thrown");
};
}
};
}
// ------------------------------------------------------------------
// TEST
public static void main(String argv[]) {
IListener ll =
new IListener() {
public void skip() {
skip += 1;
}
public void done() {
done += 1;
}
public int roll() {
roll += 1;
return roll;
}
public int rollTwice() {
roll2x += 1;
return roll2x;
}
};
human.start();
test_button(ROLLX, ROLLY, ll);
Tester.check(roll == 1, "roll button check");
test_button(ROLL2XX, ROLL2XY, ll);
Tester.check(roll2x == 1, "roll2x button check");
test_button(SKIPX, SKIPY, ll);
Tester.check(skip == 1, "skip button check");
test_button(DONEX, DONEY, ll);
Tester.check(done == 1, "done button check");
endtest = true;
}
private static void test_button(int i, int j, IListener ll) {
x = i;
y = j;
v.listen(ll);
}
}