java网络五子棋的源代码
#1 java网络五子棋的源代码 作者:有志青年 发表时间:2008-5-22 11:25:09
下面的源代码分为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; <