五子棋游戏解读Word格式.docx
- 文档编号:18360457
- 上传时间:2022-12-15
- 格式:DOCX
- 页数:58
- 大小:24.99KB
五子棋游戏解读Word格式.docx
《五子棋游戏解读Word格式.docx》由会员分享,可在线阅读,更多相关《五子棋游戏解读Word格式.docx(58页珍藏版)》请在冰豆网上搜索。
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无法关闭"
/peer"
chessclient.chesspad.chessPeerName=recMessage.substring(6);
if(chessclient.isServer)
chessclient.chesspad.chessColor=1;
chessclient.chesspad.isMouseEnabled=true;
请黑棋下子"
elseif(chessclient.isClient)
chessclient.chesspad.chessColor=-1;
已加入游戏,等待对方下子..."
/youwin"
chessclient.isOnChess=false;
chessclient.chesspad.chessVictory(chessclient.chesspad.chessColor);
对方退出,请点放弃游戏退出连接"
chessclient.chesspad.isMouseEnabled=false;
/OK"
创建游戏成功,等待别人加入..."
/error"
chessclient.chatpad.chatLineArea.append("
传输错误:
请退出程序,重新加入\n"
else
chessclient.chatpad.chatLineArea.append(recMessage+"
\n"
chessclient.chatpad.chatLineArea.setCaretPosition(
chessclient.chatpad.chatLineArea.getText().length());
publicvoidrun()
Stringmessage="
;
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)
chatSocket.close();
catch(Exceptioned)
if(isOnChess||isGameConnected)
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
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:
无法连接,建议重新启动程序\n"
returnfalse;
publicvoidactionPerformed(ActionEvente)
if(e.getSource()==controlpad.connectButton)
host=chesspad.host=controlpad.inputIP.getText();
if(connectServer(host,port))
controlpad.connectButton.setEnabled(false);
controlpad.creatGameButton.setEnabled(true);
controlpad.joinGameButton.setEnabled(true);
chesspad.statusText.setText("
连接成功,请创建游戏或加入游戏"
catch(Exceptionei)
controlpad.connectButton:
if(e.getSource()==controlpad.exitGameButton)
if(e.getSource()==controlpad.joinGameButton)
StringselectedUser=userpad.userList.getSelectedItem();
if(selectedUser==null||selectedUser.startsWith("
)||
selectedUser.equals(chessClientName))
必须先选定一个有效用户"
if(!
isGameConnected)
if(chesspad.connectServer(chesspad.host,chesspad.port))
isGameConnected=true;
isOnChess=true;
isClient=true;
controlpad.cancelGameButton.setEnabled(true);
chesspad.chessthread.sendMessage("
/joingame"
+userpad.userList.getSelectedItem()+"
+chessClientName);
isGameConnected=false;
isOnChess=false;
isClient=false;
chesspad.connectServer无法连接\n"
+ee);
if(e.getSource()==controlpad.creatGameButton)
isServer=true;
/creatgame"
catch(Exceptionec)
isServer=false;
ec.printStackTrace();
+ec);
if(e.getSource()==controlpad.cancelGameButton)
if(isOnChess)
/giveup"
chesspad.chessVictory(-1*chesspad.chessColor);
请建立游戏或者加入游戏"
isOnChess)
isClient=isServer=false;
publicvoidkeyPressed(KeyEvente)
TextFieldinputwords=(TextField)e.getSource();
if(e.getKeyCode()==KeyEvent.VK_ENTER)
if(inputpad.userChoice.getSelectedItem().equals("
out.writeUTF(inputwords.getText());
inputwords.setText("
catch(Exceptionea)
KeyPressed无法连接,建议重新连接\n"
userpad.userList.removeAll();
inputpad.userChoice.removeAll();
controlpad.connectButton.setE
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 五子棋 游戏 解读