java 网络五子棋_Java实现五子棋网络版

本文实例为大家分享了Java实现五子棋网络版的具体代码,供大家参考,具体内容如下

需求分析:

对于网络五子棋而言,在普通五子棋的基础上需要添加以下功能:

1.拥有服务器端和客户端,用户通过客户端登录服务器后可与其他登录的用户进行对弈

2.服务器支持多组用户同时进行对弈

3.用户可以在服务器上创建新游戏或加入已创建的游戏

4.用户在下棋的时候可以进行聊天交流

由上可以知道需要实现的功能:

·提供服务器和客户端的功能

·服务器将监听客户端的登录情况并允许多个客户端进行登录

·用户通过客户端可以登录服务器,之后可以看到服务器当前在线的其他用户,并与他们进行聊天等

·用户登录服务器后,可以创建新的五子棋游戏或加入已创建的五子棋游戏

·用户通过客户端可以像普通五子棋那样与其他用户对弈

根据功能将网络五子棋分为4个模块:即用户面板模块、棋盘面板模块、五子棋服务器模块、五子棋客户端模块

a3b87a76a38770541d0bbf58a993978d.png

下面我们开始进行编译用户面板模块:

1.开发用户列表面板

import java.awt.*;

/**

* Created by Administrator on 2016/11/21.

*/

//初始状态下将添加10个名称为“无用户“的信息到列表中,说明服务器最多支持10个用户同时在线

//该列表被添加到面板中,使用“BorderLayout”布局格式

public class UserListPad extends Panel{

public List userList=new List(10);

public UserListPad(){

setLayout(new BorderLayout());

for(int i=0;i<10;i++){

userList.add(i+"."+"无用户");

}

add(userList,BorderLayout.CENTER);

}

}

2.开发用户聊天面板

import javax.swing.*;

import java.awt.*;

/**

* Created by Administrator on 2016/11/21.

*/

//聊天面板为一个TextArea视图控件,拥有一个垂直方向的滚动条。

//该TextArea被添加到面板中,使用“BorderLayout”布局格式。

public class UserChatPad extends JPanel{

public JTextArea chatTextArea=new JTextArea("命令区域",18,20);

public UserChatPad(){

setLayout(new BorderLayout());

chatTextArea.setAutoscrolls(true);

chatTextArea.setLineWrap(true);

add(chatTextArea,BorderLayout.CENTER);

}

}

3.开发用户输入面板

import javax.swing.*;

import java.awt.*;

/**

* Created by Administrator on 2016/11/21.

*/

//面板包含两个视图控件

//contentInpitted为TextField控件,用户可以在其中输入聊天信息

public class UserInputPad extends JPanel{

public JTextField contentInputted = new JTextField("",26);

public JComboBox userChoice = new JComboBox();

public UserInputPad(){

setLayout(new FlowLayout(FlowLayout.LEFT));

for(int i=0;i<50;i++){

userChoice.addItem(i+"."+"无用户");

}

userChoice.setSize(60,24);

add(userChoice);

add(contentInputted);

}

}

4.开发用户操作面板

import javax.swing.*;

import java.awt.*;

/**

* Created by Administrator on 2016/11/21.

*/

public class UserControlPad extends JPanel {

public JLabel ipLabel = new JLabel("IP",JLabel.LEFT);

public JTextField ipInputted = new JTextField("localhost",10);

public JButton connectButton = new JButton("连接到服务器");

public JButton createButton = new JButton("创建游戏");

public JButton joinButton = new JButton("加入游戏");

public JButton cancelButton = new JButton("放弃游戏");

public JButton exitButton = new JButton("退出游戏");

public UserControlPad(){

setLayout(new FlowLayout(FlowLayout.LEFT));

setBackground(Color.LIGHT_GRAY);

add(ipLabel);

add(ipInputted);

add(connectButton);

add(createButton);

add(joinButton);

add(cancelButton);

add(exitButton);

}

}

