|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
为什么外国人还要写那些框架进行代码封装,他们不就是为了别人使用时可以更简单么!如果要达到一个企业级项目的不用框架是很难的。小一些的项目还行,大的光是MVC模式的设计的编码量就够大的了。还有性能方面,单轮windows,这个工具是微软写的,。收集|源代码上面的源代码分为4个文件;
chessClient.java:客户端主程序。
chessInterface.java:客户真个界面。
chessPad.java:棋盘的绘制。
chessServer.java:服务器端。
可同时包容50团体同时在线下棋,谈天。
没有加上具体正文,不外相对能够运转,j2sdk1.4下经由过程。
/*********************************************************************************************
1.chessClient.java
**********************************************************************************************/
importjava.awt.*;
importjava.awt.event.*;
importjava.io.*;
importjava.net.*;
importjava.util.*;
classclientThreadextendsThread
{
chessClientchessclient;
clientThread(chessClientchessclient)
{
this.chessclient=chessclient;
}
publicvoidacceptMessage(StringrecMessage)
{
if(recMessage.startsWith("/userlist"))
{
StringTokenizeruserToken=newStringTokenizer(recMessage,"");
intuserNumber=0;
chessclient.userpad.userList.removeAll();
chessclient.inputpad.userChoice.removeAll();
chessclient.inputpad.userChoice.addItem("一切人");
while(userToken.hasMoreTokens())
{
Stringuser=(String)userToken.nextToken("");
if(userNumber>0&&!user.startsWith("[inchess]"))
{
chessclient.userpad.userList.add(user);
chessclient.inputpad.userChoice.addItem(user);
}
userNumber++;
}
chessclient.inputpad.userChoice.select("一切人");
}
elseif(recMessage.startsWith("/yourname"))
{
chessclient.chessClientName=recMessage.substring(10);
chessclient.setTitle("Java五子棋客户端"+"用户名:"+chessclient.chessClientName);
}
elseif(recMessage.equals("/reject"))
{
try
{
chessclient.chesspad.statusText.setText("不克不及到场游戏");
chessclient.controlpad.cancelGameButton.setEnabled(false);
chessclient.controlpad.joinGameButton.setEnabled(true);
chessclient.controlpad.creatGameButton.setEnabled(true);
}
catch(Exceptionef)
{
chessclient.chatpad.chatLineArea.setText("chessclient.chesspad.chessSocket.close没法封闭");
}
chessclient.controlpad.joinGameButton.setEnabled(true);
}
elseif(recMessage.startsWith("/peer"))
{
chessclient.chesspad.chessPeerName=recMessage.substring(6);
if(chessclient.isServer)
{
chessclient.chesspad.chessColor=1;
chessclient.chesspad.isMouseEnabled=true;
chessclient.chesspad.statusText.setText("请黑棋下子");
}
elseif(chessclient.isClient)
{
chessclient.chesspad.chessColor=-1;
chessclient.chesspad.statusText.setText("已到场游戏,守候对方下子...");
}
}
elseif(recMessage.equals("/youwin"))
{
chessclient.isOnChess=false;
chessclient.chesspad.chessVictory(chessclient.chesspad.chessColor);
chessclient.chesspad.statusText.setText("对方加入,请点保持游戏加入毗连");
chessclient.chesspad.isMouseEnabled=false;
}
elseif(recMessage.equals("/OK"))
{
chessclient.chesspad.statusText.setText("创立游戏乐成,守候他人到场...");
}
elseif(recMessage.equals("/error"))
{
chessclient.chatpad.chatLineArea.append("传输毛病:请加入程序,从头到场
");
}
else
{
chessclient.chatpad.chatLineArea.append(recMessage+"
");
chessclient.chatpad.chatLineArea.setCaretPosition(
chessclient.chatpad.chatLineArea.getText().length());
}
}
publicvoidrun()
{
Stringmessage="";
try
{
while(true)
{
message=chessclient.in.readUTF();
acceptMessage(message);
}
}
catch(IOExceptiones)
{
}
}
}
publicclasschessClientextendsFrameimplementsActionListener,KeyListener
{
userPaduserpad=newuserPad();
chatPadchatpad=newchatPad();
controlPadcontrolpad=newcontrolPad();
chessPadchesspad=newchessPad();
inputPadinputpad=newinputPad();
SocketchatSocket;
DataInputStreamin;
DataOutputStreamout;
StringchessClientName=null;
Stringhost=null;
intport=4331;
booleanisOnChat=false;//在谈天?
booleanisOnChess=false;//鄙人棋?
booleanisGameConnected=false;//下棋的客户端毗连?
booleanisServer=false;//假如是下棋的主机
booleanisClient=false;//假如是下棋的客户端
PanelsouthPanel=newPanel();
PanelnorthPanel=newPanel();
PanelcenterPanel=newPanel();
PanelwestPanel=newPanel();
PaneleastPanel=newPanel();
chessClient()
{
super("Java五子棋客户端");
setLayout(newBorderLayout());
host=controlpad.inputIP.getText();
westPanel.setLayout(newBorderLayout());
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(newWindowAdapter()
{
publicvoidwindowClosing(WindowEvente)
{
if(isOnChat)
{
try
{
chatSocket.close();
}
catch(Exceptioned)
{
}
}
if(isOnChess||isGameConnected)
{
try
{
chesspad.chessSocket.close();
}
catch(Exceptionee)
{
}
}
System.exit(0);
}
publicvoidwindowActivated(WindowEventea)
{
}
});
add(westPanel,BorderLayout.WEST);
add(centerPanel,BorderLayout.CENTER);
add(southPanel,BorderLayout.SOUTH);
pack();
setSize(670,548);
setVisible(true);
setResizable(false);
validate();
}
publicbooleanconnectServer(StringserverIP,intserverPort)throwsException
{
try
{
chatSocket=newSocket(serverIP,serverPort);
in=newDataInputStream(chatSocket.getInputStream());
out=newDataOutputStream(chatSocket.getOutputStream());
clientThreadclientthread=newclientThread(this);
clientthread.start();
isOnChat=true;
returntrue;
}
catch(IOExceptionex)
{
chatpad.chatLineArea.setText("chessClient:connectServer:没法毗连,倡议从头启动程序
");
}
returnfalse;
}
publicvoidactionPerformed(ActionEvente)
{
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(Exceptionei)
{
chatpad.chatLineArea.setText("controlpad.connectButton:没法毗连,倡议从头启动程序
");
}
}
if(e.getSource()==controlpad.exitGameButton)
{
if(isOnChat)
{
try
{
chatSocket.close();
}
catch(Exceptioned)
{
}
}
if(isOnChess||isGameConnected)
{
try
{
chesspad.chessSocket.close();
}
catch(Exceptionee)
{
}
}
System.exit(0);
}
if(e.getSource()==controlpad.joinGameButton)
{
StringselectedUser=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(Exceptionee)
{
isGameConnected=false;
isOnChess=false;
isClient=false;
controlpad.creatGameButton.setEnabled(true);
controlpad.joinGameButton.setEnabled(true);
controlpad.cancelGameButton.setEnabled(false);
chatpad.chatLineArea.setText("chesspad.connectServer没法毗连
"+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(Exceptionec)
{
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没法毗连
"+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;
}
}
publicvoidkeyPressed(KeyEvente)
{
TextFieldinputWords=(TextField)e.getSource();
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
if(inputpad.userChoice.getSelectedItem().equals("一切人"))
{
try
{
out.writeUTF(inputWords.getText());
inputWords.setText("");
}
catch(Exceptionea)
{
chatpad.chatLineArea.setText("chessClient:KeyPressed没法毗连,倡议从头毗连
");
userpad.userList.removeAll();
inputpad.userChoice.removeAll();
inputWords.setText("");
controlpad.connectButton.setEnabled(true);
}
}
else
{
try
{
out.writeUTF("/"+inputpad.userChoice.getSelectedItem()+""+inputWords.getText());
inputWords.setText("");
}
catch(Exceptionea)
{
chatpad.chatLineArea.setText("chessClient:KeyPressed没法毗连,倡议从头毗连
");
userpad.userList.removeAll();
inputpad.userChoice.removeAll();
inputWords.setText("");
controlpad.connectButton.setEnabled(true);
}
}
}
}
publicvoidkeyTyped(KeyEvente)
{
}
publicvoidkeyReleased(KeyEvente)
{
}
publicstaticvoidmain(Stringargs[])
{
chessClientchessClient=newchessClient();
}
}
/******************************************************************************************
上面是:chessInteface.java
******************************************************************************************/
importjava.awt.*;
importjava.awt.event.*;
importjava.io.*;
importjava.net.*;
classuserPadextendsPanel
{
ListuserList=newList(10);
userPad()
{
setLayout(newBorderLayout());
for(inti=0;i<50;i++)
{
userList.add(i+"."+"没有效户");
}
add(userList,BorderLayout.CENTER);
}
}
classchatPadextendsPanel
{
TextAreachatLineArea=newTextArea("",18,30,TextArea.SCROLLBARS_VERTICAL_ONLY);
chatPad()
{
setLayout(newBorderLayout());
add(chatLineArea,BorderLayout.CENTER);
}
}
classcontrolPadextendsPanel
{
LabelIPlabel=newLabel("IP",Label.LEFT);
TextFieldinputIP=newTextField("localhost",10);
ButtonconnectButton=newButton("毗连主机");
ButtoncreatGameButton=newButton("创建游戏");
ButtonjoinGameButton=newButton("到场游戏");
ButtoncancelGameButton=newButton("保持游戏");
ButtonexitGameButton=newButton("封闭程序");
controlPad()
{
setLayout(newFlowLayout(FlowLayout.LEFT));
setBackground(Color.pink);
add(IPlabel);
add(inputIP);
add(connectButton);
add(creatGameButton);
add(joinGameButton);
add(cancelGameButton);
add(exitGameButton);
}
}
classinputPadextendsPanel
{
TextFieldinputWords=newTextField("",40);
ChoiceuserChoice=newChoice();
inputPad()
{
setLayout(newFlowLayout(FlowLayout.LEFT));
for(inti=0;i<50;i++)
{
userChoice.addItem(i+"."+"没有效户");
}
userChoice.setSize(60,24);
add(userChoice);
add(inputWords);
}
}
/**********************************************************************************************
上面是:chessPad.java
**********************************************************************************************/
importjava.awt.*;
importjava.awt.event.*;
importjava.io.*;
importjava.net.*;
importjava.util.*;
classchessThreadextendsThread
{
chessPadchesspad;
chessThread(chessPadchesspad)
{
this.chesspad=chesspad;
}
publicvoidsendMessage(StringsndMessage)
{
try
{
chesspad.outData.writeUTF(sndMessage);
}
catch(Exceptionea)
{
System.out.println("chessThread.sendMessage:"+ea);
}
}
publicvoidacceptMessage(StringrecMessage)
{
if(recMessage.startsWith("/chess"))
{
StringTokenizeruserToken=newStringTokenizer(recMessage,"");
StringchessToken;
String[]chessOpt={"-1","-1","0"};
intchessOptNum=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]));
}
elseif(recMessage.startsWith("/yourname"))
{
chesspad.chessSelfName=recMessage.substring(10);
}
elseif(recMessage.equals("/error"))
{
chesspad.statusText.setText("毛病:没有这个用户,请加入程序,从头到场");
}
else
{
//System.out.println(recMessage);
}
}
publicvoidrun()
{
Stringmessage="";
try
{
while(true)
{
message=chesspad.inData.readUTF();
acceptMessage(message);
}
}
catch(IOExceptiones)
{
}
}
}
classchessPadextendsPanelimplementsMouseListener,ActionListener
{
intchessPoint_x=-1,chessPoint_y=-1,chessColor=1;
intchessBlack_x[]=newint[200];
intchessBlack_y[]=newint[200];
intchessWhite_x[]=newint[200];
intchessWhite_y[]=newint[200];
intchessBlackCount=0,chessWhiteCount=0;
intchessBlackWin=0,chessWhiteWin=0;
booleanisMouseEnabled=false,isWin=false,isInGame=false;
TextFieldstatusText=newTextField("请先毗连服务器");
SocketchessSocket;
DataInputStreaminData;
DataOutputStreamoutData;
StringchessSelfName=null;
StringchessPeerName=null;
Stringhost=null;
intport=4331;
chessThreadchessthread=newchessThread(this);
chessPad()
{
setSize(440,440);
setLayout(null);
setBackground(Color.pink);
addMouseListener(this);
add(statusText);
statusText.setBounds(40,5,360,24);
statusText.setEditable(false);
}
publicbooleanconnectServer(StringServerIP,intServerPort)throwsException
{
try
{
chessSocket=newSocket(ServerIP,ServerPort);
inData=newDataInputStream(chessSocket.getInputStream());
outData=newDataOutputStream(chessSocket.getOutputStream());
chessthread.start();
returntrue;
}
catch(IOExceptionex)
{
statusText.setText("chessPad:connectServer:没法毗连
");
}
returnfalse;
}
publicvoidchessVictory(intchessColorWin)
{
this.removeAll();
for(inti=0;i<=chessBlackCount;i++)
{
chessBlack_x[i]=0;
chessBlack_y[i]=0;
}
for(inti=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+",从头残局,守候白棋下子...");
}
elseif(chessColorWin==-1)
{
chessWhiteWin++;
statusText.setText("白棋胜,黑:白为"+chessBlackWin+":"+chessWhiteWin+",从头残局,守候黑棋下子...");
}
}
publicvoidgetLocation(inta,intb,intcolor)
{
if(color==1)
{
chessBlack_x[chessBlackCount]=a*20;
chessBlack_y[chessBlackCount]=b*20;
chessBlackCount++;
}
elseif(color==-1)
{
chessWhite_x[chessWhiteCount]=a*20;
chessWhite_y[chessWhiteCount]=b*20;
chessWhiteCount++;
}
}
publicbooleancheckWin(inta,intb,intcheckColor)
{
intstep=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;
}
}
elseif(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);
}
publicvoidpaint(Graphicsg)
{
for(inti=40;i<=380;i=i+20)
{
g.drawLine(40,i,400,i);
}
g.drawLine(40,400,400,400);
for(intj=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);
}
publicvoidchessPaint(intchessPoint_a,intchessPoint_b,intcolor)
{
chessPoint_blackchesspoint_black=newchessPoint_black(this);
chessPoint_whitechesspoint_white=newchessPoint_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;
}
}
elseif(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;
}
}
}
publicvoidnetChessPaint(intchessPoint_a,intchessPoint_b,intcolor)
{
chessPoint_blackchesspoint_black=newchessPoint_black(this);
chessPoint_whitechesspoint_white=newchessPoint_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;
}
}
elseif(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;
}
}
}
publicvoidmousePressed(MouseEvente)
{
if(e.getModifiers()==InputEvent.BUTTON1_MASK)
{
chessPoint_x=(int)e.getX();
chessPoint_y=(int)e.getY();
inta=(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);
}
}
}
publicvoidmouseReleased(MouseEvente){}
publicvoidmouseEntered(MouseEvente){}
publicvoidmouseExited(MouseEvente){}
publicvoidmouseClicked(MouseEvente){}
publicvoidactionPerformed(ActionEvente)
{
}
}
classchessPoint_blackextendsCanvasimplementsMouseListener
{
chessPadchesspad=null;
chessPoint_black(chessPadp)
{
setSize(20,20);
chesspad=p;
addMouseListener(this);
}
publicvoidpaint(Graphicsg)
{
g.setColor(Color.black);
g.fillOval(0,0,14,14);
}
publicvoidmousePressed(MouseEvente)
{
//if(e.getModifiers()==InputEvent.BUTTON3_MASK)
//{
//chesspad.remove(this);
//chesspad.chessColor=1;
//chesspad.text_2.setText("");
//chesspad.text_1.setText("请黑棋下子");
//}
}
publicvoidmouseReleased(MouseEvente){}
publicvoidmouseEntered(MouseEvente){}
publicvoidmouseExited(MouseEvente){}
publicvoidmouseClicked(MouseEvente){}
}
classchessPoint_whiteextendsCanvasimplementsMouseListener
{
chessPadchesspad=null;
chessPoint_white(chessPadp)
{
setSize(20,20);
addMouseListener(this);
chesspad=p;
}
publicvoidpaint(Graphicsg)
{
g.setColor(Color.white);
g.fillOval(0,0,14,14);
}
publicvoidmousePressed(MouseEvente)
{
//if(e.getModifiers()==InputEvent.BUTTON3_MASK)
//{
//chesspad.remove(this);
//chesspad.chessColor=-1;
//chesspad.text_2.setText("请白旗下子");
//chesspad.text_1.setText("");
//}
}
publicvoidmouseReleased(MouseEvente){}
publicvoidmouseEntered(MouseEvente){}
publicvoidmouseExited(MouseEvente){}
publicvoidmouseClicked(MouseEvente)
{
//if(e.getClickCount()>=2)
//chesspad.remove(this);
}
}
/******************************************************************************************
最初是:chessServer.java
*******************************************************************************************/
importjava.io.*;
importjava.net.*;
importjava.awt.*;
importjava.util.*;
importjava.awt.event.*;
classMessageServerextendsPanel//implementsActionListener
{
TextAreamessageBoard=newTextArea("",22,50,TextArea.SCROLLBARS_VERTICAL_ONLY);
LabelstatusLabel=newLabel("以后毗连数:",Label.LEFT);
PanelboardPanel=newPanel();
PanelstatusPanel=newPanel();
MessageServer()
{
setSize(350,300);
setBackground(Color.pink);
setLayout(newBorderLayout());
boardPanel.setLayout(newFlowLayout());
boardPanel.setSize(210,210);
statusPanel.setLayout(newBorderLayout());
statusPanel.setSize(210,50);
boardPanel.add(messageBoard);
statusPanel.add(statusLabel,BorderLayout.WEST);
add(boardPanel,BorderLayout.CENTER);
add(statusPanel,BorderLayout.NORTH);
}
}
classServerThreadextendsThread
{
SocketclientSocket;
HashtableclientDataHash;
HashtableclientNameHash;
HashtablechessPeerHash;
MessageServerserver;
booleanisClientClosed=false;
ServerThread(SocketclientSocket,HashtableclientDataHash,HashtableclientNameHash,HashtablechessPeerHash,MessageServerserver)
{
this.clientSocket=clientSocket;
this.clientDataHash=clientDataHash;
this.clientNameHash=clientNameHash;
this.chessPeerHash=chessPeerHash;
this.server=server;
}
publicvoidmessageTransfer(Stringmessage)
{
StringclientName,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)));
}
}
elseif(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));
}
elseif(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());
}
}
elseif(message.equals("/list"))
{
Feedback(getUserList());
}
elseif(message.startsWith("/creatgame[inchess]"))
{
StringchessServerName=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());
}
elseif(message.startsWith("/joingame"))
{
StringTokenizeruserToken=newStringTokenizer(message,"");
StringgetUserToken,serverName,selfName;
String[]chessNameOpt={"0","0"};
intgetOptNum=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(Exceptionez)
{
}
}
}
elseif(message.startsWith("/[inchess]"))
{
intfirstLocation=0,lastLocation;
lastLocation=message.indexOf("",0);
peerName=message.substring((firstLocation+1),lastLocation);
message=message.substring((lastLocation+1));
if(chessPeerTalk(peerName,message))
{
Feedback("/error");
}
}
elseif(message.startsWith("/giveup"))
{
StringchessClientName=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
{
intfirstLocation=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+"
");
}
}
}
}
else
{
message=clientNameHash.get(clientSocket)+">"+message;
server.messageBoard.append(message+"
");
publicTalk(message);
server.messageBoard.setCaretPosition(server.messageBoard.getText().length());
}
}
publicvoidpublicTalk(StringpublicTalkMessage)
{
synchronized(clientDataHash)
{
for(Enumerationenu=clientDataHash.elements();enu.hasMoreElements();)
{
DataOutputStreamoutData=(DataOutputStream)enu.nextElement();
try
{
outData.writeUTF(publicTalkMessage);
}
catch(IOExceptiones)
{
es.printStackTrace();
}
}
}
}
publicbooleanpeerTalk(StringpeerTalk,StringtalkMessage)
{
for(Enumerationenu=clientDataHash.keys();enu.hasMoreElements();)
{
SocketuserClient=(Socket)enu.nextElement();
if(peerTalk.equals((String)clientNameHash.get(userClient))&&!peerTalk.equals((String)clientNameHash.get(clientSocket)))
{
synchronized(clientDataHash)
{
DataOutputStreampeerOutData=(DataOutputStream)clientDataHash.get(userClient);
try
{
peerOutData.writeUTF(talkMessage);
}
catch(IOExceptiones)
{
es.printStackTrace();
}
}
Feedback(talkMessage);
return(false);
}
elseif(peerTalk.equals((String)clientNameHash.get(clientSocket)))
{
Feedback(talkMessage);
return(false);
}
}
return(true);
}
publicbooleanchessPeerTalk(StringchessPeerTalk,StringchessTalkMessage)
{
for(Enumerationenu=clientDataHash.keys();enu.hasMoreElements();)
{
SocketuserClient=(Socket)enu.nextElement();
if(chessPeerTalk.equals((String)clientNameHash.get(userClient))&&!chessPeerTalk.equals((String)clientNameHash.get(clientSocket)))
{
synchronized(clientDataHash)
{
DataOutputStreampeerOutData=(DataOutputStream)clientDataHash.get(userClient);
try
{
peerOutData.writeUTF(chessTalkMessage);
}
catch(IOExceptiones)
{
es.printStackTrace();
}
}
return(false);
}
}
return(true);
}
publicvoidFeedback(StringfeedbackString)
{
synchronized(clientDataHash)
{
DataOutputStreamoutData=(DataOutputStream)clientDataHash.get(clientSocket);
try
{
outData.writeUTF(feedbackString);
}
catch(Exceptioneb)
{
eb.printStackTrace();
}
}
}
publicStringgetUserList()
{
StringuserList="/userlist";
for(Enumerationenu=clientNameHash.elements();enu.hasMoreElements();)
{
userList=userList+""+(String)enu.nextElement();
}
return(userList);
}
publicObjectgetHashKey(HashtabletargetHash,ObjecthashValue)
{
ObjecthashKey;
for(Enumerationenu=targetHash.keys();enu.hasMoreElements();)
{
hashKey=(Object)enu.nextElement();
if(hashValue.equals((Object)targetHash.get(hashKey)))
return(hashKey);
}
return(null);
}
publicvoidfirstCome()
{
publicTalk(getUserList());
Feedback("/yourname"+(String)clientNameHash.get(clientSocket));
Feedback("Java五子棋谈天客户端");
Feedback("/changename<你的名字>--变动名字");
Feedback("/list--更新用户列表");
Feedback("/<用户名><要说的话>--私聊");
Feedback("注重:用命令的时分,先把发言的对象定为一切人");
}
publicvoidclientClose()
{
server.messageBoard.append("用户断开:"+clientSocket+"
");
//假如是游戏客户端主机
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(IOExceptionexx)
{
}
isClientClosed=true;
}
publicvoidrun()
{
DataInputStreaminData;
synchronized(clientDataHash)
{
server.statusLabel.setText("以后毗连数:"+clientDataHash.size());
}
try
{
inData=newDataInputStream(clientSocket.getInputStream());
firstCome();
while(true)
{
Stringmessage=inData.readUTF();
messageTransfer(message);
}
}
catch(IOExceptionesx)
{
}
finally
{
if(!isClientClosed)
{
clientClose();
}
}
}
}
publicclasschessServerextendsFrameimplementsActionListener
{
ButtonmessageClearButton=newButton("扫除显现");
ButtonserverStatusButton=newButton("服务器形态");
ButtonserverOffButton=newButton("封闭服务器");
PanelbuttonPanel=newPanel();
MessageServerserver=newMessageServer();
ServerSocketserverSocket;
HashtableclientDataHash=newHashtable(50);
HashtableclientNameHash=newHashtable(50);
HashtablechessPeerHash=newHashtable(50);
chessServer()
{
super("Java五子棋服务器");
setBackground(Color.pink);
buttonPanel.setLayout(newFlowLayout());
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(newWindowAdapter()
{
publicvoidwindowClosing(WindowEvente)
{
System.exit(0);
}
});
pack();
setVisible(true);
setSize(400,450);
setResizable(false);
validate();
try
{
makeMessageServer(4331,server);
}
catch(Exceptione)
{
System.out.println("e");
}
}
publicvoidmakeMessageServer(intport,MessageServerserver)throwsIOException
{
SocketclientSocket;
longclientAccessNumber=1;
this.server=server;
try
{
serverSocket=newServerSocket(port);
server.messageBoard.setText("服务器入手下手于:"+serverSocket.getInetAddress().getLocalHost()+":"+serverSocket.getLocalPort()+"
");
while(true)
{
clientSocket=serverSocket.accept();
server.messageBoard.append("用户毗连:"+clientSocket+"
");
DataOutputStreamoutData=newDataOutputStream(clientSocket.getOutputStream());
clientDataHash.put(clientSocket,outData);
clientNameHash.put(clientSocket,("新来客"+clientAccessNumber++));
ServerThreadthread=newServerThread(clientSocket,clientDataHash,clientNameHash,chessPeerHash,server);
thread.start();
}
}
catch(IOExceptionex)
{
System.out.println("已有服务器在运转.
");
}
}
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==messageClearButton)
{
server.messageBoard.setText("");
}
if(e.getSource()==serverStatusButton)
{
try
{
server.messageBoard.append("服务器信息:"+serverSocket.getInetAddress().getLocalHost()+":"+serverSocket.getLocalPort()+"
");
}
catch(Exceptionee)
{
System.out.println("serverSocket.getInetAddress().getLocalHost()error
");
}
}
if(e.getSource()==serverOffButton)
{
System.exit(0);
}
}
publicstaticvoidmain(Stringargs[])
{
chessServerchessServer=newchessServer();
}
}
对于一个大型项目,如果用java来作,可能需要9个月,并且可能需要翻阅10本以上的书,但如果用ruby来作,3个月,3本书就足够了,而.net也不过3,4本书足以,这就是区别。 |
|