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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
package jrummikub.control;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import jrummikub.model.GameSettings;
import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type;
import jrummikub.model.StoneColor;
import jrummikub.util.Connection;
import jrummikub.util.IListener;
import jrummikub.util.IListener1;
import jrummikub.util.IListener2;
import jrummikub.view.ISettingsPanel;
import jrummikub.view.IView;
/**
* Abstract control class managing joined methods and attributes of all
* different (network, default...) settings controls
*/
public abstract class AbstractSettingsControl {
protected IView view;
protected List<Connection> connections = new ArrayList<Connection>();
protected GameSettings settings;
/**
* Create a new settings control
*
* @param view
* the view to use
* @param settings
* initial game settings
*/
protected AbstractSettingsControl(IView view, GameSettings settings) {
this.view = view;
this.settings = settings;
view.getSettingsPanel().setPlayerNamesEditable(
Collections.<Boolean> emptyList());
view.getSettingsPanel()
.setPlayerColors(
new HashSet<Color>(Arrays
.asList(ISettingsPanel.PLAYER_COLORS)));
}
/**
* Add event listeners to abstract settings control
*/
protected void addListeners() {
addPlayerSettingsListeners();
addOptionListeners1();
addOptionListeners2();
addVariantListeners();
}
/**
* Add event listeners to the player settings panel
*/
private void addPlayerSettingsListeners() {
connections.add(view.getSettingsPanel().getAddPlayerEvent()
.add(new IListener() {
@Override
public void handle() {
addPlayer();
}
}));
connections.add(view.getSettingsPanel().getRemovePlayerEvent()
.add(new IListener1<Integer>() {
@Override
public void handle(Integer i) {
removePlayer(i);
}
}));
connections.add(view.getSettingsPanel().getChangePlayerColorEvent()
.add(new IListener2<Integer, Color>() {
@Override
public void handle(Integer i, Color color) {
setPlayerColor(i, color);
}
}));
connections.add(view.getSettingsPanel().getChangePlayerNameEvent()
.add(new IListener2<Integer, String>() {
@Override
public void handle(Integer i, String name) {
setPlayerName(i, name);
}
}));
connections.add(view.getSettingsPanel().getChangePlayerTypeEvent()
.add(new IListener2<Integer, Type>() {
@Override
public void handle(Integer i, Type type) {
setPlayerType(i, type);
}
}));
}
/**
* Add the first half of event listeners to option panel
*/
private void addOptionListeners1() {
connections.add(view.getSettingsPanel()
.getChangeInitialMeldThresholdEvent()
.add(new IListener1<Integer>() {
@Override
public void handle(Integer value) {
settings.setInitialMeldThreshold(value);
update();
}
}));
connections.add(view.getSettingsPanel().getChangeJokerNumberEvent()
.add(new IListener1<Integer>() {
@Override
public void handle(Integer value) {
settings.setJokerNumber(value);
update();
}
}));
connections.add(view.getSettingsPanel().getChangeStoneSetNumberEvent()
.add(new IListener1<Integer>() {
@Override
public void handle(Integer value) {
settings.setStoneSetNumber(value);
update();
}
}));
connections.add(view.getSettingsPanel()
.getChangeNumberOfStonesDealtEvent()
.add(new IListener1<Integer>() {
@Override
public void handle(Integer value) {
settings.setNumberOfStonesDealt(value);
update();
}
}));
}
/**
* Add the second half of event listeners to option panel
*/
private void addOptionListeners2() {
connections.add(view.getSettingsPanel().getChangeHighestValueEvent()
.add(new IListener1<Integer>() {
@Override
public void handle(Integer value) {
settings.setHighestValue(value);
update();
}
}));
connections.add(view.getSettingsPanel().getChangeTimeEvent()
.add(new IListener1<Integer>() {
@Override
public void handle(Integer value) {
settings.setTotalTime(value);
update();
}
}));
connections.add(view.getSettingsPanel().getChangeStoneColorsEvent()
.add(new IListener1<Set<StoneColor>>() {
@Override
public void handle(Set<StoneColor> value) {
settings.setStoneColors(new HashSet<StoneColor>(value));
update();
}
}));
connections.add(view.getSettingsPanel().getChangeNoLimitsEvent()
.add(new IListener1<Boolean>() {
@Override
public void handle(Boolean value) {
settings.setNoLimits(value);
update();
}
}));
connections.add(view.getSettingsPanel().getChangeSeeHandSizeEvent()
.add(new IListener1<Boolean>() {
@Override
public void handle(Boolean value) {
settings.setSeeHandSize(value);
update();
}
}));
}
/**
* Add event listeners for variants, e.g. standard or children
*/
private void addVariantListeners() {
connections.add(view.getSettingsPanel().getSetVariantDefaultEvent()
.add(new IListener() {
@Override
public void handle() {
settings.reset();
update();
}
}));
connections.add(view.getSettingsPanel().getSetVariantChildrenEvent()
.add(new IListener() {
@Override
public void handle() {
settings.reset();
settings.setHighestValue(10);
settings.setJokerNumber(6);
settings.setTotalTime(120);
settings.setInitialMeldThreshold(20);
update();
}
}));
}
/**
* Start the operation of the settings control
*/
public void startSettings() {
view.showSettingsPanel(true);
}
/**
* Finds the next unused player color
*
* @return an unused color
*/
protected Color findUnusedColor() {
Color color = null;
colorLoop: for (Color c : ISettingsPanel.PLAYER_COLORS) {
color = c;
for (PlayerSettings player : settings.getPlayerList()) {
if (c == player.getColor()) {
continue colorLoop;
}
}
break;
}
return color;
}
/**
* Sets a player's color
*
* @param i
* index of player
* @param color
* color to be set
*/
private void setPlayerColor(int i, Color color) {
PlayerSettings player = settings.getPlayerList().get(i);
if (player.getColor() == color) {
return;
}
for (PlayerSettings other : settings.getPlayerList()) {
if (other.getColor() == color) {
other.setColor(player.getColor());
break;
}
}
player.setColor(color);
update();
}
/**
* Sets a player's name
*
* @param i
* index of player
* @param name
* name to be set
*/
private void setPlayerName(int i, String name) {
settings.getPlayerList().get(i).setName(name);
update();
}
/**
* Removes a player from the planned game
*
* @param i
* index of player
*/
private void removePlayer(int i) {
settings.getPlayerList().remove(i);
update();
}
/**
* Checks the settings for ambiguous or missing data and sets error/warning
* if necessary
*
* @return boolean check correct
*/
protected boolean checkSettings() {
if (!checkName()) {
return false;
}
if (!checkErrors()) {
return false;
}
view.getSettingsPanel().setError(ISettingsPanel.SettingsError.NO_ERROR);
view.getSettingsPanel().enableStartGameButton(true);
checkWarnings();
return true;
}
/**
* Checks if unique player name is present for each player
*
* @return is player name ok
*/
private boolean checkName() {
for (PlayerSettings player : settings.getPlayerList()) {
if (player.getName().isEmpty()) {
view.getSettingsPanel().setError(
ISettingsPanel.SettingsError.NO_PLAYER_NAME_ERROR);
view.getSettingsPanel().enableStartGameButton(false);
return false;
}
}
for (int i = 0; i < settings.getPlayerList().size(); ++i) {
PlayerSettings player1 = settings.getPlayerList().get(i);
String name = player1.getName();
Type type = player1.getType();
if (type == Type.VACANT) {
continue;
}
for (int j = i + 1; j < settings.getPlayerList().size(); ++j) {
PlayerSettings player2 = settings.getPlayerList().get(j);
if (player2.getName().equals(name) && player2.getType() == type) {
view.getSettingsPanel()
.setError(
ISettingsPanel.SettingsError.DUPLICATE_PLAYER_NAME_ERROR);
view.getSettingsPanel().enableStartGameButton(false);
return false;
}
}
}
return true;
}
/**
* Check options leading to errors
*
* @return options ok
*/
private boolean checkErrors() {
int totalStonesDealt = settings.getNumberOfStonesDealt()
* settings.getPlayerList().size();
int totalStones = settings.getTotalStones();
if (totalStones <= totalStonesDealt) {
view.getSettingsPanel().setError(
ISettingsPanel.SettingsError.NOT_ENOUGH_STONES_ERROR);
view.getSettingsPanel().enableStartGameButton(false);
return false;
}
if (settings.getStoneColors().size() < 3) {
view.getSettingsPanel().setError(
ISettingsPanel.SettingsError.NOT_ENOUGH_COLORS_ERROR);
view.getSettingsPanel().enableStartGameButton(false);
return false;
}
return true;
}
/**
* Check all options and player settings for entries causing warnings
*/
private void checkWarnings() {
if (settings.getInitialMeldThreshold() >= 100) {
view.getSettingsPanel().setError(
ISettingsPanel.SettingsError.TOO_HIGH_THRESHOLD_WARNING);
return;
}
boolean humanPlayerFound = false;
for (PlayerSettings player : settings.getPlayerList()) {
if (player.getType() == Type.HUMAN) {
humanPlayerFound = true;
break;
}
}
if (!humanPlayerFound) {
view.getSettingsPanel().setError(
ISettingsPanel.SettingsError.COMPUTER_PLAYERS_ONLY_WARNING);
return;
}
}
/**
* Abort settings control once settings are set
*/
public void abort() {
view.showSettingsPanel(false);
for (Connection c : connections) {
c.remove();
}
}
/**
* Add new player to the planned game
*/
abstract protected void addPlayer();
/**
* Set the type of a player
*
* @param i
* index of player
* @param type
* type to be set
*/
abstract protected void setPlayerType(int i, Type type);
/**
* Show newly chosen options in view
*/
abstract protected void update();
}
|