首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 操作系统-> Solaris教程
java网络五子棋的源代码-JSP教程,Java技巧及代码
作者:网友供稿 点击:555
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
下面的源代码分为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("chessclient:connectserver:无法连接,建议重新启动程序 \n");
}
return false;
}


public void actionperformed(actionevent e)
{
if(e.getsource()==controlpad.connectbutton)
{
host=chesspad.host=controlpad.inputip.gettext();
try
{
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)
{
try
{
chatsocket.close();
}
catch(exception ed)
{
}
}
if(isonchess || isgameconnected)
{
try
{
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("必须先选定一个有效用户");
}
else
{
try
{
if(!isgameconnected)
{
if(chesspad.connectserver(chesspad.host,chesspad.port))
{
isgameconnected=true;
isonchess=true;
isclient=true;
controlpad.creatgamebutton.setenabled(false);
controlpad.joingamebutton.setenabled(false);
controlpad.cancelgamebutton.setenabled(true);
chesspad.chessthread.sendmessage("/joingame "+userpad.userlist.getselecteditem()+" "+chessclientname);
}
}
else
{
isonchess=true;
isclient=true;
controlpad.creatgamebutton.setenabled(false);
controlpad.joingamebutton.setenabled(false);
controlpad.cancelgamebutton.setenabled(true);
chesspad.chessthread.sendmessage("/joingame "+userpad.userlist.getselecteditem()+" "+chessclientname);
}


}
catch(exception ee)
{
isgameconnected=false;
isonchess=false;
isclient=false;
controlpad.creatgamebutton.setenabled(true);
controlpad.joingamebutton.setenabled(true);
controlpad.cancelgamebutton.setenabled(false);
chatpad.chatlinearea.settext("chesspad.connectserver无法连接 \n"+ee);
}

}
}
if(e.getsource()==controlpad.creatgamebutton)
{
try
{
if(!isgameconnected)
{
if(chesspad.connectserver(chesspad.host,chesspad.port))
{
isgameconnected=true;
isonchess=true;
isserver=true;
controlpad.creatgamebutton.setenabled(false);
controlpad.joingamebutton.setenabled(false);
controlpad.cancelgamebutton.setenabled(true);
chesspad.chessthread.sendmessage("/creatgame "+"[inchess]"+chessclientname);
}
}
else
{
isonchess=true;
isserver=true;
controlpad.creatgamebutton.setenabled(false);
controlpad.joingamebutton.setenabled(false);
controlpad.cancelgamebutton.setenabled(true);
chesspad.chessthread.sendmessage("/creatgame "+"[inchess]"+chessclientname);
}
}
catch(exception ec)
{
isgameconnected=false;
isonchess=false;
isserver=false;
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=false;
}

}



public void keypressed(keyevent e)
{
textfield inputwords=(textfield)e.getsource();


if(e.getkeycode()==keyevent.vk_enter)
{
if(inputpad.userchoice.getselecteditem().equals("所有人"))
{
try
{
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);
}
}
else
{
try
{
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);

userpad()
{
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);

chatpad()
{
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
{
chesspad chesspad;

chessthread(chesspad chesspad)
{
this.chesspad=chesspad;
}

public void sendmessage(string sndmessage)
{
try
{
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 chesstoken;
string[] chessopt={"-1","-1","0"};
int chessoptnum=0;

while(usertoken.hasmoretokens())
{
chesstoken=(string)usertoken.nexttoken(" ");
if(chessoptnum>=1 && chessoptnum<=3)
{
chessopt[chessoptnum-1]=chesstoken;

}
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("错误:没有这个用户,请退出程序,重新加入");
}
else
{
//system.out.println(recmessage);
}
}


public void run()
{
string message="";
try
{
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=false;
textfield statustext=new textfield("请先连接服务器");

socket chesssocket;
datainputstream indata;
dataoutputstream outdata;

string chessselfname=null;
string chesspeername=null;
string host=null;
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
{
try
{
chesssocket=new socket(serverip,serverport);
indata=new datainputstream(chesssocket.getinputstream());
outdata=new dataoutputstream(chesssocket.getoutputstream());
chessthread.start();
return true;
}
catch(ioexception ex)
{
statustext.settext("chesspad:connectserver:无法连接 \n");
}
return false;
}


public void chessvictory(int chesscolorwin)
{
this.removeall();
for(int i=0;i<=chessblackcount;i++)
{
chessblack_x[i]=0;
chessblack_y[i]=0;
}
for(int i=0;i<=chesswhitecount;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<=chessblackcount;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++;
else
break;
}
for(step=1;step<=4;step++)
{
for(chesscompare=0;chesscompare<=chessblackcount;chesscompare++)
{
if(((a-step)*20==chessblack_x[chesscompare]) && (b*20==chessblack_y[chesscompare]))
{
chesslink++;
if(chesslink==5)
{
return(true);
}
}
}
if(chesslink==(chesslinktest+1))
chesslinktest++;
else
break;
}
chesslink=1;
chesslinktest=1;
for(step=1;step<=4;step++)
{
for(chesscompare=0;chesscompare<=chessblackcount;chesscompare++)
{
if((a*20==chessblack_x[chesscompare]) && ((b+step)*20==chessblack_y[chesscompare]))
{
chesslink++;
if(chesslink==5)
{
return(true);
}
}
}
if(chesslink==(chesslinktest+1))
chesslinktest++;
else
break;
}
for(step=1;step<=4;step++)
{
for(chesscompare=0;chesscompare<=chessblackcount;chesscompare++)
{
if((a*20==chessblack_x[chesscompare]) && ((b-step)*20==chessblack_y[chesscompare]))
{
chesslink++;
if(chesslink==5)
{
return(true);
}
}
}
if(chesslink==(chesslinktest+1))
chesslinktest++;
else
break;
}
chesslink=1;
chesslinktest=1;
for(step=1;step<=4;step++)
{
for(chesscompare=0;chesscompare<=chessblackcount;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
break;
}
for(step=1;step<=4;step++)
{
for(chesscompare=0;chesscompare<=chessblackcount;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
break;
}
chesslink=1;
chesslinktest=1;
for(step=1;step<=4;step++)
{
for(chesscompare=0;chesscompare<=chessblackcount;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
break;
}
for(step=1;step<=4;step++)
{
for(chesscompare=0;chesscompare<=chessblackcount;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
break;
}
}
else if(checkcolor==-1)
{
chesslink=1;
for(step=1;step<=4;step++)
{
for(chesscompare=0;chesscompare<=chesswhitecount;chesscompare++)
{
if(((a+step)*20==chesswhite_x[chesscompare]) && (b*20==chesswhite_y[chesscompare]))
{
chesslink++;
if(chesslink==5)
{
return(true);
}
}
}
if(chesslink==(chesslinktest+1))
chesslinktest++;
else
break;
}
for(step=1;step<=4;step++)
{
for(chesscompare=0;chesscompare<=chesswhitecount;chesscompare++)
{
if(((a-step)*20==chesswhite_x[chesscompare]) && (b*20==chesswhite_y[chesscompare]))
{
chesslink++;
if(chesslink==5)
{
return(true);
}
}
}
if(chesslink==(chesslinktest+1))
chesslinktest++;
else
break;
}
chesslink=1;
chesslinktest=1;
for(step=1;step<=4;step++)
{
for(chesscompare=0;chesscompare<=chesswhitecount;chesscompare++)
{
if((a*20==chesswhite_x[chesscompare]) && ((b+step)*20==chesswhite_y[chesscompare]))
{
chesslink++;
if(chesslink==5)
{
return(true);
}
}
}
if(chesslink==(chesslinktest+1))
chesslinktest++;
else
break;
}
for(step=1;step<=4;step++)
{
for(chesscompare=0;chesscompare<=chesswhitecount;chesscompare++)
{
if((a*20==chesswhite_x[chesscompare]) && ((b-step)*20==chesswhite_y[chesscompare]))
{
chesslink++;
if(chesslink==5)
{
return(true);
}
}
}
if(chesslink==(chesslinktest+1))
chesslinktest++;
else
break;
}
chesslink=1;
chesslinktest=1;
for(step=1;step<=4;step++)
{
for(chesscompare=0;chesscompare<=chesswhitecount;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++;
else
break;
}
for(step=1;step<=4;step++)
{
for(chesscompare=0;chesscompare<=chesswhitecount;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++;
else
break;
}
chesslink=1;
chesslinktest=1;
for(step=1;step<=4;step++)
{
for(chesscompare=0;chesscompare<=chesswhitecount;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++;
else
break;
}
for(step=1;step<=4;step++)
{
for(chesscompare=0;chesscompare<=chesswhitecount;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++;
else
break;
}
}
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=false;
}
else
{
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=false;
}
}
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=false;
}
else
{
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=false;
}
}
}


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=true;
}
else
{
this.add(chesspoint_black);
chesspoint_black.setbounds(chesspoint_a*20-7,chesspoint_b*20-7,16,16);
chessvictory(1);
ismouseenabled=true;
}
}
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=true;
}
else
{
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=true;
}
}
}


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)
{}
else
{
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=null;
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=null;
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
{
socket clientsocket;
hashtable clientdatahash;
hashtable clientnamehash;
hashtable chesspeerhash;
messageserver server;

boolean isclientclosed=false;

serverthread(socket clientsocket,hashtable clientdatahash,hashtable clientnamehash,hashtable chesspeerhash,messageserver server)
{
this.clientsocket=clientsocket;
this.clientdatahash=clientdatahash;
this.clientnamehash=clientnamehash;
this.chesspeerhash=chesspeerhash;
this.server=server;
}



public void messagetransfer(string message)
{
string clientname,peername;

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);
}
else
{
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)+"改名为:"+clientname;
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,selfname;
string[] chessnameopt={"0","0"};
int getoptnum=0;

while(usertoken.hasmoretokens())
{
getusertoken=(string)usertoken.nexttoken(" ");
if(getoptnum>=1 && getoptnum<=2)
{
chessnameopt[getoptnum-1]=getusertoken;
}
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));
}
else
{
chesspeertalk(selfname,"/reject");
try
{
clientclose();
}
catch(exception ez)
{
}
}
}
else if(message.startswith("/[inchess]"))
{
int firstlocation=0,lastlocation;

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));
}
}
}
else
{

int firstlocation=0,lastlocation;

lastlocation=message.indexof(" ",0);
if(lastlocation==-1)
{
feedback("无效命令");
return;
}
else
{
peername=message.substring((firstlocation+1),lastlocation);
message=message.substring((lastlocation+1));
message=(string)clientnamehash.get(clientsocket)+">"+message;
if(peertalk(peername,message))
{
feedback("没有这个用户:"+peername+"\n");
}
}

}

}

