-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCommonGUI.java
322 lines (286 loc) · 9.54 KB
/
CommonGUI.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
package Main;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import com.jgoodies.forms.factories.DefaultComponentFactory;
public class CommonGUI {
public ImageIcon image;
public JLabel labelForImage;
static ArrayList<JLabel> countriesPlayer1;
static ArrayList<JLabel> countriesPlayer2;
static ReinforcementWindow reinformentWindow;
static AttackWindow attackWindow;
static int xDim, yDim;
int flag_sim_1 = 1, flag_sim_2 = 0;
JButton reinforcement, attack, take_turn;
public void generateImage() {
if (WhichMap.map_choice == 0) {
image = new ImageIcon("egypt.jpeg");
} else {
image = new ImageIcon("usa.jpeg");
}
labelForImage = new JLabel(image);
labelForImage.setBounds(400, 50, 1000, 700);
}
public void createReinforce_Attack_takeTurn() {
if (WelcomeWindow.flagOfHuman == 1) {
reinforcement = new JButton("Reinforcement");
reinforcement.setBounds(700, 50, 150, 50);
reinforcement.setForeground(Color.BLACK);
reinforcement.setBackground(Color.WHITE);
attack = new JButton("Attack");
attack.setBounds(950, 50, 150, 50);
attack.setForeground(Color.BLACK);
attack.setBackground(Color.WHITE);
}
take_turn = new JButton("Take turn");
take_turn.setBounds(820, 750, 150, 50);
take_turn.setForeground(Color.BLACK);
take_turn.setBackground(Color.WHITE);
control3Buttons();
}
public void generateFirstTroops() {
countriesPlayer1 = new ArrayList<>();
countriesPlayer2 = new ArrayList<>();
JLabel x;
xDim = 100;
yDim = 100;
for (Country country : Randomize.player1Countries) {
x = DefaultComponentFactory.getInstance().createLabel(country.name + " " + RiskMap.armies.get(country));
x.setFont(new Font("Dialog", Font.BOLD, 16));
x.setForeground(Color.WHITE);
x.setBounds(xDim, yDim, 300, 30);
countriesPlayer1.add(x);
if (WelcomeWindow.flagOfHuman == 1) {
GameGUIHumane.frame.getContentPane().add(x);
} else {
GameGUISimulation.frame.getContentPane().add(x);
}
yDim += 30;
}
xDim = 1500;
yDim = 100;
for (Country country : Randomize.player2Countries) {
x = DefaultComponentFactory.getInstance().createLabel(country.name + " " + RiskMap.armies.get(country));
x.setFont(new Font("Dialog", Font.BOLD, 16));
x.setForeground(Color.WHITE);
x.setBounds(xDim, yDim, 300, 30);
countriesPlayer2.add(x);
if (WelcomeWindow.flagOfHuman == 1) {
GameGUIHumane.frame.getContentPane().add(x);
} else {
GameGUISimulation.frame.getContentPane().add(x);
}
yDim += 30;
}
}
public void generateTroops() {
for (JLabel jlabel : countriesPlayer1) {
jlabel.setVisible(false);
}
for (JLabel jlabel : countriesPlayer2) {
jlabel.setVisible(false);
}
countriesPlayer1 = new ArrayList<>();
countriesPlayer2 = new ArrayList<>();
JLabel x;
xDim = 100;
yDim = 100;
for (Entry<Country, String> entry : RiskMap.owners.entrySet()) {
if (entry.getValue().equals("player1")) {
x = DefaultComponentFactory.getInstance().createLabel(entry.getKey().name + " " + RiskMap.armies.get(entry.getKey()));
x.setFont(new Font("Dialog", Font.BOLD, 16));
x.setForeground(Color.WHITE);
x.setBounds(xDim, yDim, 300, 30);
countriesPlayer1.add(x);
if (WelcomeWindow.flagOfHuman == 1) {
GameGUIHumane.frame.getContentPane().add(x);
} else {
GameGUISimulation.frame.getContentPane().add(x);
}
yDim += 30;
}
}
xDim = 1500;
yDim = 100;
for (Entry<Country, String> entry : RiskMap.owners.entrySet()) {
if (entry.getValue().equals("player2")) {
x = DefaultComponentFactory.getInstance().createLabel(entry.getKey().name + " " + RiskMap.armies.get(entry.getKey()));
x.setFont(new Font("Dialog", Font.BOLD, 16));
x.setForeground(Color.WHITE);
x.setBounds(xDim, yDim, 300, 30);
countriesPlayer2.add(x);
if (WelcomeWindow.flagOfHuman == 1) {
GameGUIHumane.frame.getContentPane().add(x);
} else {
GameGUISimulation.frame.getContentPane().add(x);
}
yDim += 30;
}
}
}
public void control3Buttons() {
if (WelcomeWindow.flagOfHuman == 1) {
reinforcement.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (WelcomeWindow.flagOfHuman == 1) {
getReinforcementWindow();
}
}
});
attack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (WelcomeWindow.flagOfHuman == 1) {
getAttackWindow();
}
}
});
}
take_turn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (WelcomeWindow.flagOfHuman == 1) {
//String agent = ChooseAgents.player2_human;
ChooseAgents.playerVSHuman.reinforce();
generateTroops();
try {
Thread.sleep(200);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
Thread.currentThread().interrupt();
}
ChooseAgents.playerVSHuman.attack();
generateTroops();
//ChooseAgents.playerVSHuman.attack();
} else {
validateSimulation();
generateTroops();
if (flag_sim_1 == 1) {
flag_sim_1 = 0;
} else {
flag_sim_1 = 1;
}
if (flag_sim_2 == 1) {
flag_sim_2 = 0;
} else {
flag_sim_2 = 1;
}
}
validateEndOfGame();
}
});
}
public void validateEndOfGame() {
String s1 = "player1", s2 = "player2";
int flag1 = 0, flag2 = 0;
for (Entry<Country, String> entry : RiskMap.owners.entrySet()) {
if (entry.getValue().equals(s1)) {
flag1 += 1;
}
}
for (Entry<Country, String> entry : RiskMap.owners.entrySet()) {
if (!entry.getValue().equals(s1)) {
flag2 += 1;
}
}
if (flag1 == RiskMap.owners.entrySet().size()) {
if (WelcomeWindow.flagOfHuman == 1) {
JOptionPane.showMessageDialog(GameGUIHumane.frame, "Congratulations, you won :)");
} else {
JOptionPane.showMessageDialog(GameGUISimulation.frame, "Congratulations, player 1 won :)");
}
} else if (flag2 == RiskMap.owners.entrySet().size()) {
if (WelcomeWindow.flagOfHuman == 1) {
JOptionPane.showMessageDialog(GameGUIHumane.frame, "Sorry, you lost.");
} else {
JOptionPane.showMessageDialog(GameGUISimulation.frame, "Congratulations, player 2 won.");
}
}
if (WelcomeWindow.flagOfHuman == 1) {
if (ChooseAgents.player2_human.equals("Greedy")) {
Evaluation evaluate = new Evaluation(GreedyPlayer.l, GreedyPlayer.t);
evaluate.calceval(GreedyPlayer.l, GreedyPlayer.t);
evaluate.calceval2(GreedyPlayer.l, GreedyPlayer.t);
evaluate.calceval3(GreedyPlayer.l, GreedyPlayer.t);
} else if (ChooseAgents.player2_human.equals("AStar")) {
Evaluation evaluate = new Evaluation(AStarPlayer.l, AStarPlayer.t);
evaluate.calceval(AStarPlayer.l, AStarPlayer.t);
evaluate.calceval2(AStarPlayer.l, AStarPlayer.t);
evaluate.calceval3(AStarPlayer.l, AStarPlayer.t);
} else if (ChooseAgents.player2_human.equals("RTAStar")) {
Evaluation evaluate = new Evaluation(RTAStarPlayer.l, RTAStarPlayer.t);
evaluate.calceval(RTAStarPlayer.l, RTAStarPlayer.t);
evaluate.calceval2(RTAStarPlayer.l, RTAStarPlayer.t);
evaluate.calceval3(RTAStarPlayer.l, RTAStarPlayer.t);
}
} else {
if (ChooseAgents.player1.equals("Greedy") || ChooseAgents.player2.equals("Greedy")) {
Evaluation evaluate = new Evaluation(GreedyPlayer.l, GreedyPlayer.t);
evaluate.calceval(GreedyPlayer.l, GreedyPlayer.t);
evaluate.calceval2(GreedyPlayer.l, GreedyPlayer.t);
evaluate.calceval3(GreedyPlayer.l, GreedyPlayer.t);
} else if (ChooseAgents.player1.equals("AStar") || ChooseAgents.player2.equals("AStar")) {
Evaluation evaluate = new Evaluation(AStarPlayer.l, AStarPlayer.t);
evaluate.calceval(AStarPlayer.l, AStarPlayer.t);
evaluate.calceval2(AStarPlayer.l, AStarPlayer.t);
evaluate.calceval3(AStarPlayer.l, AStarPlayer.t);
} else if (ChooseAgents.player1.equals("RTAStar") || ChooseAgents.player2.equals("RTAStar")) {
Evaluation evaluate = new Evaluation(RTAStarPlayer.l, RTAStarPlayer.t);
evaluate.calceval(RTAStarPlayer.l, RTAStarPlayer.t);
evaluate.calceval2(RTAStarPlayer.l, RTAStarPlayer.t);
evaluate.calceval3(RTAStarPlayer.l, RTAStarPlayer.t);
}
}
int max = 100;
for (Entry<Country, Integer> entry : RiskMap.armies.entrySet()) {
if (entry.getValue() > max) {
if (WelcomeWindow.flagOfHuman == 0) {
if (ChooseAgents.player1.equals(ChooseAgents.player2)) {
JOptionPane.showMessageDialog(GameGUISimulation.frame, "Stuck !!!");
}
}
}
}
}
public void validateSimulation() {
if (flag_sim_1 == 1 && flag_sim_2 == 0) {
ChooseAgents.player_1.reinforce();
ChooseAgents.player_1.attack();
} else if (flag_sim_2 == 1 && flag_sim_1 == 0) {
ChooseAgents.player_2.reinforce();
ChooseAgents.player_2.attack();
}
}
public void getReinforcementWindow() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
reinformentWindow = new ReinforcementWindow();
reinformentWindow.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void getAttackWindow() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
attackWindow = new AttackWindow();
attackWindow.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}