-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToeClient.java
More file actions
268 lines (234 loc) · 9.33 KB
/
Copy pathTicTacToeClient.java
File metadata and controls
268 lines (234 loc) · 9.33 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
import java.net.Socket;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.event.*;
import java.util.concurrent.ExecutionException;
/**
*
* Client side class for the Tic Tac Toe game.
* Builds the initial set up for the game on the client side.
* Connects to the Tic Tac Toe server and communicates with it through messages to facilitate
* game play.
*
* @author Janvi Sharma
*
*/
public class TicTacToeClient {
private JFrame gameFrame = new JFrame("Tic Tac Toe");
private JLabel messageLabel = new JLabel("Enter your player name...");
private JPanel bottomPanel = new JPanel();
private JButton startButton = new JButton("Submit");
private JTextField nameOfPlayer = new JTextField(20);
private int startGame = 0, mouseCtr=0;
private Socket socket;
private Scanner scannerInstream;
private PrintWriter writerOutstream;
private GameBoxPanel[] gameBox = new GameBoxPanel[9];
private GameBoxPanel currPlayingBox;
/**
* Constructor for class TicTacToeClient
* Sets up socket connection to the server and loads
* GUI components on the client side
* Also sets mouse listener to the playing grid to send messages to the server.
*/
public TicTacToeClient() throws Exception {
socket = new Socket("127.0.0.1", 55000);
scannerInstream = new Scanner(socket.getInputStream());
writerOutstream = new PrintWriter(socket.getOutputStream(), true);
messageLabel.setBackground(Color.lightGray);
gameFrame.getContentPane().add(messageLabel, BorderLayout.NORTH);
bottomPanel.add(nameOfPlayer);
bottomPanel.add(startButton);
startButton.addActionListener(new startButtonListener());
bottomPanel.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
gameFrame.add(bottomPanel, BorderLayout.SOUTH);
JPanel gameSetup = new JPanel();
gameSetup.setBackground(Color.black);
gameSetup.setLayout(new GridLayout(3, 3, 2, 2));
JMenuBar menuBar = new JMenuBar();
JMenu controlMenu = new JMenu("Control");
JMenu helpMenu = new JMenu("Help");
JMenuItem exitControl = new JMenuItem("Exit");
exitControl.addActionListener(new ControlActionListener());
JMenuItem instructionsHelp = new JMenuItem("Instructions");
instructionsHelp.addActionListener(new HelpActionListener());
controlMenu.add(exitControl);
helpMenu.add(instructionsHelp);
menuBar.add(controlMenu);
menuBar.add(helpMenu);
gameFrame.setJMenuBar(menuBar);
for (int i = 0; i < gameBox.length; i++) {
final int j = i;
gameBox[i] = new GameBoxPanel();
gameBox[i].addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
currPlayingBox = gameBox[j];
if(startGame==1) {
mouseCtr++;
System.out.println("Mouse count: "+mouseCtr);
writerOutstream.println("Move " + j); }
}
});
gameSetup.add(gameBox[i]);
}
gameFrame.getContentPane().add(gameSetup, BorderLayout.CENTER);
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* Method playGame() facilitates communication with the server through messages.
* The server responds with messages telling if the move is valid, if the opponent moved,
* if the client won or lost or if the other player leaves the game.
* There are appropriate ways to handle each message according to the game rules.
*
*/
public void playGame() throws Exception {
try {
var serverMessage = scannerInstream.nextLine();
var clientSymbol = serverMessage.charAt(8);
System.out.println(serverMessage);
var opponentSymbol = clientSymbol == 'X' ? 'O' : 'X';
while (scannerInstream.hasNextLine()) {
serverMessage = scannerInstream.nextLine();
if (serverMessage.startsWith("ValidMove")) {
messageLabel.setText("Valid move, wait for your opponent.");
currPlayingBox.setText(clientSymbol);
currPlayingBox.repaint();
} else
if (serverMessage.startsWith("OppMoved")) {
var loc = Integer.parseInt(serverMessage.substring(9));
gameBox[loc].setText(opponentSymbol);
gameBox[loc].repaint();
messageLabel.setText("Your opponent has moved, now is your turn.");
} else
if (serverMessage.startsWith("Message")) {
messageLabel.setText(serverMessage.substring(8));
} else
if (serverMessage.startsWith("Winner")) {
JOptionPane.showMessageDialog(gameFrame, "Congratulations. You Win.");
break;
} else
if (serverMessage.startsWith("Loss")) {
JOptionPane.showMessageDialog(gameFrame, "You lose.");
break;
} else
if (serverMessage.startsWith("Draw")) {
JOptionPane.showMessageDialog(gameFrame, "Draw.");
break;
} else
if (serverMessage.startsWith("OppLeft")) {
JOptionPane.showMessageDialog(gameFrame, "Game Ends. One of the players left.");
break;
}
}
writerOutstream.println("QuitGame");
} catch (Exception e) {
e.printStackTrace();
}
finally {
socket.close();
gameFrame.dispose();
}
}
/**
* A game box panel for displaying the playing grid for the client.
* Overrides setText() method so that the image can change once
* user clicks on it given that the move is "allowed" by the game rules.
*
* @author Janvi Sharma
*
*/
static class GameBoxPanel extends JPanel {
JLabel newGameLabel = new JLabel();
public GameBoxPanel() {
setBackground(Color.white);
setLayout(new GridBagLayout());
newGameLabel.setFont(new Font("Courier", Font.BOLD, 40));
add(newGameLabel);
}
public void setText(char text) {
if(text=='X') {
newGameLabel.setForeground(Color.green);
newGameLabel.setText(text+"");
}
else
newGameLabel.setForeground(Color.red);
newGameLabel.setText(text+"");
}
}
/**
* Main function of client
* Creates an instance of TicTacToeClient class
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
TicTacToeClient client = new TicTacToeClient();
client.gameFrame.setSize(400, 400);
client.gameFrame.setVisible(true);
}
/**
* startButtonListener to see if "submit" button is clicked
* by user and name is input.
* Changes the GUI according to the name of user.
* Enables method to communicate with the server to faciliate the game process.
*
* @author Janvi Sharma
*
*/
public class startButtonListener implements ActionListener {
/**
* Function that changes the GUI according to the name
* entered and starts a new thread for the method playGame()
*/
public void actionPerformed(ActionEvent event){
String name = nameOfPlayer.getText();
nameOfPlayer.setText("");
messageLabel.setText("WELCOME "+name);
startButton.setEnabled(false);
startGame = 1;
gameFrame.setTitle("Tic Tac Toe- Player " + name);
writerOutstream.println("ButtonClicked");
new Thread() {
public void run() {
try {
playGame();
} catch (Exception ex) {ex.printStackTrace();}
}
}.start();
}
}
/**
* ControlActionListener is the Listener for JMenuItem "Exit"
* Terminates program at client side if "exit" is clicked.
*
* @author Janvi Sharma
*
*/
public class ControlActionListener implements ActionListener {
/**
* Function that enables the user to exit the game
*/
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
}
/**
* HelpActionListener is the Listener for JMenuItem "Instructions"
* Displays the game rules and instructions for playing the game when clicked.
*
* @author Janvi Sharma
*
*/
public class HelpActionListener implements ActionListener {
/**
* Function that displays the rules of the game
*/
public void actionPerformed(ActionEvent event) {
String message = "Some information about the game.\nCriteria for a valid move:\n- The move is not occupied by any mark.\n- The move is made in the player's turn.\n- The move is made within the 3 x 3 board.\nThe game would continue and switch among the opposite player until it reaches either one of the following conditions:\n - Player 1 wins.\n - Player 2 wins.\n - Draw.";
JOptionPane.showMessageDialog(null, message);
}
}
}