1.项目简介

本次课程设计我们以五子棋这一有趣、益智棋类游戏为题,使用 java swing、socket 实现了可联机的五子棋对战游戏。

2.个人任务

棋盘多线程、五子棋算法、部分GUI的实现

3.功能详解

(1)棋子类定义

/**
 * 白棋
 **/

public class ChessWhite extends Canvas{

    /**
     * 白棋所在棋盘
     **/
    ChessBoard chessBoard;

    public ChessWhite (ChessBoard chessBoard) {
        this.chessBoard = chessBoard;
    }

    public void paint(Graphics g) {
        //画棋子
        g.setColor(Color.WHITE);
        g.fillOval(0,0,30,30);
    }
}
/**
 * 黑棋
 **/

public class ChessBlack extends Canvas {

    /**
     * 黑棋所在棋盘
     **/
    ChessBoard chessBoard;

    public ChessBlack (ChessBoard chessBoard) {
        this.chessBoard = chessBoard;
    }

    @Override
    public void paint(Graphics g){
        //画棋子
        g.setColor(Color.BLACK);
        g.fillOval(0,0,30,30);
    }
}

(2)五子棋算法实现

/**
    * 五子棋核心算法
    * */
    public boolean checkVicStatus(int xPos, int yPos, int chessColor) {
        //连接的棋子数
        int chessLinkedCount;
        //用于比较是否要继续遍历一个棋子的相邻网格
        int chessLinkedCompare = 1;
        //要比较的棋子在数组中的索引位置
        int chessToCompareIndex;
        //相邻网格的位置
        int closeGrid;

        //当落子为黑棋时
        if(chessColor == 1) {
            //将该棋子自身算入的话,初始连接数为1
            chessLinkedCount = 1;
            //以下每对for循环语句为一组,因为下棋的位置能位于中间而非两端

            //遍历相邻4个网格
            //判断当前下的棋子的右边4个棋子是否都为黑棋
            for(closeGrid = 1; closeGrid <= 4; closeGrid++) {
                //遍历棋盘上所有黑棋子
                for(chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++) {
                    if(((xPos + closeGrid) * dis == chessBlack_XPOS[chessToCompareIndex])
                            && ((yPos * dis) == chessBlack_YPOS[chessToCompareIndex])) {
                        //连接数加1
                        chessLinkedCount = chessLinkedCount + 1;
                        //五子相连时,胜利
                        if (chessLinkedCount == 5) {
                            return true;
                        }
                    }
                }
                if(chessLinkedCount == (chessLinkedCompare + 1)) {
                    chessLinkedCompare++;
                    //若中间有一个棋子非黑棋,则会进入此分支,此时无需再遍历
                } else {
                    break;
                }
            }
            // 判断当前下的棋子的左边4个棋子是否都为黑棋
            for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
                for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++) {
                    if (((xPos - closeGrid) * dis == chessBlack_XPOS[chessToCompareIndex])
                            && (yPos * dis == chessBlack_YPOS[chessToCompareIndex])) {
                        chessLinkedCount++;
                        if (chessLinkedCount == 5) {
                            return true;
                        }
                    }
                }
                if (chessLinkedCount == (chessLinkedCompare + 1)) {
                    chessLinkedCompare++;
                } else {
                    break;
                }
            }

            // 进入新的一组for循环时要将连接数等重置
            chessLinkedCount = 1;
            chessLinkedCompare = 1;
            // 判断当前下的棋子的上边4个棋子是否都为黑棋
            for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
                for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++) {
                    if ((xPos * dis == chessBlack_XPOS[chessToCompareIndex])
                            && ((yPos + closeGrid) * dis == chessBlack_YPOS[chessToCompareIndex])) {
                        chessLinkedCount++;
                        if (chessLinkedCount == 5) {
                            return true;
                        }
                    }
                }
                if (chessLinkedCount == (chessLinkedCompare + 1)) {
                    chessLinkedCompare++;
                } else {
                    break;
                }
            }

            // 判断当前下的棋子的下边4个棋子是否都为黑棋
            for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
                for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++) {
                    if ((xPos * dis == chessBlack_XPOS[chessToCompareIndex])
                            && ((yPos - closeGrid) * dis == chessBlack_YPOS[chessToCompareIndex])) {
                        chessLinkedCount++;
                        if (chessLinkedCount == 5) {
                            return true;
                        }
                    }
                }
                if (chessLinkedCount == (chessLinkedCompare + 1)) {
                    chessLinkedCompare++;
                } else {
                    break;
                }
            }

            chessLinkedCount = 1;
            chessLinkedCompare = 1;
            for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
                for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++) {
                    // 判断当前下的棋子的左上方向4个棋子是否都为黑棋
                    if (((xPos - closeGrid) * dis == chessBlack_XPOS[chessToCompareIndex])
                            && ((yPos + closeGrid) * dis == chessBlack_YPOS[chessToCompareIndex])) {
                        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++) {
                    // 判断当前下的棋子的右下方向4个棋子是否都为黑棋
                    if (((xPos + closeGrid) * dis == chessBlack_XPOS[chessToCompareIndex])
                            && ((yPos - closeGrid) * dis == chessBlack_YPOS[chessToCompareIndex])) {
                        chessLinkedCount++;
                        if (chessLinkedCount == 5) {
                            return true;
                        }
                    }
                }
                if (chessLinkedCount == (chessLinkedCompare + 1)) {
                    chessLinkedCompare++;
                } else {
                    break;
                }
            }

            chessLinkedCount = 1;
            chessLinkedCompare = 1;
            // 判断当前下的棋子的右上方向4个棋子是否都为黑棋
            for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
                for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++) {
                    if (((xPos + closeGrid) * dis == chessBlack_XPOS[chessToCompareIndex])
                            && ((yPos + closeGrid) * dis == chessBlack_YPOS[chessToCompareIndex])) {
                        chessLinkedCount++;
                        if (chessLinkedCount == 5) {
                            return true;
                        }
                    }
                }
                if (chessLinkedCount == (chessLinkedCompare + 1)) {
                    chessLinkedCompare++;
                } else {
                    break;
                }
            }

            // 判断当前下的棋子的左下方向4个棋子是否都为黑棋
            for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
                for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++) {
                    if (((xPos - closeGrid) * dis == chessBlack_XPOS[chessToCompareIndex])
                            && ((yPos - closeGrid) * dis == chessBlack_YPOS[chessToCompareIndex])) {
                        chessLinkedCount++;
                        if (chessLinkedCount == 5) {
                            return true;
                        }
                    }
                }
                if (chessLinkedCount == (chessLinkedCompare + 1)) {
                    chessLinkedCompare++;
                } else {
                    break;
                }
            }

        //当落子为白棋时
        } else if (chessColor == -1) {
            chessLinkedCount = 1;
            // 判断当前下的棋子的右边4个棋子是否都为白棋
            for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
                for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) {
                    if (((xPos + closeGrid) * dis == chessWhite_XPOS[chessToCompareIndex])
                            && (yPos * dis == chessWhite_YPOS[chessToCompareIndex])) {
                        chessLinkedCount++;
                        if (chessLinkedCount == 5) {
                            return true;
                        }
                    }
                }
                if (chessLinkedCount == (chessLinkedCompare + 1)) {
                    chessLinkedCompare++;
                } else {
                    break;
                }
            }

            // 判断当前下的棋子的左边4个棋子是否都为白棋
            for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
                for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) {
                    if (((xPos - closeGrid) * dis == chessWhite_XPOS[chessToCompareIndex])
                            && (yPos * dis == chessWhite_YPOS[chessToCompareIndex])) {
                        chessLinkedCount++;
                        if (chessLinkedCount == 5) {
                            return true;
                        }
                    }
                }
                if (chessLinkedCount == (chessLinkedCompare + 1)) {
                    chessLinkedCompare++;
                } else {
                    break;
                }
            }

            chessLinkedCount = 1;
            chessLinkedCompare = 1;
            // 判断当前下的棋子的上边4个棋子是否都为白棋
            for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
                for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) {
                    if ((xPos * dis == chessWhite_XPOS[chessToCompareIndex])
                            && ((yPos + closeGrid) * dis == chessWhite_YPOS[chessToCompareIndex])) {
                        chessLinkedCount++;
                        if (chessLinkedCount == 5) {
                            return true;
                        }
                    }
                }
                if (chessLinkedCount == (chessLinkedCompare + 1)) {
                    chessLinkedCompare++;
                } else {
                    break;
                }
            }

            // 判断当前下的棋子的下边4个棋子是否都为白棋
            for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
                for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) {
                    if ((xPos * dis == chessWhite_XPOS[chessToCompareIndex])
                            && ((yPos - closeGrid) * dis == chessWhite_YPOS[chessToCompareIndex])) {
                        chessLinkedCount++;
                        if (chessLinkedCount == 5) {
                            return true;
                        }
                    }
                }
                if (chessLinkedCount == (chessLinkedCompare + 1)) {
                    chessLinkedCompare++;
                } else {
                    break;
                }
            }

            chessLinkedCount = 1;
            chessLinkedCompare = 1;
            // 判断当前下的棋子的左上方向4个棋子是否都为白棋
            for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
                for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) {
                    if (((xPos - closeGrid) * dis == chessWhite_XPOS[chessToCompareIndex])
                            && ((yPos + closeGrid) * dis == chessWhite_YPOS[chessToCompareIndex])) {
                        chessLinkedCount++;
                        if (chessLinkedCount == 5) {
                            return true;
                        }
                    }
                }
                if (chessLinkedCount == (chessLinkedCompare + 1)) {
                    chessLinkedCompare++;
                } else {
                    break;
                }
            }

            // 判断当前下的棋子的右下方向4个棋子是否都为白棋
            for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
                for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) {
                    if (((xPos + closeGrid) * dis == chessWhite_XPOS[chessToCompareIndex])
                            && ((yPos - closeGrid) * dis == chessWhite_YPOS[chessToCompareIndex])) {
                        chessLinkedCount++;
                        if (chessLinkedCount == 5) {
                            return true;
                        }
                    }
                }
                if (chessLinkedCount == (chessLinkedCompare + 1)) {
                    chessLinkedCompare++;
                } else {
                    break;
                }
            }

            chessLinkedCount = 1;
            chessLinkedCompare = 1;
            // 判断当前下的棋子的右上方向4个棋子是否都为白棋
            for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
                for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) {
                    if (((xPos + closeGrid) * dis == chessWhite_XPOS[chessToCompareIndex])
                            && ((yPos + closeGrid) * dis == chessWhite_YPOS[chessToCompareIndex])) {
                        chessLinkedCount++;
                        if (chessLinkedCount == 5) {
                            return true;
                        }
                    }
                }
                if (chessLinkedCount == (chessLinkedCompare + 1)) {
                    chessLinkedCompare++;
                } else {
                    break;
                }
            }

            // 判断当前下的棋子的左下方向4个棋子是否都为白棋
            for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
                for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) {
                    if (((xPos - closeGrid) * dis == chessWhite_XPOS[chessToCompareIndex])
                            && ((yPos - closeGrid) * dis == chessWhite_YPOS[chessToCompareIndex])) {
                        chessLinkedCount++;
                        if (chessLinkedCount == 5) {
                            return (true);
                        }
                    }
                }
                if (chessLinkedCount == (chessLinkedCompare + 1)) {
                    chessLinkedCompare++;
                } else {
                    break;
                }
            }
        }
        return false;
    }

