-
Notifications
You must be signed in to change notification settings - Fork 5
/
GBGLaunch.java
506 lines (459 loc) · 16.3 KB
/
GBGLaunch.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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package starters;
import games.Arena;
import gui.SolidBorder;
import tools.Types;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* This class is a general launcher for GBG. The user may select via launcher UI or predefine
* via command line arguments (see {@link #main(String[])}) which GBG game will be started.
* <p>
* For scalable games, once a game is chosen, the launcher will allow to select the scalable
* parameters (i.e. number of players, number of nodes for Sim). For non-scalable games (i.e.
* TicTacToe), the scalable parameter select boxes will be disabled.
* <p>
* When 'Start Game' is clicked, the launcher UI becomes invisible and GBG's Arena shows up.<br>
* When GBG's Arena finishes, the launcher UI will show up again and allow to choose another game.
*
* @author Wolfgang Konen, TH Koeln, 2020
*
* @see GBGBatch
* @see SetupGBG
*/
public class GBGLaunch extends SetupGBG {
/**
* The possible games:
*/
String[] game_list = {"2048","Blackjack","ConnectFour","EWN","Hex","KuhnPoker","Nim","Nim3P",
"Othello","Poker","RubiksCube","Sim","TicTacToe", "Yavalath"};
public enum LaunchTask {
STARTSELECTOR, SELECTGAME, STARTGAME, EXITSELECTOR, IDLE
}
public LaunchTask launcherState = LaunchTask.SELECTGAME; // also used in Arena.destroy()
//private static final long serialVersionUID = 1L;
public static Arena t_Game;
private final JFrame launcherUI;
private static String selectedGame = "TicTacToe";
private final JLabel scaPar0_L;
private final JLabel scaPar1_L;
private final JLabel scaPar2_L;
private final JComboBox<String> choiceScaPar0;
private final JComboBox<String> choiceScaPar1;
private final JComboBox<String> choiceScaPar2;
/**
* Starts the general launcher for GBG. The user may select via launcher UI or predefine
* via command line arguments which GBG game will be started. When GBG's Arena finishes, the
* launcher UI will show up again and allow to select another game.
*
* @param args <br>
* [0] 0: direct start w/o launcher UI, 1: with launcher UI (default is 1)<br>
* [1] the name of the game, one out of {@link #game_list} (default is "TicTacToe")
* [2] either "T" (with train rights) or "P" (just play) (default is "T")
* <p>
* Examples:
* <ul>
* <li> {@code GBGLaunch} : start launcher, with TicTacToe preselected
* <li> {@code GBGLaunch 1 Nim} : start launcher, with Nim preselected
* <li> {@code GBGLaunch 0 Hex P} : start directly Hex, no launcher, no train rights
* <li> {@code GBGLaunch 0} : start directly TicTacToe, no launcher
* </ul>
* When a scalable game is started w/o launcher, the method {@link SetupGBG#setDefaultScaPars(String)} will set
* appropriate default scalable parameters for the game in question.
*/
public static void main(String[] args) {
GBGLaunch t_Launch;
boolean withLauncherUI = true;
boolean withTrainRights = true;
if (args.length>0) {
withLauncherUI = (Integer.parseInt(args[0])==1);
}
if (args.length>1) {
selectedGame = args[1];
}
if (args.length>2) {
withTrainRights = (args[2].equals("T"));
}
if (withLauncherUI) {
t_Launch = new GBGLaunch();
t_Launch.adjustScaParGuiPart();
while(true) {
switch(t_Launch.launcherState) {
case STARTSELECTOR:
t_Launch.launcherUI.setVisible(true);
t_Launch.launcherState = LaunchTask.SELECTGAME;
break;
case STARTGAME:
t_Launch.launcherUI.setVisible(false);
// start selectedGame with launcherUI-loop
startGBGame(selectedGame,t_Launch,withTrainRights);
t_Launch.launcherState = LaunchTask.IDLE;
break;
case EXITSELECTOR:
t_Launch.launcherUI.setVisible(false);
t_Launch.launcherUI.dispose();
System.exit(0);
break;
case IDLE:
case SELECTGAME:
default:
// this state is left when button StartG ('Start Game') in launcher is hit
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
}
} else {
// start selectedGame without launcherUI-loop
startGBGame(selectedGame,null,withTrainRights);
}
}
/**
* Start a game with or without train rights
* @param selectedGame the game name
* @param t_Launch the launcher
* @param withTrainRights whether Arena is trainable or not
*
* @see SetupGBG
*/
private static void startGBGame(String selectedGame, GBGLaunch t_Launch, boolean withTrainRights) {
String x, title = "General Board Game Playing";
String[] scaPar = new String[3];
for (int i=0; i<3; i++) scaPar[i]="";
if (t_Launch==null) {
scaPar = setDefaultScaPars(selectedGame);
} else {
// replace scaPar[i] only, if the selected item is not null (to avoid NullPointerException when later using scaPar[i])
x = (String) t_Launch.choiceScaPar0.getSelectedItem(); if (x!=null) scaPar[0]=x;
x = (String) t_Launch.choiceScaPar1.getSelectedItem(); if (x!=null) scaPar[1]=x;
x = (String) t_Launch.choiceScaPar2.getSelectedItem(); if (x!=null) scaPar[2]=x;
}
// SetupGBG.setupSelectedGame has the switch statement over all games:
t_Game = setupSelectedGame(selectedGame, scaPar, title,true,withTrainRights);
t_Game.setLauncherObj(t_Launch);
t_Game.init();
}
/**
* creates the launcher UI
*/
protected GBGLaunch() {
// the colors of the TH Koeln logo (used for button coloring):
Color colTHK1 = new Color(183,29,13);
Color colTHK2 = new Color(255,137,0);
Color colTHK3 = new Color(162,0,162);
SolidBorder bord = new SolidBorder();
JPanel titlePanel = new JPanel();
titlePanel.setBackground(Types.GUI_BGCOLOR);
JLabel Blank = new JLabel(" "); // a little bit of space
JLabel m_title = new JLabel("GBG Launcher", SwingConstants.CENTER);
m_title.setForeground(Color.black);
Font tFont = new Font("Arial", Font.BOLD, Types.GUI_TITLEFONTSIZE);
m_title.setFont(tFont);
titlePanel.add(Blank);
titlePanel.add(m_title);
JComboBox<String> choiceGame = new JComboBox<>(game_list);
choiceGame.setSelectedItem(selectedGame);
scaPar0_L = new JLabel("");
choiceScaPar0 = new JComboBox<>();
scaPar1_L = new JLabel("");
choiceScaPar1 = new JComboBox<>();
scaPar2_L = new JLabel("");
choiceScaPar2 = new JComboBox<>();
JPanel scaPar0Panel = new JPanel();
scaPar0Panel.setLayout(new GridLayout(1,0,2,2)); // rows,columns,hgap,vgap
scaPar0Panel.add(scaPar0_L);
scaPar0Panel.add(choiceScaPar0);
JPanel scaPar1Panel = new JPanel();
scaPar1Panel.setLayout(new GridLayout(1,0,2,2)); // rows,columns,hgap,vgap
scaPar1Panel.add(scaPar1_L);
scaPar1Panel.add(choiceScaPar1);
JPanel scaPar2Panel = new JPanel();
scaPar2Panel.setLayout(new GridLayout(1,0,2,2)); // rows,columns,hgap,vgap
scaPar2Panel.add(scaPar2_L);
scaPar2Panel.add(choiceScaPar2);
JButton StartG=new JButton("Start Game");
StartG.setBorder(bord);
StartG.setEnabled(true);
StartG.setForeground(Color.white);
StartG.setBackground(colTHK2);
JButton Exit=new JButton("Exit");
Exit.setBorder(bord);
Exit.setEnabled(true);
Exit.setForeground(Color.white);
Exit.setBackground(colTHK2);
launcherUI = new JFrame("");
launcherUI.setLayout(new GridLayout(0,1,3,3)); // rows,columns,hgap,vgap
launcherUI.add(titlePanel);
launcherUI.add(choiceGame);
launcherUI.add(scaPar0Panel);
launcherUI.add(scaPar1Panel);
launcherUI.add(scaPar2Panel);
launcherUI.add(StartG);
launcherUI.add(Exit);
launcherUI.addWindowListener(new WindowClosingAdapter());
launcherUI.setSize(300,300);
launcherUI.setBounds(400,300,300,300); // x,y,width,height
//launcherUI.pack();
launcherUI.setVisible(true);
StartG.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
selectedGame = (String)choiceGame.getSelectedItem();
launcherState = LaunchTask.STARTGAME;
}
}
);
Exit.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
launcherState = LaunchTask.EXITSELECTOR;
}
}
);
choiceGame.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
selectedGame = (String)choiceGame.getSelectedItem();
adjustScaParGuiPart();
}
}
);
choiceScaPar0.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
selectedGame = (String)choiceGame.getSelectedItem();
if (selectedGame!=null) {
if (selectedGame.equals("Nim")) {
String heapSize = (String)choiceScaPar1.getSelectedItem();
try {
int iHeapSize = Integer.parseInt(heapSize);
if (iHeapSize == -1) { // the triangle-shaped sizes for the Ludii case
choiceScaPar2.setSelectedItem(choiceScaPar0.getSelectedItem());
}
} catch(NumberFormatException ne) {
// do nothing, if heapSize is not number
}
}
}
}
}
);
choiceScaPar1.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
selectedGame = (String)choiceGame.getSelectedItem();
if (selectedGame!=null) {
if (selectedGame.equals("Nim")) {
String heapSize = (String)choiceScaPar1.getSelectedItem();
//assert heapSize!=null : "heapSize is null!";
// /WK/ commented assertion out, it would fire right after the selection of game "Nim",
// where choiceScaPar1 is still empty
int iHeapSize = Integer.parseInt(heapSize);
choiceScaPar2.removeAllItems();
if (iHeapSize == -1) { // the triangle-shaped sizes for the Ludii case
setScaPar0List(new int[]{3,5,7,9,11});
setScaPar2List(new int[]{3,5,7,9,11});
choiceScaPar0.setSelectedItem("5"); // the initial (recommended) values
choiceScaPar2.setSelectedItem("5"); // the initial (recommended) values
} else if (iHeapSize==3) {
setScaPar0List(new int[]{2,3,4,5});
choiceScaPar0.setSelectedItem("3"); // the initial (recommended) values
setScaPar2List(new int[]{2,3});
} else {
setScaPar0List(new int[]{2,3,4,5});
choiceScaPar0.setSelectedItem("3"); // the initial (recommended) values
setScaPar2List(new int[]{2,3,4,5});
if (iHeapSize>5) choiceScaPar2.addItem(heapSize);
}
}
}
}
}
);
}
/**
* helper class for {@link GBGLaunch#GBGLaunch()}
*/
protected static class WindowClosingAdapter
extends WindowAdapter
{
public WindowClosingAdapter() { }
public void windowClosing(WindowEvent event)
{
event.getWindow().setVisible(false);
event.getWindow().dispose();
System.exit(0);
}
}
/**
* Adjust the scalable parameters of a game after the game selector has changed.
*/
public void adjustScaParGuiPart() {
switch(selectedGame) {
case "Hex":
scaPar0_L.setText("Board Size");
scaPar1_L.setText("");
scaPar2_L.setText("");
setScaPar0List(new int[]{2,3,4,5,6,7,8});
setScaPar1List(new int[]{});
setScaPar2List(new int[]{});
choiceScaPar0.setSelectedItem("6"); // the initial (recommended) value
break;
case "Nim":
case "Nim3P":
scaPar0_L.setText("Heaps");
scaPar1_L.setText("Heap Size");
setScaPar0List(new int[]{2,3,4,5}); // int values are converted to
setScaPar1List(new int[]{-1,3,5,6,7,8,9,10,20,50});// Strings in ChoiceBoxes
choiceScaPar0.setSelectedItem("3"); //
choiceScaPar1.setSelectedItem("5"); // the initial (recommended) values
if (selectedGame.equals("Nim")) {
scaPar2_L.setText("Max Minus");
setScaPar0List(new int[]{2,3,4,5});
choiceScaPar0.setSelectedItem("3");
setScaPar2List(new int[]{2,3,4,5});
choiceScaPar2.setSelectedItem("5");
} else { // i.e. "Nim3P"
scaPar2_L.setText("Extra Rule");
setScaPar2List(new String[]{"true","false"});
choiceScaPar2.setSelectedItem("true");
}
break;
case "Sim":
scaPar0_L.setText("Players");
scaPar1_L.setText("Nodes");
scaPar2_L.setText("Coalition");
scaPar0_L.setToolTipText("Number of players");
scaPar1_L.setToolTipText("Number of nodes in Sim graph");
scaPar2_L.setToolTipText("'1-2': coalition of player 1+2 against player 0 (only 3-player)");
setScaPar0List(new int[]{2,3});
setScaPar1List(new int[]{4,5,6,7,8,9,10,12,15,20});
setScaPar2List(new String[]{"None","1-2"});
choiceScaPar0.setSelectedItem("2"); //
choiceScaPar1.setSelectedItem("6"); // the initial (recommended) values
choiceScaPar2.setSelectedItem("None"); //
break;
case "RubiksCube":
scaPar0_L.setText("Cube Type");
scaPar1_L.setText("Board Vec");
scaPar2_L.setText("Twist Type");
scaPar0_L.setToolTipText("Pocket Cube or Rubik's Cube");
scaPar1_L.setToolTipText("Type of board vectors (only n-tuple agents)");
scaPar2_L.setToolTipText("Type of twists (all or only quarter)");
//setScaPar0List(new int[]{});
//setScaPar1List(new int[]{});
//setScaPar2List(new int[]{});
setScaPar0List(new String[]{"2x2x2","3x3x3"});
setScaPar1List(new String[]{"CSTATE","CPLUS","STICKER","STICKER2"});
setScaPar2List(new String[]{"HTM","QTM"});
//choiceScaPar0.addItem("3x3x3");
choiceScaPar0.setSelectedItem("3x3x3"); //
choiceScaPar1.setSelectedItem("STICKER2"); // the initial (recommended) values
choiceScaPar2.setSelectedItem("QTM"); //
break;
case "EWN":
scaPar0_L.setText("Settings:");
setScaPar0List(new String[]{"3x3 2-Player", "4x4 2-Player","5x5 2-Player","6x6 3-Player", "4x4 4-Player","6x6 4-Player"});
choiceScaPar0.setSelectedItem("3x3 2-Player");
scaPar1_L.setText("Position values:");
setScaPar1List(new String[]{"[0,..,n]", "[0,1],[2,3],[4,5]","[0],[1],[2],[3],[4],[5]"});
choiceScaPar1.setSelectedItem("[0,..,n]");
scaPar2_L.setText("Random starting:");
setScaPar2List(new String[]{"True", "False"});
choiceScaPar2.setSelectedItem("False");
break;
case "2048":
case "Blackjack":
case "ConnectFour":
case "Othello":
case "Poker":
case "KuhnPoker":
case "TicTacToe":
//
// games with no scalable parameters
//
scaPar0_L.setText("");
scaPar1_L.setText("");
scaPar2_L.setText("");
setScaPar0List(new int[]{});
setScaPar1List(new int[]{});
setScaPar2List(new int[]{});
break;
case "Yavalath":
scaPar0_L.setText("Players");
scaPar1_L.setText("Board Size");
setScaPar0List(new String[]{ "2","3"});
setScaPar1List(new String[]{ "3","4","5","6"});
choiceScaPar0.setSelectedItem("2");
choiceScaPar1.setSelectedItem("5");
scaPar0_L.setToolTipText("Number of players");
scaPar1_L.setToolTipText("Size of the board game (Meaning length of its edge)");
break;
default:
System.err.println("[GBGLaunch] "+selectedGame+": This game is unknown.");
System.exit(1);
}
}
public void setScaPar0List(int[] modeList) {
choiceScaPar0.removeAllItems();
for (int i : modeList)
choiceScaPar0.addItem(Integer.toString(i));
}
public void setScaPar1List(int[] modeList) {
try {
choiceScaPar1.removeAllItems();
} catch (NumberFormatException e) {
// this avoids a strange bug: When selecting game Sim, and then game Nim, a NumberFormatException fires
// when removeAllItems is executed, unclear why. Strange enough, this happens ONLY for game Nim not for
// the nearly identically Nim3P or any other game. We cure this bug, which is internal to JComboBox, by
// catching the NumberFormatException silently.
}
for (int i : modeList)
choiceScaPar1.addItem(Integer.toString(i));
}
public void setScaPar2List(int[] modeList) {
choiceScaPar2.removeAllItems();
for (int i : modeList)
choiceScaPar2.addItem(Integer.toString(i));
}
public void setScaPar0List(String[] modeList) {
choiceScaPar0.removeAllItems();
for (String s : modeList)
choiceScaPar0.addItem(s);
}
public void setScaPar1List(String[] modeList) {
choiceScaPar1.removeAllItems();
for (String s : modeList)
choiceScaPar1.addItem(s);
}
public void setScaPar2List(String[] modeList) {
choiceScaPar2.removeAllItems();
for (String s : modeList)
choiceScaPar2.addItem(s);
}
//
// *TODO*
//
public void setScaPar0Tooltip(String str) { choiceScaPar0.setToolTipText(str); }
public void setScaPar1Tooltip(String str) {
choiceScaPar1.setToolTipText(str);
}
public void setScaPar2Tooltip(String str) {
choiceScaPar2.setToolTipText(str);
}
}