求java五子棋游戏代码代码

java网络五子棋的源代码-JSP教程,Java技巧及代码-虚拟主机资讯|虚拟主机动态
产品服务快速通道
----------------
==域名注册==
英文域名注册
中文域名注册
==网站推广==
==虚拟主机==
----------------
双线路虚拟主机
基本型虚拟主机
商用型虚拟主机
论坛型虚拟主机
功能型虚拟主机
Vip合租虚拟主机
虚拟主机性能对比
虚拟主机免费试用
机房速度测试
----------------
==租用托管==
服务器租用
----------------
==企业邮局==
购买企业邮局
----------------
==付款方式==
----------------
==联系我们==
您当前位置:->-> ->
java网络五子棋的源代码-JSP教程,Java技巧及代码
作者:网友供稿
  西部数码-全国虚拟主机10强!20余项管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:&
下面的源代码分为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
{
clientthread(chessclient chessclient)
this.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&))
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=
chessclient.chesspad.statustext.settext(&请黑棋下子&);
else if(chessclient.isclient)
chessclient.chesspad.chesscolor=-1;
chessclient.chesspad.statustext.settext(&已加入游戏,等待对方下子...&);
else if(recmessage.equals(&/youwin&))
chessclient.isonchess=
chessclient.chesspad.chessvictory(chessclient.chesspad.chesscolor);
chessclient.chesspad.statustext.settext(&对方退出,请点放弃游戏退出连接&);
chessclient.chesspad.ismouseenabled=
else if(recmessage.equals(&/ok&))
chessclient.chesspad.statustext.settext(&创建游戏成功,等待别人加入...&);
else if(recmessage.equals(&/error&))
chessclient.chatpad.chatlinearea.append(&传输错误:请退出程序,重新加入 \n&);
chessclient.chatpad.chatlinearea.append(recmessage+&\n&);
chessclient.chatpad.chatlinearea.setcaretposition(
chessclient.chatpad.chatlinearea.gettext().length());
public void run()
string message=&&;
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();
string chessclientname=
string host=
int port=4331;
boolean isonchat=
//在聊天?
boolean isonchess= //在下棋?
boolean isgameconnected= //下棋的客户端连接?
boolean isserver= //如果是下棋的主机
boolean isclient= //如果是下棋的客户端
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)
chatsocket.close();
catch(exception ed)
if(isonchess || isgameconnected)
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);
setsize(670,548);
setvisible(true);
setresizable(false);
validate();
public boolean connectserver(string serverip,int serverport) throws exception
chatsocket=new socket(serverip,serverport);
in=new datainputstream(chatsocket.getinputstream());
out=new dataoutputstream(chatsocket.getoutputstream());
clientthread clientthread=new clientthread(this);
clientthread.start();
catch(ioexception ex)
chatpad.chatlinearea.settext(&chessclient:connectserver:无法连接,建议重新启动程序 \n&);
public void actionperformed(actionevent e)
if(e.getsource()==controlpad.connectbutton)
host=chesspad.host=controlpad.inputip.gettext();
if(connectserver(host,port))
chatpad.chatlinearea.settext(&&);
controlpad.connectbutton.setenabled(false);
controlpad.creatgamebutton.setenabled(true);
controlpad.joingamebutton.setenabled(true);
chesspad.statustext.settext(&连接成功,请创建游戏或加入游戏&);
catch(exception ei)
chatpad.chatlinearea.settext(&controlpad.connectbutton:无法连接,建议重新启动程序 \n&);
if(e.getsource()==controlpad.exitgamebutton)
if(isonchat)
chatsocket.close();
catch(exception ed)
if(isonchess || isgameconnected)
chesspad.chesssocket.close();
catch(exception ee)
system.exit(0);
if(e.getsource()==controlpad.joingamebutton)
string selecteduser=userpad.userlist.getselecteditem();
if(selecteduser==null || selecteduser.startswith(&[inchess]&) ||
selecteduser.equals(chessclientname))
chesspad.statustext.settext(&必须先选定一个有效用户&);
if(!isgameconnected)
if(chesspad.connectserver(chesspad.host,chesspad.port))
isgameconnected=
isonchess=
controlpad.creatgamebutton.setenabled(false);
controlpad.joingamebutton.setenabled(false);
controlpad.cancelgamebutton.setenabled(true);
chesspad.chessthread.sendmessage(&/joingame &+userpad.userlist.getselecteditem()+& &+chessclientname);
isonchess=
controlpad.creatgamebutton.setenabled(false);
controlpad.joingamebutton.setenabled(false);
controlpad.cancelgamebutton.setenabled(true);
chesspad.chessthread.sendmessage(&/joingame &+userpad.userlist.getselecteditem()+& &+chessclientname);
catch(exception ee)
isgameconnected=
isonchess=
controlpad.creatgamebutton.setenabled(true);
controlpad.joingamebutton.setenabled(true);
controlpad.cancelgamebutton.setenabled(false);
chatpad.chatlinearea.settext(&chesspad.connectserver无法连接 \n&+ee);
if(e.getsource()==controlpad.creatgamebutton)
if(!isgameconnected)
if(chesspad.connectserver(chesspad.host,chesspad.port))
isgameconnected=
isonchess=
controlpad.creatgamebutton.setenabled(false);
controlpad.joingamebutton.setenabled(false);
controlpad.cancelgamebutton.setenabled(true);
chesspad.chessthread.sendmessage(&/creatgame &+&[inchess]&+chessclientname);
isonchess=
controlpad.creatgamebutton.setenabled(false);
controlpad.joingamebutton.setenabled(false);
controlpad.cancelgamebutton.setenabled(true);
chesspad.chessthread.sendmessage(&/creatgame &+&[inchess]&+chessclientname);
catch(exception ec)
isgameconnected=
isonchess=
controlpad.creatgamebutton.setenabled(true);
controlpad.joingamebutton.setenabled(true);
controlpad.cancelgamebutton.setenabled(false);
ec.printstacktrace();
chatpad.chatlinearea.settext(&chesspad.connectserver无法连接 \n&+ec);
if(e.getsource()==controlpad.cancelgamebutton)
if(isonchess)
chesspad.chessthread.sendmessage(&/giveup &+chessclientname);
chesspad.chessvictory(-1*chesspad.chesscolor);
controlpad.creatgamebutton.setenabled(true);
controlpad.joingamebutton.setenabled(true);
controlpad.cancelgamebutton.setenabled(false);
chesspad.statustext.settext(&请建立游戏或者加入游戏&);
if(!isonchess)
controlpad.creatgamebutton.setenabled(true);
controlpad.joingamebutton.setenabled(true);
controlpad.cancelgamebutton.setenabled(false);
chesspad.statustext.settext(&请建立游戏或者加入游戏&);
isclient=isserver=
public void keypressed(keyevent e)
textfield inputwords=(textfield)e.getsource();
if(e.getkeycode()==keyevent.vk_enter)
if(inputpad.userchoice.getselecteditem().equals(&所有人&))
out.writeutf(inputwords.gettext());
inputwords.settext(&&);
catch(exception ea)
chatpad.chatlinearea.settext(&chessclient:keypressed无法连接,建议重新连接 \n&);
userpad.userlist.removeall();
inputpad.userchoice.removeall();
inputwords.settext(&&);
controlpad.connectbutton.setenabled(true);
out.writeutf(&/&+inputpad.userchoice.getselecteditem()+& &+inputwords.gettext());
inputwords.settext(&&);
catch(exception ea)
chatpad.chatlinearea.settext(&chessclient:keypressed无法连接,建议重新连接 \n&);
userpad.userlist.removeall();
inputpad.userchoice.removeall();
inputwords.settext(&&);
controlpad.connectbutton.setenabled(true);
public void keytyped(keyevent e)
public void keyreleased(keyevent e)
public static void main(string args[])
chessclient chessclient=new chessclient();
/******************************************************************************************
下面是:chessinteface.java
******************************************************************************************/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class userpad extends panel
{
list userlist=new list(10);
setlayout(new borderlayout());
for(int i=0;i&50;i++)
userlist.add(i+&.&+&没有用户&);
add(userlist,borderlayout.center);
class chatpad extends panel
{
textarea chatlinearea=new textarea(&&,18,30,textarea.scrollbars_vertical_only);
setlayout(new borderlayout());
add(chatlinearea,borderlayout.center);
class controlpad extends panel
{
label iplabel=new label(&ip&,label.left);
textfield inputip=new textfield(&localhost&,10);
button connectbutton=new button(&连接主机&);
button creatgamebutton=new button(&建立游戏&);
button joingamebutton=new button(&加入游戏&);
button cancelgamebutton=new button(&放弃游戏&);
button exitgamebutton=new button(&关闭程序&);
controlpad()
setlayout(new flowlayout(flowlayout.left));
setbackground(color.pink);
add(iplabel);
add(inputip);
add(connectbutton);
add(creatgamebutton);
add(joingamebutton);
add(cancelgamebutton);
add(exitgamebutton);
class inputpad extends panel
{
textfield inputwords=new textfield(&&,40);
choice userchoice=new choice();
inputpad()
setlayout(new flowlayout(flowlayout.left));
for(int i=0;i&50;i++)
userchoice.additem(i+&.&+&没有用户&);
userchoice.setsize(60,24);
add(userchoice);
add(inputwords);
/**********************************************************************************************
下面是:chesspad.java
**********************************************************************************************/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
class chessthread extends thread
{
chessthread(chesspad chesspad)
this.chesspad=
public void sendmessage(string sndmessage)
chesspad.outdata.writeutf(sndmessage);
catch(exception ea)
system.out.println(&chessthread.sendmessage:&+ea);
public void acceptmessage(string recmessage)
if(recmessage.startswith(&/chess &))
stringtokenizer usertoken=new stringtokenizer(recmessage,& &);
string[] chessopt={&-1&,&-1&,&0&};
int chessoptnum=0;
while(usertoken.hasmoretokens())
chesstoken=(string)usertoken.nexttoken(& &);
if(chessoptnum&=1 && chessoptnum&=3)
chessopt[chessoptnum-1]=
chessoptnum++;
chesspad.netchesspaint(integer.parseint(chessopt[0]),integer.parseint(chessopt[1]),integer.parseint(chessopt[2]));
else if(recmessage.startswith(&/yourname &))
chesspad.chessselfname=recmessage.substring(10);
else if(recmessage.equals(&/error&))
chesspad.statustext.settext(&错误:没有这个用户,请退出程序,重新加入&);
//system.out.println(recmessage);
public void run()
string message=&&;
while(true)
message=chesspad.indata.readutf();
acceptmessage(message);
catch(ioexception es)
class chesspad extends panel implements mouselistener,actionlistener
{
int chesspoint_x=-1,chesspoint_y=-1,chesscolor=1;
int chessblack_x[]=new int[200];
int chessblack_y[]=new int[200];
int chesswhite_x[]=new int[200];
int chesswhite_y[]=new int[200];
int chessblackcount=0,chesswhitecount=0;
int chessblackwin=0,chesswhitewin=0;
boolean ismouseenabled=false,iswin=false,isingame=
textfield statustext=new textfield(&请先连接服务器&);
string chessselfname=
string chesspeername=
string host=
int port=4331;
chessthread chessthread=new chessthread(this);
chesspad()
setsize(440,440);
setlayout(null);
setbackground(color.pink);
addmouselistener(this);
add(statustext);
statustext.setbounds(40,5,360,24);
statustext.seteditable(false);
public boolean connectserver(string serverip,int serverport) throws exception
chesssocket=new socket(serverip,serverport);
indata=new datainputstream(chesssocket.getinputstream());
outdata=new dataoutputstream(chesssocket.getoutputstream());
chessthread.start();
catch(ioexception ex)
statustext.settext(&chesspad:connectserver:无法连接 \n&);
public void chessvictory(int chesscolorwin)
this.removeall();
for(int i=0;i&=i++)
chessblack_x[i]=0;
chessblack_y[i]=0;
for(int i=0;i&=i++)
chesswhite_x[i]=0;
chesswhite_y[i]=0;
chessblackcount=0;
chesswhitecount=0;
add(statustext);
statustext.setbounds(40,5,360,24);
if(chesscolorwin==1)
{ chessblackwin++;
statustext.settext(&黑棋胜,黑:白为&+chessblackwin+&:&+chesswhitewin+&,重新开局,等待白棋下子...&);
else if(chesscolorwin==-1)
chesswhitewin++;
statustext.settext(&白棋胜,黑:白为&+chessblackwin+&:&+chesswhitewin+&,重新开局,等待黑棋下子...&);
public void getlocation(int a,int b,int color)
if(color==1)
chessblack_x[chessblackcount]=a*20;
chessblack_y[chessblackcount]=b*20;
chessblackcount++;
else if(color==-1)
chesswhite_x[chesswhitecount]=a*20;
chesswhite_y[chesswhitecount]=b*20;
chesswhitecount++;
public boolean checkwin(int a,int b,int checkcolor)
int step=1,chesslink=1,chesslinktest=1,chesscompare=0;
if(checkcolor==1)
chesslink=1;
for(step=1;step&=4;step++)
for(chesscompare=0;chesscompare&=chesscompare++)
if(((a+step)*20==chessblack_x[chesscompare]) && ((b*20)==chessblack_y[chesscompare]))
chesslink=chesslink+1;
if(chesslink==5)
return(true);
if(chesslink==(chesslinktest+1))
chesslinktest++;
for(step=1;step&=4;step++)
for(chesscompare=0;chesscompare&=chesscompare++)
if(((a-step)*20==chessblack_x[chesscompare]) && (b*20==chessblack_y[chesscompare]))
chesslink++;
if(chesslink==5)
return(true);
if(chesslink==(chesslinktest+1))
chesslinktest++;
chesslink=1;
chesslinktest=1;
for(step=1;step&=4;step++)
for(chesscompare=0;chesscompare&=chesscompare++)
if((a*20==chessblack_x[chesscompare]) && ((b+step)*20==chessblack_y[chesscompare]))
chesslink++;
if(chesslink==5)
return(true);
if(chesslink==(chesslinktest+1))
chesslinktest++;
for(step=1;step&=4;step++)
for(chesscompare=0;chesscompare&=chesscompare++)
if((a*20==chessblack_x[chesscompare]) && ((b-step)*20==chessblack_y[chesscompare]))
chesslink++;
if(chesslink==5)
return(true);
if(chesslink==(chesslinktest+1))
chesslinktest++;
chesslink=1;
chesslinktest=1;
for(step=1;step&=4;step++)
for(chesscompare=0;chesscompare&=chesscompare++)
if(((a-step)*20==chessblack_x[chesscompare]) && ((b+step)*20==chessblack_y[chesscompare]))
chesslink++;
if(chesslink==5)
return(true);
if(chesslink==(chesslinktest+1))
chesslinktest++;
for(step=1;step&=4;step++)
for(chesscompare=0;chesscompare&=chesscompare++)
if(((a+step)*20==chessblack_x[chesscompare]) && ((b-step)*20==chessblack_y[chesscompare]))
chesslink++;
if(chesslink==5)
return(true);
if(chesslink==(chesslinktest+1))
chesslinktest++;
chesslink=1;
chesslinktest=1;
for(step=1;step&=4;step++)
for(chesscompare=0;chesscompare&=chesscompare++)
if(((a+step)*20==chessblack_x[chesscompare]) && ((b+step)*20==chessblack_y[chesscompare]))
chesslink++;
if(chesslink==5)
return(true);
if(chesslink==(chesslinktest+1))
chesslinktest++;
for(step=1;step&=4;step++)
for(chesscompare=0;chesscompare&=chesscompare++)
if(((a-step)*20==chessblack_x[chesscompare]) && ((b-step)*20==chessblack_y[chesscompare]))
chesslink++;
if(chesslink==5)
return(true);
if(chesslink==(chesslinktest+1))
chesslinktest++;
else if(checkcolor==-1)
chesslink=1;
for(step=1;step&=4;step++)
for(chesscompare=0;chesscompare&=chesscompare++)
if(((a+step)*20==chesswhite_x[chesscompare]) && (b*20==chesswhite_y[chesscompare]))
chesslink++;
if(chesslink==5)
return(true);
if(chesslink==(chesslinktest+1))
chesslinktest++;
for(step=1;step&=4;step++)
for(chesscompare=0;chesscompare&=chesscompare++)
if(((a-step)*20==chesswhite_x[chesscompare]) && (b*20==chesswhite_y[chesscompare]))
chesslink++;
if(chesslink==5)
return(true);
if(chesslink==(chesslinktest+1))
chesslinktest++;
chesslink=1;
chesslinktest=1;
for(step=1;step&=4;step++)
for(chesscompare=0;chesscompare&=chesscompare++)
if((a*20==chesswhite_x[chesscompare]) && ((b+step)*20==chesswhite_y[chesscompare]))
chesslink++;
if(chesslink==5)
return(true);
if(chesslink==(chesslinktest+1))
chesslinktest++;
for(step=1;step&=4;step++)
for(chesscompare=0;chesscompare&=chesscompare++)
if((a*20==chesswhite_x[chesscompare]) && ((b-step)*20==chesswhite_y[chesscompare]))
chesslink++;
if(chesslink==5)
return(true);
if(chesslink==(chesslinktest+1))
chesslinktest++;
chesslink=1;
chesslinktest=1;
for(step=1;step&=4;step++)
for(chesscompare=0;chesscompare&=chesscompare++)
if(((a-step)*20==chesswhite_x[chesscompare]) && ((b+step)*20==chesswhite_y[chesscompare]))
chesslink++;
if(chesslink==5)
return(true);
if(chesslink==(chesslinktest+1))
chesslinktest++;
for(step=1;step&=4;step++)
for(chesscompare=0;chesscompare&=chesscompare++)
if(((a+step)*20==chesswhite_x[chesscompare]) && ((b-step)*20==chesswhite_y[chesscompare]))
chesslink++;
if(chesslink==5)
return(true);
if(chesslink==(chesslinktest+1))
chesslinktest++;
chesslink=1;
chesslinktest=1;
for(step=1;step&=4;step++)
for(chesscompare=0;chesscompare&=chesscompare++)
if(((a+step)*20==chesswhite_x[chesscompare]) && ((b+step)*20==chesswhite_y[chesscompare]))
chesslink++;
if(chesslink==5)
return(true);
if(chesslink==(chesslinktest+1))
chesslinktest++;
for(step=1;step&=4;step++)
for(chesscompare=0;chesscompare&=chesscompare++)
if(((a-step)*20==chesswhite_x[chesscompare]) && ((b-step)*20==chesswhite_y[chesscompare]))
chesslink++;
if(chesslink==5)
return(true);
if(chesslink==(chesslinktest+1))
chesslinktest++;
return(false);
public void paint(graphics g)
for (int i=40;i&=380;i=i+20)
g.drawline(40,i,400,i);
g.drawline(40,400,400,400);
for(int j=40;j&=380;j=j+20)
g.drawline(j,40,j,400);
g.drawline(400,40,400,400);
g.filloval(97,97,6,6);
g.filloval(337,97,6,6);
g.filloval(97,337,6,6);
g.filloval(337,337,6,6);
g.filloval(217,217,6,6);
public void chesspaint(int chesspoint_a,int chesspoint_b,int color)
chesspoint_black chesspoint_black=new chesspoint_black(this);
chesspoint_white chesspoint_white=new chesspoint_white(this);
if(color==1 && ismouseenabled)
getlocation(chesspoint_a,chesspoint_b,color);
iswin=checkwin(chesspoint_a,chesspoint_b,color);
if(iswin==false)
chessthread.sendmessage(&/&+chesspeername+& /chess &+chesspoint_a+& &+chesspoint_b+& &+color);
this.add(chesspoint_black);
chesspoint_black.setbounds(chesspoint_a*20-7,chesspoint_b*20-7,16,16);
statustext.settext(&黑(第&+chessblackcount+&步)&+chesspoint_a+& &+chesspoint_b+&,请白棋下子&);
ismouseenabled=
chessthread.sendmessage(&/&+chesspeername+& /chess &+chesspoint_a+& &+chesspoint_b+& &+color);
this.add(chesspoint_black);
chesspoint_black.setbounds(chesspoint_a*20-7,chesspoint_b*20-7,16,16);
chessvictory(1);
ismouseenabled=
else if(color==-1 && ismouseenabled)
getlocation(chesspoint_a,chesspoint_b,color);
iswin=checkwin(chesspoint_a,chesspoint_b,color);
if(iswin==false)
chessthread.sendmessage(&/&+chesspeername+& /chess &+chesspoint_a+& &+chesspoint_b+& &+color);
this.add(chesspoint_white);
chesspoint_white.setbounds(chesspoint_a*20-7,chesspoint_b*20-7,16,16);
statustext.settext(&白(第&+chesswhitecount+&步)&+chesspoint_a+& &+chesspoint_b+&,请黑棋下子&);
ismouseenabled=
chessthread.sendmessage(&/&+chesspeername+& /chess &+chesspoint_a+& &+chesspoint_b+& &+color);
this.add(chesspoint_white);
chesspoint_white.setbounds(chesspoint_a*20-7,chesspoint_b*20-7,16,16);
chessvictory(-1);
ismouseenabled=
public void netchesspaint(int chesspoint_a,int chesspoint_b,int color)
chesspoint_black chesspoint_black=new chesspoint_black(this);
chesspoint_white chesspoint_white=new chesspoint_white(this);
getlocation(chesspoint_a,chesspoint_b,color);
if(color==1)
iswin=checkwin(chesspoint_a,chesspoint_b,color);
if(iswin==false)
this.add(chesspoint_black);
chesspoint_black.setbounds(chesspoint_a*20-7,chesspoint_b*20-7,16,16);
statustext.settext(&黑(第&+chessblackcount+&步)&+chesspoint_a+& &+chesspoint_b+&,请白棋下子&);
ismouseenabled=
this.add(chesspoint_black);
chesspoint_black.setbounds(chesspoint_a*20-7,chesspoint_b*20-7,16,16);
chessvictory(1);
ismouseenabled=
else if(color==-1)
iswin=checkwin(chesspoint_a,chesspoint_b,color);
if(iswin==false)
this.add(chesspoint_white);
chesspoint_white.setbounds(chesspoint_a*20-7,chesspoint_b*20-7,16,16);
statustext.settext(&白(第&+chesswhitecount+&步)&+chesspoint_a+& &+chesspoint_b+&,请黑棋下子&);
ismouseenabled=
chessthread.sendmessage(&/&+chesspeername+& /victory &+color);
this.add(chesspoint_white);
chesspoint_white.setbounds(chesspoint_a*20-7,chesspoint_b*20-7,16,16);
chessvictory(-1);
ismouseenabled=
public void mousepressed(mouseevent e)
if (e.getmodifiers()==inputevent.button1_mask)
chesspoint_x=(int)e.getx();
chesspoint_y=(int)e.gety();
int a=(chesspoint_x+10)/20,b=(chesspoint_y+10)/20;
if(chesspoint_x/20&2||chesspoint_y/20&2||chesspoint_x/20&19||chesspoint_y/20&19)
chesspaint(a,b,chesscolor);
public void mousereleased(mouseevent e){}
public void mouseentered(mouseevent e) {}
public void mouseexited(mouseevent e) {}
public void mouseclicked(mouseevent e) {}
public void actionperformed(actionevent e)
class chesspoint_black extends canvas implements mouselistener
{
chesspad chesspad=
chesspoint_black(chesspad p)
setsize(20,20);
chesspad=p;
addmouselistener(this);
public void paint(graphics g)
g.setcolor(color.black);
g.filloval(0,0,14,14);
public void mousepressed(mouseevent e)
if(e.getmodifiers()==inputevent.button3_mask)
//
chesspad.remove(this);
//
chesspad.chesscolor=1;
//
chesspad.text_2.settext(&&);
//
chesspad.text_1.settext(&请黑棋下子&);
//
public void mousereleased(mouseevent e){}
public void mouseentered(mouseevent e) {}
public void mouseexited(mouseevent e) {}
public void mouseclicked(mouseevent e) {}
}
class chesspoint_white extends canvas implements mouselistener
{
chesspad chesspad=
chesspoint_white(chesspad p)
setsize(20,20);
addmouselistener(this);
chesspad=p;
public void paint(graphics g)
g.setcolor(color.white);
g.filloval(0,0,14,14);
public void mousepressed(mouseevent e)
if(e.getmodifiers()==inputevent.button3_mask)
//
chesspad.remove(this);
//
chesspad.chesscolor=-1;
//
chesspad.text_2.settext(&请白旗下子&);
//
chesspad.text_1.settext(&&);
//
public void mousereleased(mouseevent e){}
public void mouseentered(mouseevent e) {}
public void mouseexited(mouseevent e) {}
public void mouseclicked(mouseevent e)
if(e.getclickcount()&=2)
//
chesspad.remove(this);
/******************************************************************************************
最后是:chessserver.java
*******************************************************************************************/
import java.io.*;
import java.net.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
class messageserver extends panel //implements actionlistener
{
textarea messageboard=new textarea(&&,22,50,textarea.scrollbars_vertical_only);
label statuslabel=new label(&当前连接数:&,label.left);
panel boardpanel=new panel();
panel statuspanel=new panel();
messageserver()
setsize(350,300);
setbackground(color.pink);
setlayout(new borderlayout());
boardpanel.setlayout(new flowlayout());
boardpanel.setsize(210,210);
statuspanel.setlayout(new borderlayout());
statuspanel.setsize(210,50);
boardpanel.add(messageboard);
statuspanel.add(statuslabel,borderlayout.west);
add(boardpanel,borderlayout.center);
add(statuspanel,borderlayout.north);
class serverthread extends thread
{
boolean isclientclosed=
serverthread(socket clientsocket,hashtable clientdatahash,hashtable clientnamehash,hashtable chesspeerhash,messageserver server)
this.clientsocket=
this.clientdatahash=
this.clientnamehash=
this.chesspeerhash=
this.server=
public void messagetransfer(string message)
string clientname,
if(message.startswith(&/&))
if(message.startswith(&/changename &))
clientname=message.substring(12);
if( clientname.length()&=0
|| clientname.length()&20
clientname.startswith(&/&)
|| clientnamehash.containsvalue(clientname) ||
clientname.startswith(&changename&)|| clientname.startswith(&list&)
clientname.startswith(&[inchess]&) || clientname.startswith(&creatgame&)
clientname.startswith(&joingame&)
|| clientname.startswith(&yourname&)
clientname.startswith(&userlist&)
|| clientname.startswith(&chess&)
clientname.startswith(&ok&)
|| clientname.startswith(&reject&)
clientname.startswith(&peer&)
|| clientname.startswith(&peername&)
clientname.startswith(&giveup&)
|| clientname.startswith(&youwin&)
clientname.startswith(&所有人&))
message=&无效命令&;
feedback(message);
if(clientnamehash.containsvalue((&[inchess]&+(string)clientnamehash.get(clientsocket))))
synchronized(clientnamehash)
clientnamehash.put((socket)gethashkey(clientnamehash,(&[inchess]&+clientnamehash.get(clientsocket))),
(&[inchess]&+clientname));
chesspeertalk((&[inchess]&+clientname),(&/yourname &+(&[inchess]&+clientname)));
else if(chesspeerhash.containskey(clientnamehash.get(clientsocket)))
//游戏客户端改名字
synchronized(clientnamehash)
clientnamehash.put((socket)gethashkey(clientnamehash,(&[inchess]&+clientnamehash.get(clientsocket))),
(&[inchess]&+clientname));
synchronized(chesspeerhash)
//chesspeerhash添加新名字映射
chesspeerhash.put(clientname,chesspeerhash.get(clientnamehash.get(clientsocket)));
//chesspeerhash删除旧映射
chesspeerhash.remove(clientnamehash.get(clientsocket));
//向游戏客户端发送新名字
chesspeertalk((&[inchess]&+clientname),(&/yourname &+(&[inchess]&+clientname)));
//向peer游戏客户端发送
chesspeertalk((string)chesspeerhash.get(clientname),(&/peer &+&[inchess]&+clientname));
else if(chesspeerhash.containsvalue(clientnamehash.get(clientsocket)))
synchronized(clientnamehash)
//游戏客户端改名字
clientnamehash.put((socket)gethashkey(clientnamehash,(&[inchess]&+clientnamehash.get(clientsocket))),
(&[inchess]&+clientname));
synchronized(chesspeerhash)
//chesspeerhash重新映射
chesspeerhash.put((string)gethashkey(chesspeerhash,clientnamehash.get(clientsocket)),clientname);
//向游戏客户端发送新名字
chesspeertalk((&[inchess]&+clientname),(&/yourname &+(&[inchess]&+clientname)));
//向peer游戏客户端发送
chesspeertalk((string)gethashkey(chesspeerhash,clientname),(&/peer &+&[inchess]&+clientname));
message=clientnamehash.get(clientsocket)+&改名为:&+
synchronized(clientnamehash)
clientnamehash.put(clientsocket,clientname);
publictalk(message);
feedback(&/yourname &+(string)clientnamehash.get(clientsocket));
publictalk(getuserlist());
else if(message.equals(&/list&))
feedback(getuserlist());
else if(message.startswith(&/creatgame [inchess]&))
string chessservername=message.substring(20);
synchronized(clientnamehash)
clientnamehash.put(clientsocket,message.substring(11));
synchronized(chesspeerhash)
chesspeerhash.put(chessservername,&wait&);
feedback(&/yourname &+clientnamehash.get(clientsocket));
chesspeertalk(chessservername,&/ok&);
publictalk(getuserlist());
else if(message.startswith(&/joingame &))
stringtokenizer usertoken=new stringtokenizer(message,& &);
string getusertoken,servername,
string[] chessnameopt={&0&,&0&};
int getoptnum=0;
while(usertoken.hasmoretokens())
getusertoken=(string)usertoken.nexttoken(& &);
if(getoptnum&=1 && getoptnum&=2)
chessnameopt[getoptnum-1]=
getoptnum++;
servername=chessnameopt[0];
selfname=chessnameopt[1];
if(chesspeerhash.containskey(servername) && chesspeerhash.get(servername).equals(&wait&))
synchronized(clientnamehash)
clientnamehash.put(clientsocket,(&[inchess]&+selfname));
synchronized(chesspeerhash)
chesspeerhash.put(servername,selfname);
publictalk(getuserlist());
chesspeertalk(selfname,(&/peer &+&[inchess]&+servername));
chesspeertalk(servername,(&/peer &+&[inchess]&+selfname));
chesspeertalk(selfname,&/reject&);
clientclose();
catch(exception ez)
else if(message.startswith(&/[inchess]&))
int firstlocation=0,
lastlocation=message.indexof(& &,0);
peername=message.substring((firstlocation+1),lastlocation);
message=message.substring((lastlocation+1));
if(chesspeertalk(peername,message))
feedback(&/error&);
else if(message.startswith(&/giveup &))
string chessclientname=message.substring(8);
if(chesspeerhash.containskey(chessclientname) && !((string)chesspeerhash.get(chessclientname)).equals(&wait&))
chesspeertalk((string)chesspeerhash.get(chessclientname),&/youwin&);
synchronized(chesspeerhash)
chesspeerhash.remove(chessclientname);
if(chesspeerhash.containsvalue(chessclientname))
chesspeertalk((string)gethashkey(chesspeerhash,chessclientname),&/youwin&);
synchronized(chesspeerhash)
chesspeerhash.remove((string)gethashkey(chesspeerhash,chessclientname));
int firstlocation=0,
lastlocation=message.indexof(& &,0);
if(lastlocation==-1)
feedback(&无效命令&);
peername=message.substring((firstlocation+1),lastlocation);
message=message.substring((lastlocation+1));
message=(string)clientnamehash.get(clientsocket)+&&&+
if(peertalk(peername,message))
feedback(&没有这个用户:&+peername+&\n&);
message=clientnamehash.get(clientsocket)+&&&+
server.messageboard.append(message+&\n&);
publictalk(message);
server.messageboard.setcaretposition(server.messageboard.gettext().length());
public void publictalk(string publictalkmessage)
synchronized(clientdatahash)
for(enumeration enu=clientdatahash.elements();enu.hasmoreelements();)
dataoutputstream outdata=(dataoutputstream)enu.nextelement();
outdata.writeutf(publictalkmessage);
catch(ioexception es)
es.printstacktrace();
public boolean peertalk(string peertalk,string talkmessage)
for(enumeration enu=clientdatahash.keys();enu.hasmoreelements();)
socket userclient=(socket)enu.nextelement();
if(peertalk.equals((string)clientnamehash.get(userclient)) && !peertalk.equals((string)clientnamehash.get(clientsocket)))
synchronized(clientdatahash)
dataoutputstream peeroutdata=(dataoutputstream)clientdatahash.get(userclient);
peeroutdata.writeutf(talkmessage);
catch(ioexception es)
es.printstacktrace();
feedback(talkmessage);
return(false);
else if(peertalk.equals((string)clientnamehash.get(clientsocket)))
feedback(talkmessage);
return(false);
return(true);
public boolean chesspeertalk(string chesspeertalk,string chesstalkmessage)
for(enumeration enu=clientdatahash.keys();enu.hasmoreelements();)
socket userclient=(socket)enu.nextelement();
if(chesspeertalk.equals((string)clientnamehash.get(userclient)) && !chesspeertalk.equals((string)clientnamehash.get(clientsocket)))
synchronized(clientdatahash)
dataoutputstream peeroutdata=(dataoutputstream)clientdatahash.get(userclient);
peeroutdata.writeutf(chesstalkmessage);
catch(ioexception es)
es.printstacktrace();
return(false);
return(true);
public void feedback(string feedbackstring)
synchronized(clientdatahash)
dataoutputstream outdata=(dataoutputstream)clientdatahash.get(clientsocket);
outdata.writeutf(feedbackstring);
catch(exception eb)
eb.printstacktrace();
public string getuserlist()
string userlist=&/userlist&;
for(enumeration enu=clientnamehash.elements();enu.hasmoreelements();)
userlist=userlist+& &+(string)enu.nextelement();
return(userlist);
public object gethashkey(hashtable targethash,object hashvalue)
for(enumeration enu=targethash.keys();enu.hasmoreelements();)
hashkey=(object)enu.nextelement();
if(hashvalue.equals((object)targethash.get(hashkey)))
return(hashkey);
return(null);
public void firstcome()
publictalk(getuserlist());
feedback(&/yourname &+(string)clientnamehash.get(clientsocket));
feedback(&java五子棋聊天客户端&);
feedback(&/changename &你的名字&
--更改名字&);
feedback(&/list --更新用户列表&);
feedback(&/&用户名& &要说的话&
feedback(&注意:用命令的时候,先把谈话的对象定为所有人&);
public void clientclose()
server.messageboard.append(&用户断开:&+clientsocket+&\n&);
//如果是游戏客户端主机
synchronized(chesspeerhash)
if(chesspeerhash.containskey(clientnamehash.get(clientsocket)))
chesspeerhash.remove((string)clientnamehash.get(clientsocket));
if(chesspeerhash.containsvalue(clientnamehash.get(clientsocket)))
chesspeerhash.put((string)gethashkey(chesspeerhash,(string)clientnamehash.get(clientsocket)),&tobeclosed&);
synchronized(clientdatahash)
clientdatahash.remove(clientsocket);
synchronized(clientnamehash)
clientnamehash.remove(clientsocket);
publictalk(getuserlist());
server.statuslabel.settext(&当前连接数:&+clientdatahash.size());
clientsocket.close();
catch(ioexception exx)
isclientclosed=
public void run()
synchronized(clientdatahash)
server.statuslabel.settext(&当前连接数:&+clientdatahash.size());
indata=new datainputstream(clientsocket.getinputstream());
firstcome();
while(true)
string message=indata.readutf();
messagetransfer(message);
catch(ioexception esx)
if(!isclientclosed)
clientclose();
public class chessserver extends frame implements actionlistener
{
button messageclearbutton=new button(&清除显示&);
button serverstatusbutton=new button(&服务器状态&);
button serveroffbutton=new button(&关闭服务器&);
panel buttonpanel=new panel();
messageserver server=new messageserver();
hashtable clientdatahash=new hashtable(50);
hashtable clientnamehash=new hashtable(50);
hashtable chesspeerhash=new hashtable(50);
chessserver()
super(&java五子棋服务器&);
setbackground(color.pink);
buttonpanel.setlayout(new flowlayout());
messageclearbutton.setsize(60,25);
buttonpanel.add(messageclearbutton);
messageclearbutton.addactionlistener(this);
serverstatusbutton.setsize(75,25);
buttonpanel.add(serverstatusbutton);
serverstatusbutton.addactionlistener(this);
serveroffbutton.setsize(75,25);
buttonpanel.add(serveroffbutton);
serveroffbutton.addactionlistener(this);
add(server,borderlayout.center);
add(buttonpanel,borderlayout.south);
addwindowlistener(new windowadapter()
public void windowclosing(windowevent e)
system.exit(0);
setvisible(true);
setsize(400,450);
setresizable(false);
validate();
makemessageserver(4331,server);
catch(exception e)
system.out.println(&e&);
public void makemessageserver(int port,messageserver server) throws ioexception
long clientaccessnumber=1;
this.server=
serversocket=new serversocket(port);
server.messageboard.settext(&服务器开始于:&+serversocket.getinetaddress().getlocalhost()+&:&+serversocket.getlocalport()+&\n&);
while(true)
clientsocket=serversocket.accept();
server.messageboard.append(&用户连接:&+clientsocket+&\n&);
dataoutputstream outdata=new dataoutputstream(clientsocket.getoutputstream());
clientdatahash.put(clientsocket,outdata);
clientnamehash.put(clientsocket,(&新来客&+clientaccessnumber++));
serverthread thread=new serverthread(clientsocket,clientdatahash,clientnamehash,chesspeerhash,server);
thread.start();
catch(ioexception ex)
system.out.println(&已经有服务器在运行. \n&);
public void actionperformed(actionevent e)
if(e.getsource()==messageclearbutton)
server.messageboard.settext(&&);
if(e.getsource()==serverstatusbutton)
server.messageboard.append(&服务器信息:&+serversocket.getinetaddress().getlocalhost()+&:&+serversocket.getlocalport()+&\n&);
catch(exception ee)
system.out.println(&serversocket.getinetaddress().getlocalhost() error \n&);
if(e.getsource()==serveroffbutton)
system.exit(0);
public static void main(string args[])
chessserver chessserver=new chessserver();
文章整理:西部数码--专业提供、服务
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
文章页数:&
??????????
??????????
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
版权所有 西部数码()
CopyRight (c) 2002~ all right reserved.
公司地址:四川省成都市金牛区一环路北一段99号环球广场24楼 邮编:610031
电话总机:028-08 38
售前咨询:总机转201 202 203 204 206 208
售后服务:总机转211 212 213 214
财务咨询:总机转224 223 传真:028- 财务QQ:
售前咨询QQ:2182518
售后服务QQ:

我要回帖

更多关于 java五子棋源代码 的文章

 

随机推荐