(3)棋盘多线程

/**
     * 处理取得的信息
     **/
    public void dealWithMsg(String msgReceived){
        //收到的信息为下棋
        if(msgReceived.startsWith("/chess")){
            StringTokenizer userMsgToken = new StringTokenizer(msgReceived, " ");
            // 表示棋子信息的数组、0索引为:x坐标;1索引位:y坐标;2索引位:棋子颜色
            String[] chessInfo = { "-1", "-1", "0" };
            // 标志位
            int i = 0;
            String chessInfoToken;
            while (userMsgToken.hasMoreTokens()) {
                chessInfoToken = userMsgToken.nextToken(" ");
                if (i >= 1 && i <= 3) {
                    chessInfo[i - 1] = chessInfoToken;
                }
                i++;
            }
            thisBoard.paintNetChessPoint(Integer.parseInt(chessInfo[0]), Integer
                    .parseInt(chessInfo[1]), Integer.parseInt(chessInfo[2]));
            // 收到的信息为改名
        } else if (msgReceived.startsWith("/yourname ")) {
            thisBoard.chessSelfName = msgReceived.substring(10);
            // 收到的为错误信息
        } else if ("/error".equals(msgReceived)) {
            thisBoard.statusText.setText("用户不存在,请重新加入!!!");
        }
    }

    /**
     * 发送信息
     **/
    public void sendMessage(String sentMessage){
        try {
            thisBoard.outputData.writeUTF(sentMessage);
        } catch (Exception ea) {
            ea.printStackTrace();
        }
    }

    @Override
    public void run(){
        String msgReceived;
        try {
            //等待信息输入
            while(true){
                msgReceived = thisBoard.inputData.readUTF();
                dealWithMsg(msgReceived);
            }
        } catch (IOException ignored) {
        }
    }

(4)部分GUI实现

/**
    * 画棋盘
    * */
    public void paint(Graphics g) {
        for (int i = 40; i <= 580; i = i + 30) {
            g.drawLine(40, i, 580, i);
        }

        for (int j = 40; j <= 580; j = j + 30) {
            g.drawLine(j, 40, j, 580);
        }
        g.fillOval(127, 127, 8, 8);
        g.fillOval(487, 127, 8, 8);
        g.fillOval(127, 487, 8, 8);
        g.fillOval(487, 487, 8, 8);
        g.fillOval(307, 307, 8, 8);
    }

4.心得体会

(1)在本次课程设计中,我们掌握了不少在课堂上未曾学到或是常常被我们忽略的编程技巧与知识,更学会了如何用git管理我们的代码。团队成员的编程能力得到了一定程度的提高。

(2)我们的五子棋游戏实现了联机对战功能,但仍有很多功能有待完善,如:聊天室、棋局计时等。且只能实现同一局域网内的联机。

(3)由于本次课设时间有限,我们的图形界面美观度还不够高,后期还可以加入图片、动画等进行完善。