-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrame.java
268 lines (237 loc) · 10.7 KB
/
Frame.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
package tictactoe;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Frame extends JFrame {
private TicTacToeClient client;
private JFrame frame;
private JButton submitButton;
private JLabel statusLabel;
private JLabel timeLabel;
private JButton[][] gameBoardButtons = new JButton[3][3];
private JLabel player1ScoreLabel;
private JLabel player2ScoreLabel;
private JLabel drawsLabel;
private JPanel scorePanel;
private JTextField textField;
private int player1Score = 0;
private int player2Score = 0;
private int draws = 0;
private int Move = 0;
private boolean nameSubmitted = false;
private boolean meExit = false;
private boolean canMove = true;
public Frame(TicTacToeClient client) {
this.client = client;
setupGUI();
}
private void setupGUI() {
frame = new JFrame();
frame.setTitle("Tic Tac Toe");
frame.setSize(700, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create menu bar
JMenuBar menuBar = new JMenuBar();
JMenu controlMenu = new JMenu("Control");
JMenu helpMenu = new JMenu("Help");
JMenuItem exitItem = new JMenuItem("Exit");
JMenuItem instructionItem = new JMenuItem("Instruction");
exitItem.addActionListener(e -> {
meExit = true ;
client.sendExitMessage();
System.exit(0);
}); // Exit the game
instructionItem.addActionListener(e -> showInstructions());
controlMenu.add(exitItem);
helpMenu.add(instructionItem);
menuBar.add(controlMenu);
menuBar.add(helpMenu);
frame.setJMenuBar(menuBar);
frame.getContentPane().setLayout(new GridBagLayout());
statusLabel = new JLabel("Enter your player name...", SwingConstants.CENTER);
GridBagConstraints gbc_welcomeLabel = new GridBagConstraints();
gbc_welcomeLabel.insets = new Insets(0, 0, 5, 5);
gbc_welcomeLabel.gridx = 1;
gbc_welcomeLabel.gridy = 0;
frame.getContentPane().add(statusLabel, gbc_welcomeLabel);
JPanel welcomePanel = new JPanel(new GridLayout(3,0));
GridBagConstraints welcomePanelConstraints = new GridBagConstraints();
welcomePanelConstraints.insets = new Insets(0, 0, 5, 5);
welcomePanelConstraints.gridx = 1;
welcomePanelConstraints.gridy = 6;
frame.getContentPane().add(welcomePanel, welcomePanelConstraints);
// Score panel
JPanel scorePanel = new JPanel(new BorderLayout());
scorePanel.setBorder(BorderFactory.createLineBorder(Color.blue, 2));
JLabel scoreTitleLabel = new JLabel("Score");
scoreTitleLabel.setHorizontalAlignment(SwingConstants.CENTER);
scorePanel.add(scoreTitleLabel, BorderLayout.NORTH);
player1ScoreLabel = new JLabel("Player 1 Wins: " + player1Score);
player2ScoreLabel = new JLabel("Player 2 Wins: " + player2Score);
drawsLabel = new JLabel("Draws: " + draws);
JPanel scoreContentPanel = new JPanel(new GridLayout(3, 1));
scoreContentPanel.add(player1ScoreLabel);
scoreContentPanel.add(player2ScoreLabel);
scoreContentPanel.add(drawsLabel);
scorePanel.add(scoreContentPanel, BorderLayout.CENTER);
GridBagConstraints scorePanelConstraints = new GridBagConstraints();
scorePanelConstraints.insets = new Insets(0, 0, 5, 0);
scorePanelConstraints.gridx = 2;
scorePanelConstraints.gridy = 6;
scorePanelConstraints.gridheight = 10;
frame.getContentPane().add(scorePanel, scorePanelConstraints);
// Game board panel
JPanel gameBoardPanel = new JPanel(new GridLayout(3, 3));
gameBoardButtons = new JButton[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
gameBoardButtons[i][j] = new JButton("");
gameBoardButtons[i][j].setBackground(Color.WHITE);
gameBoardButtons[i][j].setRolloverEnabled(false);
gameBoardButtons[i][j].setFocusPainted(false);
gameBoardButtons[i][j].setPreferredSize(new Dimension(100, 100));
Font currentFont = gameBoardButtons[i][j].getFont();
Font newFont = new Font(currentFont.getName(), currentFont.getStyle(), 30); // Set font size to 30
gameBoardButtons[i][j].setFont(newFont);
gameBoardPanel.add(gameBoardButtons[i][j]);
final int row = i;
final int col = j;
gameBoardButtons[i][j].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (nameSubmitted && canMove) {
client.sendMove(row+","+col);
Move++;
}
}
});
}
}
GridBagConstraints gamePanelConstraints = new GridBagConstraints();
gamePanelConstraints.insets = new Insets(0, 0, 5, 5);
gamePanelConstraints.gridx = 1;
gamePanelConstraints.gridy = 6;
frame.getContentPane().add(gameBoardPanel, gamePanelConstraints);
// Panel for player name input and submit button
JLabel enterNameLabel = new JLabel("Enter your name:");
GridBagConstraints enterNameLabelConstraints = new GridBagConstraints();
enterNameLabelConstraints.insets = new Insets(0, 0, 5, 5);
enterNameLabelConstraints.gridx = 0;
enterNameLabelConstraints.gridy = 16; // Set the same row as the text field
frame.getContentPane().add(enterNameLabel, enterNameLabelConstraints);
textField = new JTextField();
textField.setPreferredSize(new Dimension(200, 30));
GridBagConstraints textFieldConstraints = new GridBagConstraints();
textFieldConstraints.gridx = 1; // Set the column next to the label
textFieldConstraints.gridy = 16;
textFieldConstraints.insets = new Insets(0, 0, 5, 5);
frame.getContentPane().add(textField, textFieldConstraints);
submitButton = new JButton("Submit");
GridBagConstraints buttonConstraints = new GridBagConstraints();
buttonConstraints.insets = new Insets(0, 0, 5, 0);
buttonConstraints.gridx = 2;
buttonConstraints.gridy = 16;
frame.getContentPane().add(submitButton, buttonConstraints);
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!nameSubmitted && !textField.getText().isEmpty()) {
String playerName = textField.getText();
frame.setTitle("Tic Tac Toe - Player: " + playerName);
client.submitPlayerName(playerName);
submitButton.setEnabled(false); // Disable the submit button after name is submitted
nameSubmitted = true;
statusLabel.setText("WELCOME " + playerName); // Update the text of the label
frame.revalidate();
}
}
});
timeLabel = new JLabel();
timeLabel.setHorizontalAlignment(SwingConstants.CENTER);
GridBagConstraints timeLabelConstraints = new GridBagConstraints();
timeLabelConstraints.insets = new Insets(0, 0, 0, 5);
timeLabelConstraints.gridx = 0;
timeLabelConstraints.gridy = 30;
timeLabelConstraints.gridwidth = 2;
// timeLabelConstraints.anchor = GridBagConstraints.SOUTH;
frame.getContentPane().add(timeLabel, timeLabelConstraints);
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateTime();
}
});
timer.start();
frame.setVisible(true);
}
private void showInstructions() {
JOptionPane.showMessageDialog(frame,
"Criteria for a valid move:\r\n"
+ "- The move is not occupied by any mark.\r\n"
+ "- The move is made in the player’s turn.\r\n"
+ "- The move is made within the 3 x 3 board.\r\n"
+ "The game continues and switches among the opposite players until it reaches either:\r\n"
+ "- Player 1 wins.\r\n"
+ "- Player 2 wins.\r\n"
+ "- A draw.", "Instructions", JOptionPane.INFORMATION_MESSAGE);
}
private void updateTime() {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Date date = new Date();
timeLabel.setText("Current Time: " + sdf.format(date));
}
public void update(String line) {
// Update the game board buttons based on the received message
if(line.startsWith("Don't move.")) {
canMove = false;
if(Move!=0) {
statusLabel.setText("Valid move, wait for your opponent.");}
}
else if(line.startsWith("Can move.")) {
canMove = true;
if(Move!=0) {
statusLabel.setText("Your opponent has moved. Now is your turn.");}
}
if (line.startsWith("Board:")) {
String[] cells = line.substring(6).split(",");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
gameBoardButtons[i][j].setText(cells[i * 3 + j]);
}
}
}
if (line.equals("EXIT_GAME")&&!meExit) {
JOptionPane.showMessageDialog(frame, "Game Ends. One of the players left.","Game Over", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
if (line.contains("winplayer")||line.contains("draw")) {
if(line.contains("1")){
player1Score ++;
player1ScoreLabel.setText("Player Wins: " + player1Score);
}
else if(line.contains("2")){
player2Score ++;
player2ScoreLabel.setText("Player Wins: " + player2Score);
}
else {
draws ++ ;
drawsLabel.setText("Draws: " + draws);
}
}
if(line.contains("Would you like to play again?")) {
Move = 0;
String[] options = {"Restart","Exit"};
int choice = JOptionPane.showOptionDialog(frame,line,"Game over",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if (choice == 0) {
client.sendRestart();
} else if (choice == 1) {
meExit = true ;
client.sendExitMessage();
System.exit(0);
}
}
}
}