1、java 五子棋源代码chessClient.java:客户端主程序。chessInterface.java:客户端的界面。chessPad.java:棋盘的绘制。chessServer.java:服务器端。可同时容纳50个人同时在线下棋,聊天。没有加上详细注释,不过绝对可以运行,j2sdk1.4下通过。Java 培训/*1.chessClient.java*/import java.awt.*;import java.awt.event.*;import java.io.*;import .*;import java.util.*;class clientThread extends Thr
2、eadchessClient 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();chess
3、client.inputpad.userChoice.removeAll();chessclient.inputpad.userChoice.addItem(“所有人“);while(userToken.hasMoreTokens()String user=(String)userToken.nextToken(“ “);if(userNumber0 chessclient.inputpad.userChoice.addItem(user);userNumber+;chessclient.inputpad.userChoice.select(“所有人“);else if(recMessage.
4、startsWith(“/yourname “)chessclient.chessClientName=recMessage.substring(10);chessclient.setTitle(“Java 五子棋客户端 “+“用户名:“+chessclient.chessClientName);else if(recMessage.equals(“/reject“)trychessclient.chesspad.statusText.setText(“不能加入游戏“);chessclient.controlpad.cancelGameButton.setEnabled(false);ches
5、sclient.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(“
6、/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.
7、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“)
8、chessclient.chesspad.statusText.setText(“创建游戏成功,等待别人加入.“);else if(recMessage.equals(“/error“)chessclient.chatpad.chatLineArea.append(“传输错误:请退出程序,重新加入 n“);elsechessclient.chatpad.chatLineArea.append(recMessage+“n“);chessclient.chatpad.chatLineArea.setCaretPosition(chessclient.chatpad.chatLineArea.get
9、Text().length();public void run()String message=“;trywhile(true)message=chessclient.in.readUTF();acceptMessage(message);catch(IOException es)public class chessClient extends Frame implements ActionListener,KeyListeneruserPad userpad=new userPad();chatPad chatpad=new chatPad();controlPad controlpad=n
10、ew 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; /下棋的客户端连接?b
11、oolean 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.get
12、Text();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);cen
13、terPanel.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.exitGam
14、eButton.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
15、windowClosing(WindowEvent e)if(isOnChat)trychatSocket.close();catch(Exception ed)if(isOnChess | isGameConnected)trychesspad.chessSocket.close();catch(Exception ee)System.exit(0);public void windowActivated(WindowEvent ea);add(westPanel,BorderLayout.WEST);add(centerPanel,BorderLayout.CENTER);add(sout
16、hPanel,BorderLayout.SOUTH);pack();setSize(670,548);setVisible(true);setResizable(false);validate();public boolean connectServer(String serverIP,int serverPort) throws ExceptiontrychatSocket=new Socket(serverIP,serverPort);in=new DataInputStream(chatSocket.getInputStream();out=new DataOutputStream(ch
17、atSocket.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
18、.connectButton)host=chesspad.host=controlpad.inputIP.getText();tryif(connectServer(host,port)chatpad.chatLineArea.setText(“);controlpad.connectButton.setEnabled(false);controlpad.creatGameButton.setEnabled(true);controlpad.joinGameButton.setEnabled(true);chesspad.statusText.setText(“连接成功,请创建游戏或加入游戏
19、“);catch(Exception ei)chatpad.chatLineArea.setText(“controlpad.connectButton:无法连接,建议重新启动程序 n“);if(e.getSource()=controlpad.exitGameButton)if(isOnChat)trychatSocket.close();catch(Exception ed)if(isOnChess | isGameConnected)trychesspad.chessSocket.close();catch(Exception ee)System.exit(0);if(e.getSour
20、ce()=controlpad.joinGameButton)String selectedUser=userpad.userList.getSelectedItem();if(selectedUser=null | selectedUser.startsWith(“inchess“) |selectedUser.equals(chessClientName)chesspad.statusText.setText(“必须先选定一个有效用户 “);elsetryif(!isGameConnected)if(chesspad.connectServer(chesspad.host,chesspad
21、.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);elseisOn
22、Chess=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=fals
23、e;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)tryif(!isGameConnected)if
24、(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“+chessCl
25、ientName);elseisOnChess=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;isOnChes
26、s=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)
27、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)co
28、ntrolpad.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()=Key
29、Event.VK_ENTER)if(inputpad.userChoice.getSelectedItem().equals(“所有人 “)tryout.writeUTF(inputWords.getText();inputWords.setText(“);catch(Exception ea)chatpad.chatLineArea.setText(“chessClient:KeyPressed 无法连接,建议重新连接 n“);userpad.userList.removeAll();inputpad.userChoice.removeAll();inputWords.setText(“);
30、controlpad.connectButton.setEnabled(true);elsetryout.writeUTF(“/“+inputpad.userChoice.getSelectedItem()+“ “+inputWords.getText();inputWords.setText(“);catch(Exception ea)chatpad.chatLineArea.setText(“chessClient:KeyPressed 无法连接,建议重新连接 n“);userpad.userList.removeAll();inputpad.userChoice.removeAll();
31、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
32、 .*;class userPad extends PanelList userList=new List(10);userPad()setLayout(new BorderLayout();for(int i=0;i=1 chessOptNum+;ChessPaint(Integer.parseInt(chessOpt0),Integer.parseInt(chessOpt1),Integer.parseInt(chessOpt2);else if(recMessage.startsWith(“/yourname “)chesspad.chessSelfName=recMessage.sub
33、string(10);else if(recMessage.equals(“/error“)chesspad.statusText.setText(“错误:没有这个用户,请退出程序,重新加入“);else/System.out.println(recMessage);public void run()String message=“;trywhile(true)message=chesspad.inData.readUTF();acceptMessage(message);catch(IOException es)class chessPad extends Panel implements
34、MouseListener,ActionListenerint chessPoint_x=-1,chessPoint_y=-1,chessColor=1;int chessBlack_x=new int200;int chessBlack_y=new int200;int chessWhite_x=new int200;int chessWhite_y=new int200;int chessBlackCount=0,chessWhiteCount=0;int chessBlackWin=0,chessWhiteWin=0;boolean isMouseEnabled=false,isWin=
35、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(
36、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 ExceptiontrychessSocket=new Socket(ServerIP,ServerPort);inData=new DataInputStream(chessSocket.ge
37、tInputStream();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_xi=0
38、;chessBlack_yi=0;for(int i=0;i=chessWhiteCount;i+)chessWhite_xi=0;chessWhite_yi=0;chessBlackCount=0;chessWhiteCount=0;add(statusText);statusText.setBounds(40,5,360,24);if(chessColorWin=1) chessBlackWin+;statusText.setText(“黑棋胜,黑:白为“+chessBlackWin+“:“+chessWhiteWin+“,重新开局,等待白棋下子.“);else if(chessColor
39、Win=-1)chessWhiteWin+;statusText.setText(“白棋胜,黑:白为“+chessBlackWin+“:“+chessWhiteWin+“,重新开局,等待黑棋下子.“);public void getLocation(int a,int b,int color)if(color=1)chessBlack_xchessBlackCount=a*20;chessBlack_ychessBlackCount=b*20;chessBlackCount+;else if(color=-1)chessWhite_xchessWhiteCount=a*20;chessWhit
40、e_ychessWhiteCount=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_xchessCompare)
41、if(chessLink=5)return(true);if(chessLink=(chessLinkTest+1)chessLinkTest+;elsebreak;for(step=1;step=4;step+)for(chessCompare=0;chessCompare=chessBlackCount;chessCompare+)if(a-step)*20=chessBlack_xchessCompare) if(chessLink=5)return(true);if(chessLink=(chessLinkTest+1)chessLinkTest+;elsebreak;chessLin
42、k=1;chessLinkTest=1;for(step=1;step=4;step+)for(chessCompare=0;chessCompare=chessBlackCount;chessCompare+)if(a*20=chessBlack_xchessCompare) if(chessLink=5)return(true);if(chessLink=(chessLinkTest+1)chessLinkTest+;elsebreak;for(step=1;step=4;step+)for(chessCompare=0;chessCompare=chessBlackCount;chess
43、Compare+)if(a*20=chessBlack_xchessCompare) if(chessLink=5)return(true);if(chessLink=(chessLinkTest+1)chessLinkTest+;elsebreak;chessLink=1;chessLinkTest=1;for(step=1;step=4;step+)for(chessCompare=0;chessCompare=chessBlackCount;chessCompare+)if(a-step)*20=chessBlack_xchessCompare) if(chessLink=5)retur
44、n(true);if(chessLink=(chessLinkTest+1)chessLinkTest+;elsebreak;for(step=1;step=4;step+)for(chessCompare=0;chessCompare=chessBlackCount;chessCompare+)if(a+step)*20=chessBlack_xchessCompare) if(chessLink=5)return(true);if(chessLink=(chessLinkTest+1)chessLinkTest+;elsebreak;chessLink=1;chessLinkTest=1;
45、for(step=1;step=4;step+)for(chessCompare=0;chessCompare=chessBlackCount;chessCompare+)if(a+step)*20=chessBlack_xchessCompare) if(chessLink=5)return(true);if(chessLink=(chessLinkTest+1)chessLinkTest+;elsebreak;for(step=1;step=4;step+)for(chessCompare=0;chessCompare=chessBlackCount;chessCompare+)if(a-step)*20=chessBlack_xchessCompare) if(chessLink=5)return(true);Java 游戏源代码 详见 上海青大实训 java