/
TicTacToeClient.java
399 lines (325 loc) · 11.6 KB
/
TicTacToeClient.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
/*
CLIENT
*/
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.*;
public class TicTacToeClient extends JFrame implements Runnable {
private JButton restartButton;
private JTextField idField;
private JTextArea displayArea;
private JPanel boardPanel, panel2;
private Square board[][], currentSquare;
private Socket connection;
private DataInputStream input;
private DataOutputStream output;
private String ticTacToeHost;
private int ticTacToePort;
private char myMark;
private boolean myTurn;
private final char X_MARK = 'X', O_MARK = 'O';
private boolean continueGame = false;
// Set up user-interface and board
public TicTacToeClient(String host, int port)
{
ticTacToeHost = host;
ticTacToePort = port;
//Container container = getContentPane();
// set up JTextArea to display messages to user
displayArea = new JTextArea( 4, 30 );
displayArea.setEditable( false );
add( new JScrollPane( displayArea ), BorderLayout.SOUTH );
// set up panel for squares in board
boardPanel = new JPanel();
boardPanel.setLayout( new GridLayout( 3, 3, 0, 0 ) );
// create board
board = new Square[ 3 ][ 3 ];
// When creating a Square, the location argument to the constructor
// is a value from 0 to 8 indicating the position of the Square on
// the board. Values 0, 1, and 2 are the first row, values 3, 4,
// and 5 are the second row. Values 6, 7, and 8 are the third row.
for ( int row = 0; row < board.length; row++ ) {
for ( int column = 0; column < board[ row ].length; column++ ) {
// create Square
board[ row ][ column ] = new Square( ' ', row * 3 + column );
boardPanel.add( board[ row ][ column ] );
}
}
// textfield to display player's mark
idField = new JTextField();
idField.setEditable( false );
add( idField, BorderLayout.NORTH );
restartButton = new JButton("restart Game");
restartButton.setVisible(false);
add(restartButton, BorderLayout.EAST);
restartButton.addActionListener(new reset());
// set up panel to contain boardPanel (for layout purposes)
panel2 = new JPanel();
panel2.add( boardPanel, BorderLayout.CENTER );
add( panel2, BorderLayout.CENTER );
setSize(300, 255);
setVisible(true);
startClient();
} // end method TicTacToe Constructor
// Make connection to server and get associated streams.
// Start separate thread to allow this applet to
// continually update its output in textarea display.
private void resetBoard() {
for ( int row = 0; row < board.length; row++ ) {
for ( int column = 0; column < board[ row ].length; column++ ) {
setMark( board[ row ][ column ],
(' ') );
}
}
restartButton.setVisible(false);
}
public void startClient()
{
// connect to server, get streams and start outputThread
try {
// make connection
connection = new Socket( ticTacToeHost, ticTacToePort );
// get streams
input = new DataInputStream( connection.getInputStream() );
output = new DataOutputStream( connection.getOutputStream() );
restartButton.setVisible(false);
}
// catch problems setting up connection and streams
catch ( IOException ioException ) {
ioException.printStackTrace();
}
// create and start output thread
ExecutorService worker = Executors.newFixedThreadPool( 1 );
worker.execute( this ); // execute client
} // end method start
// control thread that allows continuous update of displayArea
public void run()
{
// get player's mark (X or O)
try {
myMark = input.readChar();
// display player ID in event-dispatch thread
SwingUtilities.invokeLater(
new Runnable() {
public void run()
{
idField.setText( "You are player \"" + myMark + "\"" );
}
}
);
myTurn = ( myMark == X_MARK ? true : false );
// receive messages sent to client and output them
while ( true ) {
processMessage( input.readUTF() );
}
} // end try
// process problems communicating with server
catch ( IOException ioException ) {
ioException.printStackTrace();
}
} // end method run
// process messages received by client
private void processMessage( String message )
{
if(message.equals("player1"))
{
myTurn = true;
myMark = X_MARK;
}
else if(message.equals("player2"))
{
myTurn = true;
myMark = O_MARK;
}
else if(message.equals("game over"))
{
SwingUtilities.invokeLater(
new Runnable() {
public void run()
{
restartButton.setVisible(true);
}
}
);
if(myTurn == false)
{
displayMessage( myMark + " won!\n" );
}
else
{
displayMessage( myMark + " lost!\n" );
}
}
// valid move occurred
else if ( message.equals( "Valid move." ) ) {
displayMessage( "Valid move, please wait.\n" );
setMark( currentSquare, myMark );
}
// invalid move occurred
else if ( message.equals( "Invalid move, try again" ) ) {
displayMessage( message + "\n" );
myTurn = true;
}
// opponent moved
else if ( message.equals( "Opponent moved" ) ) {
// get move location and update board
try {
int location = input.readInt();
int row = location / 3;
int column = location % 3;
setMark( board[ row ][ column ],
( myMark == X_MARK ? O_MARK : X_MARK ) );
displayMessage( "Opponent moved. Your turn.\n" );
myTurn = true;
} // end try
// process problems communicating with server
catch ( IOException ioException ) {
ioException.printStackTrace();
}
}
else if (message.equals("Draw")){
try{
int location = input.readInt();
if (location != -1){
int row = location / 3;
int column = location % 3;
setMark( board[ row ][ column ],( myMark == X_MARK ? O_MARK : X_MARK ) );
}
System.out.println("Player "+myMark+" Draw");
SwingUtilities.invokeLater(
new Runnable() {
public void run()
{
restartButton.setVisible(true);
}
}
);
/*SwingUtilities.invokeLater(new Runnable() {
public void run() {
displayMessage( "Draw.\n" );
restartButton.setVisible(true);
}
});*/
} // end try
catch ( IOException ioException ) {
ioException.printStackTrace();
}
//return false;
}
// simply display message
else
displayMessage( message + "\n" );
} // end method processMessage
// utility method called from other threads to manipulate
// outputArea in the event-dispatch thread
private void displayMessage( final String messageToDisplay )
{
// display message from event-dispatch thread of execution
SwingUtilities.invokeLater(
new Runnable() { // inner class to ensure GUI updates properly
public void run() // updates displayArea
{
displayArea.append( messageToDisplay );
displayArea.setCaretPosition(
displayArea.getText().length() );
}
} // end inner class
); // end call to SwingUtilities.invokeLater
}
// utility method to set mark on board in event-dispatch thread
private void setMark( final Square squareToMark, final char mark )
{
SwingUtilities.invokeLater(
new Runnable() {
public void run()
{
squareToMark.setMark( mark );
}
}
);
}
// send message to server indicating clicked square
public void sendClickedSquare( int location )
{
if ( myTurn )
{
// send location to server
try {
output.writeInt( location );
output.flush();
myTurn = false;
}
// process problems communicating with server
catch ( IOException ioException ) {
ioException.printStackTrace();
}
}
}
// set current Square
public void setCurrentSquare( Square square )
{
currentSquare = square;
}
public class reset implements ActionListener {
public void actionPerformed(ActionEvent evt) {
try {
output.writeInt(-1);
output.flush();
resetBoard();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// private inner class for the squares on the board
private class Square extends JPanel {
private char mark;
private int location;
public Square( char squareMark, int squareLocation )
{
mark = squareMark;
location = squareLocation;
addMouseListener(
new MouseAdapter() {
public void mouseReleased( MouseEvent e )
{
setCurrentSquare( Square.this );
sendClickedSquare( getSquareLocation() );
}
}
);
} // end Square constructor
// return preferred size of Square
public Dimension getPreferredSize()
{
return new Dimension( 30, 30 );
}
// return minimum size of Square
public Dimension getMinimumSize()
{
return getPreferredSize();
}
// set mark for Square
public void setMark( char newMark )
{
mark = newMark;
repaint();
}
// return Square location
public int getSquareLocation()
{
return location;
}
// draw Square
public void paintComponent( Graphics g )
{
super.paintComponent( g );
g.drawRect( 0, 0, 29, 29 );
g.drawString( String.valueOf( mark ), 11, 20 );
}
} // end inner-class Square
} // end class TicTacToeClient