SOURCE CODE for TicTacToe Clientapplet which allows the user to connect to a server and play TicTacToe
import java.applet.Applet;
import java.net.*;
import java.io.*;
import java.awt.*;
// set up a TicTacToe client applet
public class TicTacToeClient extends Applet
implements Runnable {
TextField id;
TextArea display;
Panel boardPanel, panel2;
Square board[][], currentSquare;
Socket connection;
DataInputStream input;
DataOutputStream output;
Thread outputThread;
char myMark;
// set up user-interface and board
public void init()
{
setLayout ( new BorderLayout() );
display = new TextArea( 4, 30);
display.setEditable(false);
add ( "South", display );
boardPanel = new Panel();
boardPanel.setLayout( new GridLayout( 3, 3, 0, 0 ) );
board = new Square[3][3];
for ( int row = 0; row < board.length; row++ )
for ( int col = 0; col < board[row].length; col++)
{
board[row][col] = new Square();
boardPanel.add(board[row][col]);
}
id = new TextField();
id.setEditable(false);
add ( "North", id );
panel2 = new Panel();
panel2.add (boardPanel);
add ( "Center", panel2 );
}
// Make connection to server and get associated streams.
// Start separate thread to allow this applet to continually update its output in display
public void start()
{
try
{
connection = new Socket (InetAddress.getLocalHost(), 5000);
input = new DataInputStream( connection.getInputStream() );
output = new DataOutputStream (connection.getOutputStream() );
}
catch ( IOException e)
{ e.printStackTrace(); }
outputThread = new Thread ( this );
outputThread.start();
}
public boolean mouseUp( Event e, int x, int y)
// when user clicks mouse and releases in a square, an integer [0-8] representing
// the clicked square is sent to the server
{
for ( int row = 0; row < board.length; row++ )
for ( int col = 0; col < board[row].length; col++)
try {
if ( e.target == board[row][col] )
{
currentSquare = board[row][col];
output.writeInt ( row * 3 + col );
}
}
catch ( IOException exc)
{ exc.printStackTrace(); }
return true;
}
public void run()
// control thread that allows the continuous update of the display
{
// first get player's mark (X or O)
try {
myMark = input.readChar();
id.setText ( "You are player \"" + myMark + "\"" );
}
catch ( IOException e)
{ e.printStackTrace(); }
// Receive messages sent to client
while (true)
try {
String str = input.readUTF();
processMessage (str);
}
catch ( IOException exc)
{ exc.printStackTrace(); }
}
public void processMessage (String str)
// process message sent to client
{
if ( str.equals( "Valid move." ) )
{
display.appendText( "Valid move, please wait.\n" );
currentSquare.setMark (myMark);
currentSquare.repaint();
}
else if ( str.equals( "Opponent moved" ) )
{
try {
int loc = input.readInt();
done:
for ( int row = 0; row < board.length; row++ )
for ( int col = 0; col < board[row].length; col++)
if ( row * 3 + col == loc)
{
board[row][col].setMark (
( myMark == 'X' ? 'O' : 'X' ));
board[row][col].repaint();
break done;
}
display.appendText( "Opponent moved. Your turn.\n");
}
catch ( IOException e)
{ e.printStackTrace(); }
}
else
display.appendText( str + "\n");
}
}
class Square extends Canvas {
// maintains one square of the board
char mark;
public Square()
{
resize( 30, 30 );
}
public void setMark ( char ch ) { mark = ch; }
public void paint ( Graphics g)
{
g.drawRect ( 0, 0, 29, 29 );
g.drawString( String.valueOf ( mark ), 11, 20 );
}
}