java五子棋报告全程(完整版)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

下面的源代码分为4个文件;(1)chessClient.java:客户端主程序。(2)chessInterface.java:客户端的界面。(3)chessPad.java:棋盘的绘制。(4)chessServer.java:服务器端。可同时容纳50个人同时在线下棋,聊天。/*********************************************************************************************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(userNumber0&&!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(传输错误:请退出程序,重新加入\n);}else{chessclient.chatpad.chatLineArea.append(recMessage+\n);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.getOutput

1 / 44
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功