⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serveoneclient.java

📁 基于java具备人工智能的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                strToCharArray(senderName,ms.msg);
                ms.type=3;
                if(spp.color==1){
                    ms.color = 2; //设置对方的棋子颜色
                }
                else{
                    ms.color = 1;
                }
                ms.setting=spp.setting;
                try{// 玩家B的 socket发送信息给 B的客户端
				pp.selfSocket.out.writeObject(ms);
                }catch(IOException er){
                    er.printStackTrace();
                }
                break;
            }
        }
    }
    // 工具函数,用来将字符串转换成字符数组
    public void strToCharArray(String str,char [] arr){
        int i=0;
        for(i=0;i<str.length()&&i<49;i++){
            arr[i] = str.charAt(i);
        }
        arr[i]='\0';
    }
    /**
     * 设置信息标志
     */
    public void setting(Message msg,boolean flag){
        int i=0;
        Player pp=null;
        for(i=0;i<Server.playerList.size();i++){
            pp =(Player) Server.playerList.get(i);
            if(this.equals(pp.selfSocket)==true){
                if(flag==true)
                    pp.setting=msg.setting;
                else
                    pp.color=msg.color;
            }
        }

    }

    /**
     * 过滤字符数组中的空格
     */
    public String arrayToString(char [] arr){
        int i=0,length=0;
        while(arr[i]!='\0' && i<50){
            i++;
        }
        length=i;
        char [] ss = new char[length];
        for(i=0;i<length;i++){
            ss[i]=arr[i];
        }
        String str = new String(ss);
        return str;
    }

    /**
     * 发送添加到用户列表中的新的用户信息
     */
    public void sendNewPlayer(Message player){
        Player pp=null;
        player.type=9;
        for(int i=0;i<Server.playerList.size();i++){
            pp=(Player)Server.playerList.get(i);
            try{
                if(pp.self!=null){//发送给除了自身之外的列表中所有的用户
                    pp.selfSocket.out.writeObject(player);
                }
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
    /**
     * 玩家游戏结束,等待下一个游戏开始
     * @param msg
     */
    public void playerRefresh(Message player){
        Player ppo = new Player();
        Player pp = null;
        ppo.color = player.color;
        ppo.self = new String(player.msg);
        ppo.selfSocket = this;
        Server.playerList.add(ppo);

        for(int i=0;i<Server.playerList.size();i++){
            pp = (Player)Server.playerList.get(i);
            if(this.equals(pp.selfSocket)==false){
                Message msg = new Message();
                strToCharArray(pp.self, msg.msg);
                msg.type = 9;
                msg.color = pp.color;
                try {
                    this.out.writeObject(msg);
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        Message ms = new Message();
        strToCharArray(ppo.self, ms.msg);
        ms.type=10;
        try{
            this.out.writeObject(ms);
        }catch(IOException e){
            e.printStackTrace();
        }
        player.type=10;
        for(int i=0 ;i<Server.playerList.size();i++){
            pp = (Player)Server.playerList.get(i);
            if(this.equals(pp.selfSocket)!=true){
                try{
                    pp.selfSocket.out.writeObject(player);
                }catch(IOException e){
                    e.printStackTrace();
                }

            }
        }

    }
    /**
     * 向用户列表中添加新用户
     * @param player
     */
    public void addPlayer(Message player){
        int i=0;
        Player pp=null,tp=null;

        for(i=0;i<Server.playerList.size();i++){
            pp=(Player)Server.playerList.get(i);
            if(this.equals(pp.selfSocket)==true){
                pp.self = new String(player.msg);
                try{
                    for (int j = 0; j < Server.playerList.size(); j++) {
                        Message temp = new Message();
                        tp = (Player) Server.playerList.get(j);
                        if (tp.self != null) {
                            strToCharArray(tp.self, temp.msg);
                            temp.type = 10; 
                            pp.selfSocket.out.writeObject(temp);
                        }
                    }
                }catch(IOException e){
                    e.printStackTrace();
                }
                break;
            }
        }
    }
    public Socket getSocket(){
        return socket;
    }
   
    public void checkVictory(Message msg){

    }
    /**
     * 落子
     */
    public void putChessman(Message msg){
        Group gg = new Group();
        ServeOneClient soc=null;
        String tName=null;
        int color=0;
        // 修改服务器棋盘
        for(int i=0;i<Server.groupList.size();i++){
            gg = (Group)Server.groupList.get(i);
            if(this.equals(gg.selfSocket)==true){
                soc = gg.playerSocket;
                tName = new String(gg.player);
                color = gg.selfColor;
                break;
            }
            if(this.equals(gg.playerSocket)==true){
                soc = gg.selfSocket;
                tName = new String(gg.self);
                color = gg.playerColor;
                break;
            }
        }
        gg.board[msg.coordinateX][msg.coordinateY]=color;

        // 判断是否有人获胜
        if(judge(gg,msg.coordinateX,msg.coordinateY)==true){// 如果有人获胜
            // 通知游戏双方
            try{
                msg.type=6;  // 设置获胜标志
                this.out.writeObject(msg);// 通知胜利方
                msg.type=17; // 设置失利标志
                soc.out.writeObject(msg); //通知失利方
            }catch(IOException e){
                e.printStackTrace();
            }
            Server.groupList.remove(gg); 
            return;
        }
        // 发送Message对象
        try{
            soc.out.writeObject(msg);
        }catch(IOException e){
            e.printStackTrace();
        }

    }
    /**
     * 判断是否有玩家获得游戏胜利
     */
    private boolean judge(Group gg,int x,int y){
        int i = 0, j = 0, count = 0;
        int color=gg.board[x][y];
        // 判断x轴方向
        for (i = 0, count = 0; x - i >= 0 && i < 5; i++) {
            if (color == gg.board[x - i][y]) {
                count++;
            }
            else {
                break;
            }
            if (count == 5)
                return true;
        }
        for (i = 1; x + i < 15 && i < 5; i++) {
            if (color == gg.board[x + i][y]) {
                count++;
            }
            else {
                break;
            }
            if (count == 5)
                return true;
        }
        // 判断y轴方向
        for (i = 0, count = 0; y - i >= 0 && i < 5; i++) {
            if (color == gg.board[x][y - i]) {
                count++;
            }
            else {
                break;
            }
            if (count == 5)
                return true;
        }
        for (i = 1; y + i < 15 && i < 5; i++) {
            if (color == gg.board[x][y + i]) {
                count++;
            }
            else {
                break;
            }
            if (count == 5)
                return true;
        }
        // 判断左上到右下方向
        for (i = 0, count = 0; x - i >= 0 && y - i >= 0 && i < 5; i++) {
            if (color == gg.board[x - i][y - i]) {
                count++;
            }
            else {
                break;
            }
            if (count == 5)
                return true;
        }
        for (i = 1; x + i < 15 && y + i < 15 && i < 5; i++) {
            if (color == gg.board[x + i][y + i]) {
                count++;
            }
            else {
                break;
            }
            if (count == 5) {
                return true;
            }
        }
        // 判断左下到右上方向
        for (i = 0, count = 0; x + i < 15 && y - i >= 0 && i < 5; i++) {
            if (color == gg.board[x + i][y - i]) {
                count++;
            }
            else {
                count = 0;
            }
            if (count == 5)
                return true;
        }
        for (i = 1; x - i >= 0 && y + i < 15 && i < 5; i++) {
            if (color == gg.board[x - i][y + i]) {
                count++;
            }
            else {
                break;
            }
            if (count == 5) {
                return true;
            }
        }
        return false;
    }   

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -