-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameGUI.java
More file actions
278 lines (236 loc) · 7.85 KB
/
Copy pathGameGUI.java
File metadata and controls
278 lines (236 loc) · 7.85 KB
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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
public class GameGUI extends JFrame implements ActionListener {
private Game game;
private String mode;
private Population p;
private Image x_img;
private Image o_img;
private JPanel panel, topPanel, gamePanel, bottomPanel, bottomLeftPanel, bottomRightPanel, bottomRTPanel, bottomRBPanel;
private JButton[] buttons;
private JButton humanButton, aiButton, playButton;
private JLabel label;
private JSlider slider;
public GameGUI() {
panel = new JPanel();
topPanel = new JPanel();
gamePanel = new JPanel();
bottomPanel = new JPanel();
bottomLeftPanel = new JPanel();
bottomRightPanel = new JPanel();
bottomRTPanel = new JPanel();
bottomRBPanel = new JPanel();
// Main Panel
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
setSize(500, 650);
// Top Panel
topPanel.setPreferredSize(new Dimension(500, 30));
label = new JLabel("Player 1's turn.");
label.setFont(new Font(label.getFont().getName(), Font.BOLD, 16));
topPanel.add(label);
// Game Panel
gamePanel.setLayout(new GridLayout(3, 3));
gamePanel.setPreferredSize(new Dimension(500, 500));
try {
Image x = ImageIO.read(getClass().getResource("/images/x.png"));
Image o = ImageIO.read(getClass().getResource("/images/o.png"));
x_img = x.getScaledInstance(120, 120, Image.SCALE_DEFAULT);
o_img = o.getScaledInstance(120, 120, Image.SCALE_DEFAULT);
} catch (IOException e1) { e1.printStackTrace(); }
buttons = new JButton[9];
for (int i = 0; i < 9; i++) {
buttons[i] = new JButton();
buttons[i].setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
buttons[i].setActionCommand((i+1) + "");
buttons[i].addActionListener(this);
gamePanel.add(buttons[i]);
}
// Bottom Panels
bottomPanel.setPreferredSize(new Dimension(500, 120));
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.X_AXIS));
// Bottom Left Panel
bottomLeftPanel.setPreferredSize(new Dimension(100, 120));
bottomLeftPanel.setLayout(new GridLayout(2, 1));
humanButton = new JButton("Human");
humanButton.setActionCommand("human");
humanButton.addActionListener(this);
aiButton = new JButton("AI");
aiButton.setActionCommand("ai");
aiButton.addActionListener(this);
bottomLeftPanel.add(humanButton);
bottomLeftPanel.add(aiButton);
// Bottom Right Top Panel
bottomRightPanel.setPreferredSize(new Dimension(400, 120));
bottomRightPanel.setLayout(new GridLayout(3, 1));
bottomRTPanel.setLayout(new GridLayout(1, 3));
playButton = new JButton("Play Again");
playButton.setActionCommand("playAgain");
playButton.addActionListener(this);
bottomRTPanel.add(new JLabel());
bottomRTPanel.add(playButton);
bottomRTPanel.add(new JLabel());
bottomRightPanel.add(bottomRTPanel);
bottomRightPanel.add(new JLabel("Number of Generations Trained", JLabel.CENTER));
// Bottom Right Bottom Panel
slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);
slider.setMajorTickSpacing(20);
slider.setPaintLabels(true);
bottomRBPanel.add(slider);
bottomRightPanel.add(bottomRBPanel);
bottomPanel.add(bottomLeftPanel);
bottomPanel.add(bottomRightPanel);
panel.add(topPanel);
panel.add(gamePanel);
panel.add(bottomPanel);
add(panel);
setTitle("Tic-Tac-Toe");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setup();
}
public void setup() {
game = new Game(0);
game.setPlayer(1);
mode = "human";
}
public void endCurrentGame() {
// Disable all board tiles and print game message
for (int i = 0; i < 9; i++)
buttons[i].setEnabled(false);
if (game.checkWin(game.getPlayer())) {
System.out.println("Congratulations, Player " + game.getPlayer() + " won!");
setLabel("Congratulations, Player " + game.getPlayer() + " won!");
}
else if (game.isFull()) {
System.out.println("It's a tie!");
setLabel("It's a tie!");
}
}
public void resetGame() {
clearButtons();
game.resetGame();
setLabel("Game was reset. Player " + game.getPlayer() + "'s turn.");
}
public void setButton(JButton button, int button_num) {
if (!button.isEnabled())
return;
button.setEnabled(false);
if (game.getPlayer() == 1)
button.setIcon(new ImageIcon(x_img));
else
button.setIcon(new ImageIcon(o_img));
}
public void clearButtons() {
for (int i = 0; i < 9; i++) {
buttons[i].setEnabled(true);
buttons[i].setIcon(null);
}
}
public void setLabel(String text) {
label.setText(text);
}
/* Action Commands for Buttons:
*
* Play Again: "playAgain"
* Human Mode: "human"
* AI Mode: "ai"
* Game Tile: Gives the tile number (1-9)
*
*
*/
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
String command = button.getActionCommand();
System.out.println("Button Command Pressed: " + command);
if (command == "playAgain") {
resetGame();
}
else if (command == "human") {
if (mode == "human")
setLabel("You are already in Human vs. Human mode");
else if (!game.isEmpty())
setLabel("You can only switch modes at the beginning of the game.");
else {
setLabel("NOT YET IMPLEMENTED");
}
}
else if (command == "ai") {
if (mode == "ai")
setLabel("You are already in Human vs. AI mode");
else if (!game.isEmpty())
setLabel("You can only switch modes at the beginning of the game.");
else {
mode = "ai";
p = new Population(20);
p.printScores();
for (int i = 0; i < 1000; i++) {
System.out.println("Generation: " + (i+1));
p.evolve();
//p.printScores();
}
}
}
else { // It is a game tile (1-9)
if (mode == "human") {
// Set button press
int buttonNum = Integer.parseInt(command);
setButton(buttons[buttonNum - 1], buttonNum);
game.sendTurn(game.getPlayer(), buttonNum);
// Check for end of game
if (game.checkWin(game.getPlayer()) || game.isFull()) {
endCurrentGame();
return;
}
game.advancePlayer();
System.out.println("Player " + game.getPlayer() + "'s turn.");
setLabel("Player " + game.getPlayer() + "'s turn.");
}
else {
if (game.getPlayer() == 1) {
int buttonNum = Integer.parseInt(command);
setButton(buttons[buttonNum - 1], buttonNum);
game.sendTurn(game.getPlayer(), buttonNum);
// Check for end of game
if (game.checkWin(game.getPlayer()) || game.isFull()) {
endCurrentGame();
return;
}
game.advancePlayer();
System.out.println("Player " + game.getPlayer() + "'s turn.");
setLabel("Player " + game.getPlayer() + "'s turn.");
// AI
System.out.println("Finding move response");
int response = p.findResponse(game.getBoard());
if (response == -1)
System.out.println("ERROR COULD NOT FIND RESPONSE");
System.out.println("Found move " + response);
setButton(buttons[response - 1], response);
game.sendTurn(2, response);
if (game.checkWin(game.getPlayer()) || game.isFull()) {
endCurrentGame();
return;
}
game.advancePlayer();
System.out.println("Player " + game.getPlayer() + "'s turn.");
setLabel("Player " + game.getPlayer() + "'s turn.");
}
}
}
}
}