導入Java . awt . *;
導入Java . io . *;
導入Java . net . *;
公共類TicTacToeclient擴展Applet實現Runnable
{//class1
文本字段id;
Panel boardpanel,panel2
方板[][],currentsquare
插座連接;
DataInputStream輸入;
DataOutputStream輸出;
線程輸出線程;
char mymark
文本區顯示;
公共void init()
{
set layout(new BorderLayout());
display=new TextArea(4,30);
display . set editable(false);
添加(“南”,顯示);
board Panel = new Panel();
board panel . set background(color . cyan);
board panel . set layout(new GridLayout(3,3,0,0));
板=新方塊[3][3];
for(int row = 0;row & lt板長;row++)
for(int col = 0;col & lt木板[排]。長度;col++)
{
board[row][col]=新方塊();
repaint();
board panel . add(board[row][col]);
}
id = new TextField();
id . set editable(false);
添加(“北”,id);
Panel 2 = new Panel();
panel 2 . add(board panel);
add("中心",面板2);
}
///////////結束
公共void開始()
{
嘗試{
connection = new Socket(inet address . getlocalhost(),5000);
input = new data inputstream(connection . getinputstream());
output = new data output stream(connection . get output stream());
}
catch(IOException e){
//e . printstacktrace();
}
outputThread =新線程(this);
output thread . start();
}
///////////結束
公共布爾mouseUP(事件e,int x,int y)
{
for(int row = 0;row & lt板長;row++)
{
for(int col = 0;col & lt木板[排]。長度;col++)
嘗試{
if(例如,目標= =板[行][列])
{
current square = board[row][col];
output . write int(row * 3+col);
}
}
catch(IOException ie){
//ie . printstacktrace();
}
}
返回true
}
///////////結束
公共無效運行()
{
嘗試{
my mark = input . read char();
Id.setText("歡迎玩\ " "+mymark+" \ ");
}
catch(IOException e){
e . printstacktrace();
}
while(真)
{
嘗試{
string s = input . read utf();
processMessage
}
catch(IOException e){
//e . printstacktrace();
}
}
}
///////////結束
公共void processMessage(字符串s)
{
if(s等於(“有效移動”))
{
display.appendText("有效移動,請稍候\ n ");
current square . set mark(my mark);
current square . repaint();
}
else if(s.equals("無效移動,重試"))
{
display . appendtext(s+" \ n ");
}
else if(s.equals("對手移動"))
{
嘗試{
int loc = input . readint();
完成:
for(int row = 0;row & lt板長;row++)
for(int col = 0;col & lt木板[排]。長度;col++)
if(row*3+col==loc)
{
板[行][列]。setMark(mymark=='x '?o ':' x ');
板[行][列]。repaint();
休息完畢;
}
display.appendText("對手動了。輪到妳了\ n ");
}
catch(IOException e){
e . printstacktrace();
}
}
其他
{
display . appendtext(s+" \ n ");
}
}
}//class1.end
//////////////////////////////////////
類方形擴展畫布
{//class2
字符標記;
公共廣場()
{
調整大小(30,30);
}
///////////結束
公共空設置標記(char c)
{
mark = c;
}
///////////結束
公共空白油漆(圖形g)
{
g.drawRect(0,0,29,29);
g . drawstring(string . value of(mark),11,20);
}
}//class2.end
//& lt;applet code = " tictactoeclient . class " width = 275 height = 300 & gt;& lt/applet & gt;
服務器端:
導入Java . awt . *;
導入Java . net . *;
導入Java . io . *;
公共類TicTacToeServer擴展框架
{//class1
私有字節板[];
私有布爾xMove
私有TextArea輸出;
私人玩家玩家[];
私有服務器套接字服務器;
private int numberofplayers
私有int currentplayer
公共TicTacToeServer()
{
超級(“三子棋牌服務器”);
board =新字節[9];
xMove = true
玩家=新玩家[2];
current player = 0;
嘗試{
server=new ServerSocket(5000,2);
}
catch(IOException e){
//e . printstackrace();
system . exit(1);
}
output = new TextArea();
output . set background(color . yellow);
add(“居中”,輸出);
調整大小(300,300);
show();
toolkit TP = toolkit . getdefaulttoolkit();
image logo = TP . getimage(" 1 . gif ");
seticon image(logo);
setResizable(false);
}
///////////結束
public void execute()
{
for(int I = 0;我& lt球員.長度;i++)
{
嘗試{
players[I]=新玩家(server.accept(),this,I);
玩家[我]。start();
++ number of players;
}
catch(IOException e){
//e . printstackrace();
system . exit(1);
}
}
}
///////////結束
public int getNumberOfplayers()
{
返回numberofplayers
}
///////////結束
公共空顯示(字符串s)
{
output . appendtext(s+" \ n ");
}
//////////結束
公共布爾validMove(int loc,int player)
{
布爾moveDone = false
而(玩家!=當前播放器)
{
嘗試{
wait();
}
catch(interrupted exception e){//not
}
}
if(等積(loc))
{
board[loc]=(byte)(current player = = 0?x ':' o ');
current player = ++ current player % 2;
玩家[當前玩家]。other player moved(loc);
notify();
返回true
}
其他
返回false
}
///////////結束
公共布爾等積(int loc)
{
if(board[loc]= = ' x ' | | board[loc]= = ' o ')
返回true
其他
返回false
}
///////////結束
公共布爾handleEvent(事件事件)
{
if(event.id==Event。窗口_銷毀)
{
hide();
dispose();
for(int I = 0;我& lt球員.長度;i++)
玩家[我]。stop();
system . exit(0);
}
返回super.handleEvent(事件);
}
///////////結束
公共布爾gameOver()
{
返回false
}
///////////結束
公共靜態void main(String args[])
{
TicTacToeServer game = new TicTacToeServer();
game . execute();
}
}//class1.end
//////////////////////////////////////////////////下壹節課
類播放器擴展線程
{//class2
插座連接;
DataInputStream輸入;
DataOutputStream輸出;
TicTacToeServer控件;
int數;
字符標記;
公共播放器(套接字s,TicTacToeServer t,int num)
{
mark=(num==0?x ':' o ');
connection = s;
嘗試{
input = new data inputstream(connection . getinputstream());
output = new data output stream(connection . get output stream());
}
catch(IOException e){
//e . printstacktrale();
system . exit(1);
}
控制= t;
number = num
}
///////////結束
public void other player moved(int loc)
{
嘗試{
output . write utf(" Opponet moved ");
output . write int(loc);
}
catch(IOException e){//not
}
}
///////////結束
公共無效運行()
{
布爾done = false
嘗試{
control . display(" player "+(number = = 0?X': 'o')+"登錄!");
output . write char(mark);
output . write utf(" player "+(number = = 0?“X降落!\n": "o要登錄,請稍候!\ n "));
if(control . getnumberofplayers()& lt;2)
{
Output.writeUTF("請等待另壹個玩家登錄!");
while(control . getnumberofplayers()& lt;2);
Output.writeUTF("另壹個玩家登錄了!現在妳可以下棋了!”);
}
而(!完成)
{
int location = input . readint();
if(control.validMove(location,number))
{
control . display(" loc "+location);
output.writeUTF("Valic move。");
}
其他
output.writeUTF("無效移動,重試");
if(control.gameOver())
{
done = true
}
connection . close();
}
}
catch(IOException e){
e . printstacktrace();
system . exit(1);
}
}
}//class.end