服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java网络五子棋的源代码


  下面的源代码分为4个文件;
chessclient.java:客户端主程序。
chessinterface.java:客户端的界面。
chesspad.java:棋盘的绘制。
chessserver.java:服务器端。
可同时容纳50个人同时在线下棋,聊天。
没有加上详细注释,不过绝对可以运行,j2sdk1.4下通过。


/*********************************************************************************************
1.chessclient.java
**********************************************************************************************/

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;


class clientthread extends thread
{
chessclient chessclient;

clientthread(chessclient chessclient)
{
this.chessclient=chessclient;
}

public void acceptmessage(string recmessage)
{
if(recmessage.startswith("/userlist "))
{
stringtokenizer usertoken=new stringtokenizer(recmessage," ");
int usernumber=0;

chessclient.userpad.userlist.removeall();
chessclient.inputpad.userchoice.removeall();
chessclient.inputpad.userchoice.additem("所有人");
while(usertoken.hasmoretokens())
{
string user=(string)usertoken.nexttoken(" ");
if(usernumber>0 && !user.startswith("[inchess]"))
{
chessclient.userpad.userlist.add(user);
chessclient.inputpad.userchoice.additem(user);
}

usernumber++;
}
chessclient.inputpad.userchoice.select("所有人");
}
else if(recmessage.startswith("/yourname "))
{
chessclient.chessclientname=recmessage.substring(10);
chessclient.settitle("java五子棋客户端 "+"用户名:"+chessclient.chessclientname);
}
else if(recmessage.equals("/reject"))
{
try
{
chessclient.chesspad.statustext.settext("不能加入游戏");
chessclient.controlpad.cancelgamebutton.setenabled(false);
chessclient.controlpad.joingamebutton.setenabled(true);
chessclient.controlpad.creatgamebutton.setenabled(true);
}
catch(exception ef)
{
chessclient.chatpad.chatlinearea.settext("chessclient.chesspad.chesssocket.close无法关闭");
}
chessclient.controlpad.joingamebutton.setenabled(true);
}
else if(recmessage.startswith("/peer "))
{
chessclient.chesspad.chesspeername=recmessage.substring(6);
if(chessclient.isserver)
{
chessclient.chesspad.chesscolor=1;
chessclient.chesspad.ismouseenabled=true;
chessclient.chesspad.statustext.settext("请黑棋下子");
}
else if(chessclient.isclient)
{
chessclient.chesspad.chesscolor=-1;
chessclient.chesspad.statustext.settext("已加入游戏,等待对方下子...");
}

}
else if(recmessage.equals("/youwin"))
{
chessclient.isonchess=false;
chessclient.chesspad.chessvictory(chessclient.chesspad.chesscolor);
chessclient.chesspad.statustext.settext("对方退出,请点放弃游戏退出连接");
chessclient.chesspad.ismouseenabled=false;
}
else if(recmessage.equals("/ok"))
{
chessclient.chesspad.statustext.settext("创建游戏成功,等待别人加入...");
}
else if(recmessage.equals("/error"))
{
chessclient.chatpad.chatlinearea.append("传输错误:请退出程序,重新加入 /n");
}
else
{
chessclient.chatpad.chatlinearea.append(recmessage+"/n");
chessclient.chatpad.chatlinearea.setcaretposition(
chessclient.chatpad.chatlinearea.gettext().length());
}
}


public void run()
{
string message="";
try
{
while(true)
{
message=chessclient.in.readutf();
acceptmessage(message);
}
}
catch(ioexception es)
{
}
}

}






public class chessclient extends frame implements actionlistener,keylistener
{
userpad userpad=new userpad();
chatpad chatpad=new chatpad();
controlpad controlpad=new controlpad();
chesspad chesspad=new chesspad();
inputpad inputpad=new inputpad();


socket chatsocket;
datainputstream in;
dataoutputstream out;
string chessclientname=null;
string host=null;
int port=4331;

boolean isonchat=false; //在聊天?
boolean isonchess=false; //在下棋?
boolean isgameconnected=false; //下棋的客户端连接?
boolean isserver=false; //如果是下棋的主机
boolean isclient=false; //如果是下棋的客户端

panel southpanel=new panel();
panel northpanel=new panel();
panel centerpanel=new panel();
panel westpanel=new panel();
panel eastpanel=new panel();

chessclient()
{
super("java五子棋客户端");
setlayout(new borderlayout());
host=controlpad.inputip.gettext();

westpanel.setlayout(new borderlayout());
westpanel.add(userpad,borderlayout.north);
westpanel.add(chatpad,borderlayout.center);
westpanel.setbackground(color.pink);

inputpad.inputwords.addkeylistener(this);
chesspad.host=controlpad.inputip.gettext();

centerpanel.add(chesspad,borderlayout.center);
centerpanel.add(inputpad,borderlayout.south);
centerpanel.setbackground(color.pink);

controlpad.connectbutton.addactionlistener(this);
controlpad.creatgamebutton.addactionlistener(this);
controlpad.joingamebutton.addactionlistener(this);
controlpad.cancelgamebutton.addactionlistener(this);
controlpad.exitgamebutton.addactionlistener(this);

controlpad.creatgamebutton.setenabled(false);
controlpad.joingamebutton.setenabled(false);
controlpad.cancelgamebutton.setenabled(false);

southpanel.add(controlpad,borderlayout.center);
southpanel.setbackground(color.pink);


addwindowlistener(new windowadapter()
{
public void windowclosing(windowevent e)
{
if(isonchat)
{
try
{
chatsocket.close();
}
catch(exception ed)
{
}
}
if(isonchess || isgameconnected)
{
try
{
chesspad.chesssocket.close();
}
catch(exception ee)
{
}
}
system.exit(0);
}
public void windowactivated(windowevent ea)
{

}
});

add(westpanel,borderlayout.west);
add(centerpanel,borderlayout.center);
add(southpanel,borderlayout.south);

pack();
setsize(670,548);
setvisible(true);
setresizable(false);
validate();
}



public boolean connectserver(string serverip,int serverport) throws exception
{
try
{
chatsocket=new socket(serverip,serverport);
in=new datainputstream(chatsocket.getinputstream());
out=new dataoutputstream(chatsocket.getoutputstream());

clientthread clientthread=new clientthread(this);
clientthread.start();
isonchat=true;
return true;
}
catch(ioexception ex)
{
chatpad.chatlinearea.settext("chesscl

扫描关注微信公众号