您当前的位置:首页 > 互联网教程

急求Java五子棋代码。。。要绝对的原创(可以加分)

发布时间:2025-05-20 08:56:26    发布人:远客网络

急求Java五子棋代码。。。要绝对的原创(可以加分)

一、急求Java五子棋代码。。。要绝对的原创(可以加分)

chessClient.java:客户端主程序。

chessInterface.java:客户端的界面。

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

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

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

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

class clientThread extends Thread

clientThread(chessClient chessclient)

public void acceptMessage(String recMessage)

if(recMessage.startsWith("/userlist"))

StringTokenizer userToken=new StringTokenizer(recMessage,"");

chessclient.userpad.userList.removeAll();

chessclient.inputpad.userChoice.removeAll();

chessclient.inputpad.userChoice.addItem("所有人");

while(userToken.hasMoreTokens())

String user=(String)userToken.nextToken("");

if(userNumber>0&&!user.startsWith("[inchess]"))

chessclient.userpad.userList.add(user);

chessclient.inputpad.userChoice.addItem(user);

chessclient.inputpad.userChoice.select("所有人");

else if(recMessage.startsWith("/yourname"))

chessclient.chessClientName=recMessage.substring(10);

chessclient.setTitle("Java五子棋客户端"+"用户名:"+chessclient.chessClientName);

else if(recMessage.equals("/reject"))

chessclient.chesspad.statusText.setText("不能加入游戏");

chessclient.controlpad.cancelGameButton.setEnabled(false);

chessclient.controlpad.joinGameButton.setEnabled(true);

chessclient.controlpad.creatGameButton.setEnabled(true);

chessclient.chatpad.chatLineArea.setText("chessclient.chesspad.chessSocket.close无法关闭");

chessclient.controlpad.joinGameButton.setEnabled(true);

else if(recMessage.startsWith("/peer"))

chessclient.chesspad.chessPeerName=recMessage.substring(6);

chessclient.chesspad.chessColor=1;

chessclient.chesspad.isMouseEnabled=true;

chessclient.chesspad.statusText.setText("请黑棋下子");

chessclient.chesspad.chessColor=-1;

chessclient.chesspad.statusText.setText("已加入游戏,等待对方下子...");

else if(recMessage.equals("/youwin"))

chessclient.chesspad.chessVictory(chessclient.chesspad.chessColor);

chessclient.chesspad.statusText.setText("对方退出,请点放弃游戏退出连接");

chessclient.chesspad.isMouseEnabled=false;

else if(recMessage.equals("/OK"))

chessclient.chesspad.statusText.setText("创建游戏成功,等待别人加入...");

else if(recMessage.equals("/error"))

chessclient.chatpad.chatLineArea.append("传输错误:请退出程序,重新加入\n");

chessclient.chatpad.chatLineArea.append(recMessage+"\n");

chessclient.chatpad.chatLineArea.setCaretPosition(

chessclient.chatpad.chatLineArea.getText().length());

message=chessclient.in.readUTF();

public class chessClient extends Frame implements ActionListener,KeyListener

userPad userpad=new userPad();

chatPad chatpad=new chatPad();

controlPad controlpad=new controlPad();

chessPad chesspad=new chessPad();

inputPad inputpad=new inputPad();

boolean isOnChat=false;//在聊天?

boolean isOnChess=false;//在下棋?

boolean isGameConnected=false;//下棋的客户端连接?

boolean isServer=false;//如果是下棋的主机

boolean isClient=false;//如果是下棋的客户端

Panel centerPanel=new Panel();

setLayout(new BorderLayout());

