五子棋课设

局域网下的联机五子棋游戏 

功能描述

该游戏能够实现在局域网下双人对战的五子棋,游戏界面以及图形全以gui展示,使用socket编程实现联机对战,功能上以创建游戏,加入游戏为主,判断五子棋是否五个棋子连成线以及socket编程为主要编程内容。

实例图片:

步骤:
1.绘制一个五子棋窗口
2.创建游戏,开始游戏
3.落子
4.判断五子棋的胜利
5.两个客户端联机

主要功能流程图


四、UML图


项目运行截图
 

为了演示方便,该截图为一台电脑开启两个客户端进行联机

开启服务端,两个客户端连接:

创建游戏以及加入游戏

游戏进行中

赢下小局

项目关键代码
判断五子连线的情况

    /**
    * 五子棋核心算法
    * */
    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;
    }


服务端多线程

public void dealWithMsg(String msgReceived){
        String peerName;
        if(msgReceived.startsWith("/")){
            //更新用户列表
            if(msgReceived.equals("/list")){
                Feedback(getUserList());
                //创建游戏
            }else if (msgReceived.startsWith("/creatgame [inchess]")){
                //获取服务器名
                String gameCreaterName = msgReceived.substring(20);
                //将用户端口放入用户列表中
                clientNameHash.put(clientSocket, msgReceived.substring(11));
                //将主机设置为等待状态
                chessPeerHash.put(gameCreaterName, "wait");
                Feedback("/yourname " + clientNameHash.get(clientSocket));
                sendGamePeerMsg(gameCreaterName, "/OK");
                sendPublicMsg(getUserList());
                //收到的信息为加入游戏
            }else if (msgReceived.startsWith("/joingame ")){
                //StringTokenizer用于分隔字符串
                StringTokenizer userTokens = new StringTokenizer(msgReceived," ");
                String userToken;
                String gameCreatorName;
                String gamePaticipantName;
                String[] playerNames = {"0","0"};
                int nameIndex = 0;

                while(userTokens.hasMoreTokens()){
                    userToken =userTokens.nextToken(" ");
                    if(nameIndex >= 1 && nameIndex <= 2){
                        playerNames[nameIndex - 1] = userToken;
                    }
                    nameIndex++;
                }
                gameCreatorName = playerNames[0];
                gamePaticipantName = playerNames[1];
                //游戏已经创建
                if(chessPeerHash.containsKey(gameCreatorName)
                        && chessPeerHash.get(gameCreatorName).equals("wait")){
                    //增加游戏加入者的接口与名称对应
                    clientNameHash.put(clientSocket, ("[inchess]" + gamePaticipantName));
                    // 增加或修改游戏创建者与游戏加入者的名称的对应
                    chessPeerHash.put(gameCreatorName, gamePaticipantName);
                    sendPublicMsg(getUserList());
                    //发送信息给游戏加入者
                    sendGamePeerMsg(gamePaticipantName,("/peer " + "[inchess]" + gameCreatorName));
                    //发送游戏给游戏创建者
                    sendGamePeerMsg(gameCreatorName,("/peer " + "[inchess]" + gamePaticipantName));
                    //游戏未创建 则拒绝加入游戏
                }else{
                    sendGamePeerMsg(gamePaticipantName,"/reject");
                    try{
                        closeClient();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                //游戏中
            } else if (msgReceived.startsWith("/[inchess]")) {
                int firstLocation = 0;
                int lastLocation = msgReceived.indexOf(" ",0);
                peerName = msgReceived.substring((firstLocation + 1),lastLocation);
                msgReceived = msgReceived.substring((lastLocation + 1));
                if(sendGamePeerMsg(peerName,msgReceived)){
                    Feedback("/error");
                }
                //放弃游戏
            }else if(msgReceived.startsWith("/giveup ")){
                String chessClientName = msgReceived.substring(8);
                //胜利方为加入游戏者,发送胜利信息
                if(chessPeerHash.containsKey(chessClientName)
                        && !chessPeerHash.get(chessClientName).equals("wait")){
                    sendGamePeerMsg((String) chessPeerHash.get(chessClientName),"/youwin");
                    //删除退出游戏的用户
                    chessPeerHash.remove(chessClientName);
                }
                //胜利方为游戏创建者,发送胜利信息
                if(chessPeerHash.containsValue(chessClientName)){
                    sendGamePeerMsg((String) getHashKey(chessPeerHash,chessClientName),"/youwin");
                    // 删除退出游戏的用户
                    chessPeerHash.remove(getHashKey(chessPeerHash, chessClientName));
                }
                //收到其他信息
            }else{
                int lastLocation = msgReceived.indexOf(" ",0);
                if(lastLocation == -1){
                    Feedback("无效命令");
                }
            }
        } else {
            msgReceived = clientNameHash.get(clientSocket) + ">" +msgReceived;
            serverMsgPanel.msgTextArea.append(msgReceived + "\n");
            sendPublicMsg(msgReceived);
            serverMsgPanel.msgTextArea.setCaretPosition(serverMsgPanel.msgTextArea.getText().length());
        }
    }


项目总结与感想
1.从老师布置课设任务的时候就在构思跟初步编写代码,可以说在时间充裕的情况下,本次项目的编写完成度还行(除了没有完成聊天功能以外)。
2.在本次项目编写中学会了如何用git管理,知道了许多实用知识,也完善了许多知识点的不足。
3.可惜的是没有实现不同网络下的对局,仅仅只是在同一局域网下的对局。
4.界面还有待改进,甚至可以加入个数据库,对局的胜利应该要加上相应分数,玩家之间可以进行排名(等后续吧)
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值