else
{
message=clientnamehash.get(clientsocket)+">"+message;
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();
try
{
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);
try
{
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);
try
{
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);
try
{
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)
{
object hashkey;
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());
try
{
clientsocket.close();
}
catch(ioexception exx)
{
}

isclientclosed=true;

}


public void run()
{
datainputstream indata;
synchronized(clientdatahash)
{
server.statuslabel.settext("当前连接数:"+clientdatahash.size());
}
try
{
indata=new datainputstream(clientsocket.getinputstream());
firstcome();
while(true)
{
string message=indata.readutf();
messagetransfer(message);
}
}
catch(ioexception esx)
{
}
finally
{
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();
serversocket serversocket;
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);
}
});
pack();
setvisible(true);
setsize(400,450);
setresizable(false);
validate();
try
{
makemessageserver(4331,server);
}
catch(exception e)
{
system.out.println("e");
}
}

public void makemessageserver(int port,messageserver server) throws ioexception
{
socket clientsocket;
long clientaccessnumber=1;
this.server=server;

try
{
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)
{
try
{
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();
}
}




文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·使用JSP对文件下载的控制-JSP教程,Jsp/Servlet
·[ASP.net(C#)]XML操作类(一)-.NET教程,C#语言
·java网络五子棋的源代码-JSP教程,Java技巧及代码
·用C#给程序加启动画面并只允许一个应用程序实例运行-.NET教程,C#语言
·.Net平台下开发中文语音应用程序-.NET教程,VB.Net语言
·用java取得本机的ip和机器名-JSP教程,Java技巧及代码
·动力设备环境及图像集中监控管理系统若干新问题——王观坤、刘崇海
·Java下XML编程接口比较:DOM SAX JDOM JAXP-.NET教程,XML应用
·使用Java技术在Cocoon中实现商业逻辑-JSP教程,Java技巧及代码
·[SQL]启动SQLServer的死锁检查-数据库专栏,SQL Server
最新文章
·日一万ip网站 gg月入万刀_网赚技巧
·浅谈cpm、cpc、cpa、pfp网站赚钱说明_网赚技巧
·王通:网站赚钱靠服务、产品和投资三种方式_网赚技巧
·网站的首页最应该放什么?_站长心得
·简洁而不简单:google成功十条真理_google推广
·google排名 如何用关键字优化网站_google推广
·用google adsense渠道跟踪你的广告(新手读)_google推广
·全面了解google 网页目录_google推广
·做完网站别忘调试----网站调试全攻略2_站长心得
·网上开店快乐赚钱-发现潜在商机_站长心得
相关主题
  • Java网络服务器编程(NIO版)-JSP教程,Java技巧及代码
  • Java网络编程的学习笔记(二)-JSP教程,Java技巧及代码
  • Java网络编程的学习笔记(一)-JSP教程,Java技巧及代码
  • Java网络服务器编程-JSP教程,Java技巧及代码
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