Java五子棋网上对战代码.docx

上传人:b****2 文档编号:1952099 上传时间:2023-05-02 格式:DOCX 页数:32 大小:19.29KB
下载 相关 举报
Java五子棋网上对战代码.docx_第1页
第1页 / 共32页
Java五子棋网上对战代码.docx_第2页
第2页 / 共32页
Java五子棋网上对战代码.docx_第3页
第3页 / 共32页
Java五子棋网上对战代码.docx_第4页
第4页 / 共32页
Java五子棋网上对战代码.docx_第5页
第5页 / 共32页
Java五子棋网上对战代码.docx_第6页
第6页 / 共32页
Java五子棋网上对战代码.docx_第7页
第7页 / 共32页
Java五子棋网上对战代码.docx_第8页
第8页 / 共32页
Java五子棋网上对战代码.docx_第9页
第9页 / 共32页
Java五子棋网上对战代码.docx_第10页
第10页 / 共32页
Java五子棋网上对战代码.docx_第11页
第11页 / 共32页
Java五子棋网上对战代码.docx_第12页
第12页 / 共32页
Java五子棋网上对战代码.docx_第13页
第13页 / 共32页
Java五子棋网上对战代码.docx_第14页
第14页 / 共32页
Java五子棋网上对战代码.docx_第15页
第15页 / 共32页
Java五子棋网上对战代码.docx_第16页
第16页 / 共32页
Java五子棋网上对战代码.docx_第17页
第17页 / 共32页
Java五子棋网上对战代码.docx_第18页
第18页 / 共32页
Java五子棋网上对战代码.docx_第19页
第19页 / 共32页
Java五子棋网上对战代码.docx_第20页
第20页 / 共32页
亲,该文档总共32页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

Java五子棋网上对战代码.docx

《Java五子棋网上对战代码.docx》由会员分享,可在线阅读,更多相关《Java五子棋网上对战代码.docx(32页珍藏版)》请在冰点文库上搜索。

Java五子棋网上对战代码.docx

Java五子棋网上对战代码

chessClient.java:

客户端主程序。

chessInterface.java:

客户端的界面。

chessPad.java:

棋盘的绘制。

chessServer.java:

服务器端。

可同时容纳50个人同时在线下棋,聊天。

没有加上详细注释,不过绝对可以运行,j2sdk1.4下通过。

/*********************************************************************************************

1.chessClient.java

**********************************************************************************************/

importjava.awt.*;

importjava.awt.event.*;

importjava.io.*;

import.*;

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("传输错误:

请退出程序,重新加入\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.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();

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:

无法连接,建议重新启动程序\n");

}

}

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无法连接\n"+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无法连接\n"+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.userChoic

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2