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

📄 serveoneclient.java

📁 用java 编写的五子棋程序。适合java初学者
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * @param str
     */
    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;
        //System.out.println("arraytoString "+str+"length = "+length);
    }

    /**
     * add new player to all client's Player List
     * ... read server player list and send msg to everyone of them
     * @param player
     */
    public void sendNewPlayer(Message player){
        Player pp=null;
        player.type=9;
//        System.out.println("send new Player ...");
        for(int i=0;i<Server.playerList.size();i++){
            pp=(Player)Server.playerList.get(i);
            try{
                if(pp.self!=null){//send message to all but itself
                    //System.out.println(pp.self+" add list "+player.msg[0]+"i = "+i);
                    pp.selfSocket.out.writeObject(player);
                }
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
    /**
     * a player end a game and wait for a new one
     * @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;
//                System.out.println("refresh " + pp.self + "serverlist size " +
  //                                 Server.playerList.size());
                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();
        }
        //Message ms = new Message();
        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();
                }

            }
        }

    }
    /**
     * add the new player to server playerList
     * @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){
                //System.out.println("match socket ok and send to itself...");
                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.coordinateX=(byte)j;
                            temp.type = 10; //reply for type==1
                            //System.out.println("host "+pp.self+" add list to client name = "+temp.coordinateX+temp.msg[0]);
                            pp.selfSocket.out.writeObject(temp);
                        }
                    }
                   // out.writeObject(player);
                }catch(IOException e){
                    e.printStackTrace();
                }
                break;
            }
        }/*
        System.out.print("welcome ");
        int k=0;
        while(true){
            if(player.msg[k]!='\0')
                System.out.print(player.msg[k++]);
            else break;
        }
        System.out.println();*/
        //System.out.println(" at "+pp.selfSocket.socket.toString());
    }
    public Socket getSocket(){
        return socket;
    }
    /**
     * check whether msg sender is win
     * type=6 msg = winner 's name
     * @param msg
     */
    public void checkVictory(Message msg){

    }
    /**
     * type = 2 ,(msg.coordinateX,msg.coordinateY).msg.color
     * @param msg
     */
    public void putChessman(Message msg){
        Group gg = new Group();
        ServeOneClient soc=null;
        String tName=null;
        int color=0;
        // modify server board
        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;

        // whether someone win the game
        if(judge(gg,msg.coordinateX,msg.coordinateY)==true){// a man win
            // tell the two and remove them form the group list
            try{
                msg.type=6;  // win the game
                this.out.writeObject(msg);// tell this ,he win the game
                msg.type=17; // failed in the game
                soc.out.writeObject(msg); // tell soc ,he failed
//                System.out.println("send failed to "+tName);
            }catch(IOException e){
                e.printStackTrace();
            }
            Server.groupList.remove(gg); // remove from list
            return;
        }
        // send msg to another player
        try{
            //System.out.println("server put chess man "+msg.coordinateX+","+msg.coordinateY);
            soc.out.writeObject(msg);
        }catch(IOException e){
            e.printStackTrace();
        }

    }
    /**
     * judge if a man win the game
     * @param gg a group for judge
     * @param x  the newest kid's x coordinate
     * @param y  the newest kid's y coordinate
     * @return
     */
    private boolean judge(Group gg,int x,int y){
        int i = 0, j = 0, count = 0;
        int color=gg.board[x][y];
        // x direction
        for (i = 0, count = 0; x - i >= 0 && i < 5; i++) {
            if (color == gg.board[x - i][y]) {
                count++;
            }
            else {
                break;
            }
//          System.out.println("( "+x+" , "+y+" )"+"count = "+count);
            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 direction
        for (i = 0, count = 0; y - i >= 0 && i < 5; i++) {
            if (color == gg.board[x][y - i]) {
                count++;
            }
            else {
                break;
            }
//            System.out.println("( "+x+" , "+y+" )"+"count = "+count);
            if (count == 5)
                return true;
        }
        for (i = 1; y + i < 15 && i < 5; i++) {
            if (color == gg.board[x][y + i]) {
                count++;
            }
            else {
                break;
            }
//        System.out.println("( "+x+" , "+y+" )"+"count = "+count);
            if (count == 5)
                return true;
        }
        // '\' direction
        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;
            }
//            System.out.println("( "+x+" , "+y+" )"+"count = "+count);
            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;
            }
//          System.out.println("( "+x+" , "+y+" )"+"count = "+count);
            if (count == 5) {
                return true;
            }
        }
        // '/' direction
        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;
            }
//          System.out.println("( "+x+" , "+y+" )"+"count = "+count);
            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;
            }
//            System.out.println("( "+x+" , "+y+" )"+"count = "+count);
            if (count == 5) {
                return true;
            }
        }
        return false;
    }
    /**
     * judge if a man win the game
     * @param gg a group for judge
     * @param x  the newest kid's x coordinate
     * @param y  the newest kid's y coordinate
     * @return
     */
/*    private boolean judge(Group gg,int x,int y){

        int borderX=0,borderY=0,counter=0,i=0;
        int color=gg.board[x][y];
        if(x-4<0)            borderX=0;
        else            borderX=x-4;
        if(y-4<0)            borderY=0;
        else            borderY=y-4;


        // x direction
        while(borderX+i<x+5 && borderX+i<15){
            if(color==gg.board[borderX+i][y]){
                counter++;
            }else{
                counter=0;
            }
            i++;
            if(counter==5){
//                System.out.println("color "+color+gg.board[borderX+i][y]+"return form x "+(borderX+i)+","+y+"x,y"+x+","+y);
                return true;
            }
        }
        // y direction
        i=0;counter=0;
        while(borderY+i<y+5 && borderY+i<15){
            if(color==gg.board[x][borderY+i]){
                counter++;
            }else{
                counter=0;
            }
            i++;
            if(counter==5){
//                System.out.println("color "+color+gg.board[x][borderY+i]+"return form y "+x+","+(borderY+i)+","+y+"x,y"+x+","+y);
                return true;
            }
        }
        // '\' direction
        if(borderX>borderY)
            borderX = x-(y-borderY);
        else
            borderY = y-(x-borderX);
        i=0;counter=0;
        while(borderX+i<x+5 && borderY+i<y+5 && borderY+i<15&&borderX+i<15){
            if(color==gg.board[borderX+i][borderY+i]){
//                System.out.println("coordinate  \\  "+(borderX+i)+","+(borderY+i)+","+y+"x,y"+x+","+y);
                counter++;
            }else{
                counter=0;
            }
            i++;
            if(counter==5){
//                System.out.println("return form  \\  "+(borderX+i)+","+(borderY+i)+","+y+"x,y"+x+","+y);
                return true;
            }
        }
        // '/' direction
        if(x+4>=15){
            borderX = 14;
        }
        else{
            borderX = x+4;
        }
        if(borderX-x<y-borderY){
            borderY = y - (borderX - x);
        }
        else{
            borderX = x + (y - borderY);
        }
        i=0;counter=0;
        while(borderX-i>x-5 && borderY+i<y+5 && borderX-i>=0 && borderY+i<15){
            if(color==gg.board[borderX-i][borderY+i]){
                counter++;
            }else{
                counter=0;
            }
            i++;
            if(counter==5){
//                System.out.println("color "+color+gg.board[borderX-i][borderY+i]+"return form '/'   "+(borderX-i)+","+(borderY+i)+","+y+"x,y"+x+","+y);
                return true;
            }
        }
        return false;
    }
*/

} ///:-)

⌨️ 快捷键说明

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