下面开始开发棋盘面板模块

1.开发黑棋类

import java.awt.*;

/**

* Created by Administrator on 2016/11/21.

*/

public class FIRPointBlack extends Canvas {

FIRPad padBelonged; // 黑棋所属的棋盘

public FIRPointBlack(FIRPad padBelonged)

{

setSize(20, 20); // 设置棋子大小

this.padBelonged = padBelonged;

}

public void paint(Graphics g)

{ // 画棋子

g.setColor(Color.black);

g.fillOval(0, 0, 14, 14);

}

}

2.开发白棋类

import java.awt.*;

/**

* Created by Administrator on 2016/11/21.

*/

public class FIRPointWhite extends Canvas{

FIRPad padBelonged; // 白棋所属的棋盘

public FIRPointWhite(FIRPad padBelonged)

{

setSize(20, 20);

this.padBelonged = padBelonged;

}

public void paint(Graphics g)

{ // 画棋子

g.setColor(Color.white);

g.fillOval(0, 0, 14, 14);

}

}

3.开发棋盘面板

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.net.*;

import javax.swing.JTextField;

/**

* Created by Administrator on 2016/11/21.

*/

public class FIRPad extends Panel implements MouseListener,ActionListener{

// 鼠标是否能使用

public boolean isMouseEnabled = false;

// 是否胜利

public boolean isWinned = false;

// 是否在下棋中

public boolean isGaming = false;

// 棋子的x轴坐标位

public int chessX_POS = -1;

// 棋子的y轴坐标位

public int chessY_POS = -1;

// 棋子的颜色

public int chessColor = 1;

// 黑棋x轴坐标位数组

public int chessBlack_XPOS[] = new int[200];

// 黑棋y轴坐标位数组

public int chessBlack_YPOS[] = new int[200];

// 白棋x轴坐标位数组

public int chessWhite_XPOS[] = new int[200];

// 白棋y轴坐标位数组

public int chessWhite_YPOS[] = new int[200];

// 黑棋数量

public int chessBlackCount = 0;

// 白棋数量

public int chessWhiteCount = 0;

// 黑棋获胜次数

public int chessBlackVicTimes = 0;

// 白棋获胜次数

public int chessWhiteVicTimes = 0;

// 套接口

public Socket chessSocket;

public DataInputStream inputData;

public DataOutputStream outputData;

public String chessSelfName = null;

public String chessPeerName = null;

public String host = null;

public int port = 4331;

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

public FIRThread firThread = new FIRThread(this);

public FIRPad()

{

setSize(440, 440);

setLayout(null);

setBackground(Color.LIGHT_GRAY);

addMouseListener(this);

add(statusText);

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

statusText.setEditable(false);

}

// 连接到主机

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

{

try

{

// 取得主机端口

chessSocket = new Socket(ServerIP, ServerPort);

// 取得输入流

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

// 取得输出流

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

firThread.start();

return true;

}

catch (IOException ex)

{

statusText.setText("连接失败! \n");

}

return false;

}

// 设定胜利时的棋盘状态

public void setVicStatus(int vicChessColor)

{

// 清空棋盘

this.removeAll();

// 将黑棋的位置设置到零点

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

{

chessBlack_XPOS[i] = 0;

chessBlack_YPOS[i] = 0;

}

// 将白棋的位置设置到零点

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

{

chessWhite_XPOS[i] = 0;

chessWhite_YPOS[i] = 0;

}

// 清空棋盘上的黑棋数

chessBlackCount = 0;

// 清空棋盘上的白棋数

chessWhiteCount = 0;

add(statusText);

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

if (vicChessColor == 1)

{ // 黑棋胜

chessBlackVicTimes++;

statusText.setText("黑方胜,黑:白 " + chessBlackVicTimes + ":" + chessWhiteVicTimes

+ ",游戏重启,等待白方...");

}

else if (vicChessColor == -1)

{ // 白棋胜

chessWhiteVicTimes++;

statusText.setText("白方胜,黑:白 " + chessBlackVicTimes + ":" + chessWhiteVicTimes

+ ",游戏重启,等待黑方...");

}

}

// 取得指定棋子的位置

public void setLocation(int xPos, int yPos, int chessColor)

{

if (chessColor == 1)

{ // 棋子为黑棋时

chessBlack_XPOS[chessBlackCount] = xPos * 20;

chessBlack_YPOS[chessBlackCount] = yPos * 20;

chessBlackCount++;

}

else if (chessColor == -1)

{ // 棋子为白棋时

chessWhite_XPOS[chessWhiteCount] = xPos * 20;

chessWhite_YPOS[chessWhiteCount] = yPos * 20;

chessWhiteCount++;

}

}

// 判断当前状态是否为胜利状态

public boolean checkVicStatus(int xPos, int yPos, int chessColor)

{

int chessLinkedCount = 1; // 连接棋子数

int chessLinkedCompare = 1; // 用于比较是否要继续遍历一个棋子的相邻网格

int chessToCompareIndex = 0; // 要比较的棋子在数组中的索引位置

int closeGrid = 1; // 相邻网格的位置

if (chessColor == 1)

{ // 黑棋时

chessLinkedCount = 1; // 将该棋子自身算入的话,初始连接数为1

//以下每对for循环语句为一组,因为下期的位置能位于中间而非两端

for (closeGrid = 1; closeGrid <= 4; closeGrid++)

{ // 遍历相邻4个网格

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

{ // 遍历棋盘上所有黑棋子

if (((xPos + closeGrid) * 20 == chessBlack_XPOS[chessToCompareIndex])

&& ((yPos * 20) == chessBlack_YPOS[chessToCompareIndex]))

{ // 判断当前下的棋子的右边4个棋子是否都为黑棋

chessLinkedCount = chessLinkedCount + 1; // 连接数加1

if (chessLinkedCount == 5)

{ // 五子相连时,胜利

return true;

}

}

}

if (chessLinkedCount == (chessLinkedCompare + 1)) {

chessLinkedCompare++;

}

else {// 若中间有一个棋子非黑棋,则会进入此分支,此时无需再遍历

break;

}

}

for (closeGrid = 1; closeGrid <= 4; closeGrid++)

{

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

{

if (((xPos - closeGrid) * 20 == chessBlack_XPOS[chessToCompareIndex])

&& (yPos * 20 == chessBlack_YPOS[chessToCompareIndex]))

{ // 判断当前下的棋子的左边4个棋子是否都为黑棋

chessLinkedCount++;

if (chessLinkedCount == 5)

{

return true;

}

}

}

if (chessLinkedCount == (chessLinkedCompare + 1)) {

chessLinkedCompare++;

}

else {

break;

}

}

// 进入新的一组for循环时要将连接数等重置

chessLinkedCount = 1;

chessLinkedCompare = 1;

for (closeGrid = 1; closeGrid <= 4; closeGrid++)

{

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

{

if ((xPos * 20 == chessBlack_XPOS[chessToCompareIndex])

&& ((yPos + closeGrid) * 20 == chessBlack_YPOS[chessToCompareIndex]))

{ // 判断当前下的棋子的上边4个棋子是否都为黑棋

chessLinkedCount++;

if (chessLinkedCount == 5)

{

return true;

}

}

}

if (chessLinkedCount == (chessLinkedCompare + 1)) {

chessLinkedCompare++;

}

else {

break;

}

}

for (closeGrid = 1; closeGrid <= 4; closeGrid++)

{

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

{

if ((xPos * 20 == chessBlack_XPOS[chessToCompareIndex])

&& ((yPos - closeGrid) * 20 == chessBlack_YPOS[chessToCompareIndex]))

{ // 判断当前下的棋子的下边4个棋子是否都为黑棋

chessLinkedCount++;

if (chessLinkedCount == 5)

<
  • 0
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值