import java.applet.Applet;
import java.awt.*;
import java.io.*;
import java.net.*;
public class TicTacToeclient extends Applet implements Runnable
{//class1
TextField id;
Panel boardpanel, panel2;
Square board[][],currentsquare;
Socket connection;
DataInputStream input;
DataOutputStream output;
Thread outputThread;
char mymark;
TextArea display;
public void init()
{
setLayout(new BorderLayout());
display=new TextArea(4,30);
display.setEditable(false);
add("South",display);
boardpanel=new Panel();
boardpanel.setBackground(Color.cyan);
boardpanel.setLayout(new GridLayout(3,3,0,0));
board=new Square[3][3];
for(int row=0; row
board[row][col]=new Square();
repaint();
boardpanel.add(board[row][col]);
}
id=new TextField();
id.setEditable(false);
add("North",id);
panel2=new Panel();
panel2.add(boardpanel);
add("Center",panel2);
}
//////////end
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();
}
//////////end
public boolean mouseUP(Event e, int x, int y)
{
for( int row=0; row
for(int col=0; col
if(e.target==board[row][col])
{
currentsquare=board[row][col];
output.writeInt(row*3+col);
}
}
catch(IOException ie){
//ie.printStackTrace();
}
}
return true;
}
//////////end
public void run()
{
try{
mymark=input.readChar();
id.setText("欢迎您玩家\""+mymark+"\"");
}
catch(IOException e){
e.printStackTrace();
}
while(true)
{
try{
String s=input.readUTF();
processMessage(s);
}
catch(IOException e){
//e.printStackTrace();
}
}
}
//////////end
public void processMessage(String s)
{
if(s.equals("Valid move"))
{
display.appendText("Valid move,please wait\n");
currentsquare.setMark(mymark);
currentsquare.repaint();
}
else if(s.equals("Invalid move,tryagain"))
{
display.appendText(s+"\n");
}
else if(s.equals("Opponent moved"))
{
try{
int loc=input.readInt();
done:
for(int row=0; row
{
board[row][col].setMark(mymark=='x' ? 'o':'x');
board[row][col].repaint();
break done;
}
display.appendText("Opponent moved.Yourturn\n");
}
catch(IOException e){
e.printStackTrace();
}
}
else
{
display.appendText(s+"\n");
}
}
}//class1.end
//////////////////////////////////////
class Square extends Canvas
{//class2
char mark;
public Square()
{
resize(30,30);
}
//////////end
public void setMark(char c)
{
mark=c;
}
//////////end
public void paint(Graphics g)
{
g.drawRect(0,0,29,29);
g.drawString(String.valueOf(mark),11,20);
}
}//class2.end
//
服务器端:
import java.awt.*;
import java.net.*;
import java.io.*;
public class TicTacToeServer extends Frame
{//class1
private byte board[];
private boolean xMove;
private TextArea output;
private Player players[];
private ServerSocket server;
private int numberofplayers;
private int currentplayer;
public TicTacToeServer()
{
super("三子棋服务器");
board=new byte[9];
xMove=true;
players=new Player[2];
currentplayer=0;
try{
server=new ServerSocket(5000,2);
}
catch(IOException e){
// e.printStackrace();
System.exit(1);
}
output=new TextArea();
output.setBackground(Color.yellow);
add("Center",output);
resize(300,300);
show();
Toolkit tp=Toolkit.getDefaultToolkit();
Image logo=tp.getImage("1.gif");
setIconImage(logo);
setResizable(false);
}
//////////end
public void execute()
{
for(int i=0; i
try{
players[i]=new Player(server.accept(), this, i);
players[i].start();
++numberofplayers;
}
catch(IOException e){
//e.printStackrace();
System.exit(1);
}
}
}
//////////end
public int getNumberOfplayers()
{
return numberofplayers;
}
//////////end
public void display(String s)
{
output.appendText(s+"\n");
}
/////////end
public boolean validMove(int loc,int player)
{
boolean moveDone=false;
while(player!=currentplayer)
{
try{
wait();
}
catch(InterruptedException e){//not
}
}
if(isOccupied(loc))
{
board[loc]=(byte)(currentplayer==0 ? 'x' : 'o');
currentplayer=++currentplayer%2;
players[currentplayer].otherplayerMoved(loc);
notify();
return true;
}
else
return false;
}
//////////end
public boolean isOccupied(int loc)
{
if(board[loc]=='x'||board[loc]=='o')
return true;
else
return false;
}
//////////end
public boolean handleEvent(Event event)
{
if(event.id==Event.WINDOW_DESTROY)
{
hide();
dispose();
for(int i=0; i
System.exit(0);
}
return super.handleEvent(event);
}
//////////end
public boolean gameOver()
{
return false;
}
//////////end
public static void main(String args[])
{
TicTacToeServer game=new TicTacToeServer();
game.execute();
}
}//class1.end
////////////////////////////////////////////////next.class
class Player extends Thread
{//class2
Socket connection;
DataInputStream input;
DataOutputStream output;
TicTacToeServer control;
int number;
char mark;
public Player(Socket s, TicTacToeServer t,int num)
{
mark=(num==0 ? 'x' : 'o');
connection=s;
try{
input=new DataInputStream(connection.getInputStream());
output=new DataOutputStream(connection.getOutputStream());
}
catch(IOException e){
//e.printStackTrale();
System.exit(1);
}
control=t;
number=num;
}
//////////end
public void otherplayerMoved(int loc)
{
try{
output.writeUTF("Opponet moved");
output.writeInt(loc);
}
catch(IOException e){//not
}
}
//////////end
public void run()
{
boolean done=false;
try{
control.display("玩家"+(number==0 ? 'x' : 'o')+"以登陆!");
output.writeChar(mark);
output.writeUTF("玩家"+(number==0 ? "x 以登陆!\n" : "o 以登陆,请等待!\n"));
if(control.getNumberOfplayers()<2)
{
output.writeUTF("请您等待另一个玩家登陆!");
while(control.getNumberOfplayers()<2);
output.writeUTF("另一个玩家已经登陆!现在您可以走棋了!");
}
while(!done)
{
int location=input.readInt();
if(control.validMove(location,number))
{
control.display("loc"+location);
output.writeUTF("Valic move.");
}
else
output.writeUTF("INvalid move,tryagain");
if(control.gameOver())
{
done=true;
}
connection.close();
}
}
catch(IOException e){
e.printStackTrace();
System.exit(1);
}
}
}//class.end
找我!