host=controlpad.inputIP.getText();

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);

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(new WindowAdapter()

public void windowClosing(WindowEvent e)

if(isOnChess|| isGameConnected)

public void windowActivated(WindowEvent ea)

add(westPanel,BorderLayout.WEST);

add(centerPanel,BorderLayout.CENTER);

add(southPanel,BorderLayout.SOUTH);

public boolean connectServer(String serverIP,int serverPort) throws Exception

chatSocket=new Socket(serverIP,serverPort);

in=new DataInputStream(chatSocket.getInputStream());

out=new DataOutputStream(chatSocket.getOutputStream());

clientThread clientthread=new clientThread(this);

chatpad.chatLineArea.setText("chessClient:connectServer:无法连接,建议重新启动程序\n");

public void actionPerformed(ActionEvent e)

if(e.getSource()==controlpad.connectButton)

host=chesspad.host=controlpad.inputIP.getText();

chatpad.chatLineArea.setText("");

controlpad.connectButton.setEnabled(false);

controlpad.creatGameButton.setEnabled(true);

controlpad.joinGameButton.setEnabled(true);

chesspad.statusText.setText("连接成功,请创建游戏或加入游戏");

chatpad.chatLineArea.setText("controlpad.connectButton:无法连接,建议重新启动程序\n");

if(e.getSource()==controlpad.exitGameButton)

if(isOnChess|| isGameConnected)

if(e.getSource()==controlpad.joinGameButton)

String selectedUser=userpad.userList.getSelectedItem();

if(selectedUser==null|| selectedUser.startsWith("[inchess]")||

selectedUser.equals(chessClientName))

chesspad.statusText.setText("必须先选定一个有效用户");

if(chesspad.connectServer(chesspad.host,chesspad.port))

controlpad.creatGameButton.setEnabled(false);

controlpad.joinGameButton.setEnabled(false);

controlpad.cancelGameButton.setEnabled(true);

chesspad.chessthread.sendMessage("/joingame"+userpad.userList.getSelectedItem()+""+chessClientName);

controlpad.creatGameButton.setEnabled(false);

controlpad.joinGameButton.setEnabled(false);

controlpad.cancelGameButton.setEnabled(true);

chesspad.chessthread.sendMessage("/joingame"+userpad.userList.getSelectedItem()+""+chessClientName);

controlpad.creatGameButton.setEnabled(true);

controlpad.joinGameButton.setEnabled(true);

controlpad.cancelGameButton.setEnabled(false);

chatpad.chatLineArea.setText("chesspad.connectServer无法连接\n"+ee);

if(e.getSource()==controlpad.creatGameButton)

if(chesspad.connectServer(chesspad.host,chesspad.port))

controlpad.creatGameButton.setEnabled(false);

controlpad.joinGameButton.setEnabled(false);

controlpad.cancelGameButton.setEnabled(true);

chesspad.chessthread.sendMessage("/creatgame"+"[inchess]"+chessClientName);

controlpad.creatGameButton.setEnabled(false);

controlpad.joinGameButton.setEnabled(false);

controlpad.cancelGameButton.setEnabled(true);

chesspad.chessthread.sendMessage("/creatgame"+"[inchess]"+chessClientName);

controlpad.creatGameButton.setEnabled(true);

controlpad.joinGameButton.setEnabled(true);

controlpad.cancelGameButton.setEnabled(false);

chatpad.chatLineArea.setText("chesspad.connectServer无法连接\n"+ec);

if(e.getSource()==controlpad.cancelGameButton)

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("请建立游戏或者加入游戏");

controlpad.creatGameButton.setEnabled(true);

controlpad.joinGameButton.setEnabled(true);

controlpad.cancelGameButton.setEnabled(false);

chesspad.statusText.setText("请建立游戏或者加入游戏");

public void keyPressed(KeyEvent e)

TextField inputWords=(TextField)e.getSource();

if(e.getKeyCode()==KeyEvent.VK_ENTER)

if(inputpad.userChoice.getSelectedItem().equals("所有人"))

out.writeUTF(inputWords.getText());

chatpad.chatLineArea.setText("chessClient:KeyPressed无法连接,建议重新连接\n");

inputpad.userChoice.removeAll();

controlpad.connectButton.setEnabled(true);

out.writeUTF("/"+inputpad.userChoice.getSelectedItem()+""+inputWords.getText());

chatpad.chatLineArea.setText("chessClient:KeyPressed无法连接,建议重新连接\n");

inputpad.userChoice.removeAll();

controlpad.connectButton.setEnabled(true);

public void keyTyped(KeyEvent e)

public void keyReleased(KeyEvent e)

public static void main(String args[])

chessClient chessClient=new chessClient();

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

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

setLayout(new BorderLayout());

userList.add(i+"."+"没有用户");

add(userList,BorderLayout.CENTER);

TextArea chatLineArea=new TextArea("",18,30,TextArea.SCROLLBARS_VERTICAL_ONLY);

setLayout(new BorderLayout());

add(chatLineArea,BorderLayout.CENTER);

class controlPad extends Panel

Label IPlabel=new Label("IP",Label.LEFT);

TextField inputIP=new TextField("localhost",10);

Button connectButton=new Button("连接主机");

Button creatGameButton=new Button("建立游戏");

Button joinGameButton=new Button("加入游戏");

Button cancelGameButton=new Button("放弃游戏");

Button exitGameButton=new Button("关闭程序");

setLayout(new FlowLayout(FlowLayout.LEFT));

TextField inputWords=new TextField("",40);

Choice userChoice=new Choice();

setLayout(new FlowLayout(FlowLayout.LEFT));

userChoice.addItem(i+"."+"没有用户");

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

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

class chessThread extends Thread

chessThread(chessPad chesspad)

public void sendMessage(String sndMessage)

chesspad.outData.writeUTF(sndMessage);

System.out.println("chessThread.sendMessage:"+ea);

public void acceptMessage(String recMessage)

if(recMessage.startsWith("/chess"))

StringTokenizer userToken=new StringTokenizer(recMessage,"");

String[] chessOpt={"-1","-1","0"};

while(userToken.hasMoreTokens())

chessToken=(String)userToken.nextToken("");

if(chessOptNum>=1&& chessOptNum<=3)

chessOpt[chessOptNum-1]=chessToken;

chesspad.netChessPaint(Integer.parseInt(chessOpt[0]),Integer.parseInt(chessOpt[1]),Integer.parseInt(chessOpt[2]));

else if(recMessage.startsWith("/yourname"))

chesspad.chessSelfName=recMessage.substring(10);

else if(recMessage.equals("/error"))

chesspad.statusText.setText("错误:没有这个用户,请退出程序,重新加入");

//System.out.println(recMessage);

message=chesspad.inData.readUTF();

class chessPad extends Panel implements MouseListener,ActionListener

int chessPoint_x=-1,chessPoint_y=-1,chessColor=1;

int chessBlack_x[]=new int[200];

int chessBlack_y[]=new int[200];

int chessWhite_x[]=new int[200];

int chessWhite_y[]=new int[200];

int chessBlackCount=0,chessWhiteCount=0;

int chessBlackWin=0,chessWhiteWin=0;

boolean isMouseEnabled=false,isWin=false,isInGame=false;

TextField statusText=new TextField("请先连接服务器");

chessThread chessthread=new chessThread(this);

statusText.setBounds(40,5,360,24);

statusText.setEditable(false);

public boolean connectServer(String ServerIP,int ServerPort) throws Exception

chessSocket=new Socket(ServerIP,ServerPort);

inData=new DataInputStream(chessSocket.getInputStream());

outData=new DataOutputStream(chessSocket.getOutputStream());

statusText.setText("chessPad:connectServer:无法连接\n");

public void chessVictory(int chessColorWin)

for(int i=0;i<=chessBlackCount;i++)

for(int i=0;i<=chessWhiteCount;i++)

statusText.setBounds(40,5,360,24);

statusText.setText("黑棋胜,黑:白为"+chessBlackWin+":"+chessWhiteWin+",重新开局,等待白棋下子...");

statusText.setText("白棋胜,黑:白为"+chessBlackWin+":"+chessWhiteWin+",重新开局,等待黑棋下子...");

public void getLocation(int a,int b,int color)

chessBlack_x[chessBlackCount]=a*20;

chessBlack_y[chessBlackCount]=b*20;

chessWhite_x[chessWhiteCount]=a*20;

chessWhite_y[chessWhiteCount]=b*20;

public boolean checkWin(int a,int b,int checkColor)

int step=1,chessLink=1,chessLinkTest=1,chessCompare=0;

for(chessCompare=0;chessCompare<=chessBlackCount;chessCompare++)

if(((a+step)*20==chessBlack_x[chessCompare])&&((b*20)==chessBlack_y[chessCompare]))

if(chessLink==(chessLinkTest+1))

for(chessCompare=0;chessCompare<=chessBlackCount;chessCompare++)

if(((a-step)*20==chessBlack_x[chessCompare])&&(b*20==chessBlack_y[chessCompare]))

if(chessLink==(chessLinkTest+1))

for(chessCompare=0;chessCompare<=chessBlackCount;chessCompare++)

if((a*20==chessBlack_x[chessCompare])&&((b+step)*20==chessBlack_y[chessCompare]))

if(chessLink==(chessLinkTest+1))

for(chessCompare=0;chessCompare<=chessBlackCount;chessCompare++)

if((a*20==chessBlack_x[chessCompare])&&((b-step)*20==chessBlack_y[chessCompare]))

if(chessLink==(chessLinkTest+1))

for(chessCompare=0;chessCompare<=chessBlackCount;chessCompare++)

if(((a-step)*20==chessBlack_x[chessCompare])&&((b+step)*20==chessBlack_y[chessCompare]))

if(chessLink==(chessLinkTest+1))

for(chessCompare=0;chessCompare<=chessBlackCount;chessCompare++)

if(((a+step)*20==chessBlack_x[chessCompare])&&((b-step)*20==chessBlack_y[chessCompare]))

if(chessLink==(chessLinkTest+1))

for(chessCompare=0;chessCompare<=chessBlackCount;chessCompare++)

if(((a+step)*20==chessBlack_x[chessCompare])&&((b+step)*20==chessBlack_y[chessCompare]))

if(chessLink==(chessLinkTest+1))

for(chessCompare=0;chessCompare<=chessBlackCount;chessCompare++)

if(((a-step)*20==chessBlack_x[chessCompare])&&((b-step)*20==chessBlack_y[chessCompare]))

二、五子棋获胜表,编程怎么写

1、在一场五子棋的游戏中,计算机必须要知道有那些的获胜组合,因此我们必须求得获胜组合的总数。我们假定当前的棋盘为10*10。

2、(1),计算水平方向的获胜组合数,每一列的获胜组合是:6,共10列,所以水平方向的获胜组合数为:6*10=60。

3、(2),计算垂直方向的获胜组合总数,每一行的获胜组合是:6,共10行,则垂直方向的获胜组合数为:6*10=60。

4、(3),计算正对角线方向的获胜组合总数,正对角线上的获胜组合总数为6+(5+4+3+2+1)*2=36。

5、(4),计算反对角线方向的获胜组合总数,反对角线上的获胜组合总数为6+(5+4+3+2+1)*2=36。

6、这样所有的获胜组合数为:60+60+36+36=192。

三、五子棋棋盘怎么用C++实现呢

1、在C++中实现五子棋棋盘,通常不需要手动绘制背景,而是通过代码生成棋盘的边界和坐标。比如,我们可以使用简单的循环结构来打印棋盘的线条。下面是一个简单的例子:

2、这个代码片段展示了如何通过简单的循环来生成棋盘。它打印了一个简单的线条表示棋盘边界,但实际上,这样的实现过于简化,并不适合实际的五子棋游戏。在实际开发中,我们通常会使用图形库来生成更复杂的图形界面。

3、对于五子棋,处理棋盘上的坐标位置是非常重要的。你可能需要创建一个二维数组来表示棋盘上的每个点,通过这些点来记录玩家的棋子位置。同时,还需要实现判断胜利的逻辑,比如检测是否有五子连珠。

4、当然,这只是一个基础的实现方式。你可以根据具体需求进一步扩展功能,比如增加棋子的移动、判断胜负等更复杂的逻辑。

5、在实际应用中,你可以通过类似的方式生成棋盘,并结合图形库(如SDL、SFML等)来绘制更美观的界面。这样的实现不仅能够展示你的编程技能,还能让你更好地理解游戏开发的基本原理。

6、总之,实现五子棋棋盘的关键在于正确地生成棋盘边界和处理棋盘上的坐标位置。希望这个简单的示例能为你提供一些启示。



相关内容FAQs: