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

📄 chesswzq.java~2~

📁 这是一个用java编写的网络对战五子棋程序
💻 JAVA~2~
📖 第 1 页 / 共 3 页
字号:
            label3.setText(str);
            setting.setEnabled(false);
            jrbBlack.setEnabled(false);
            jrbWhite.setEnabled(false);
            beginFlag=true;
        }
        if(flag==1){// press NO
            msg.type=4; // deny request
        }
        try{
            out.writeObject(msg);
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    public void paint(Graphics g){
        super.paint(g);
        drawChess(bpanel.getColor());
    }
    /**
     * Overridden so we can exit when window is closed
     */
    protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
            System.exit(0);
        }
    }
    // convert string to array which is end with '\0'
    public void strToCharArray(String str,char [] arr){
        int i;
        for(i=0;i<str.length()&&i<49;i++){
            arr[i] = str.charAt(i);
        }
        arr[i]='\0';
    }
    /**
     * filter array 's black space which is end of the array
     * @param arr
     * @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);
    }
    /**
     * do people play with computer
     */
    public void ptoComputer(){
        int x=0,y=0;
        int position;
        if(pFirst==false){
            x=7;
            y=7;
            bpanel.updateBoard(x,y);
            bpanel.drawChess(x,y);
            beginFlag=true;
        }else{
            beginFlag=true;
        }
    }
    /**
     * choos a best position to put chessman
     * @param x the newest chessman's x coordinate
     * @param y the newest chessman's y coordinate
     */
    private void cPutChess(int x,int y){
        setRect(x,y);
        setWeight(x,y,pColor);
        getBetter(3);
    }
    /**
     * set a rectangle of 9*9
     * @param x center of rectangle
     * @param y center of rectangle
     */
    private void setRect(int x,int y){
        if(x-4>0)  RectX1=x-4;
        else       RectX1=0;
        if(x+4>14) RectX2=14;
        else       RectX2=x+4;
        if(y-4>0)  RectY1=y-4;
        else       RectY1=0;
        if(y+4>14) RectY2=14;
        else       RectY2=y+4;
        if(RectX1>RectY1) RectX1 = x-(y-RectY1);
        else              RectY1 = y-(x-RectX1);
        if(RectX2>RectY2) RectY2 = y+(RectX2-x);
        else              RectX2 = x+(RectY2-y);
    }
    /**
     * set each black position's weight in the rectangle
     * @param x rectangle center's x coordinate
     * @param y rectangle center's x coordinate
     */
    private void setWeight(int x,int y,int tcolor){
        int i=RectX1,j=RectY1,value=0,k=0,n=0,flag=0;
        // '--' direction
        for(i=RectX1,j=y;i<=RectX2;i++){
            if(BoardPanel.board[i][j]!=0){
                continue;
            }
            value=0;flag=0;
            for(k=1;i-k>=RectX1 && k<5;k++){
                if(BoardPanel.board[i-k][j]==tcolor){
                    value++;
                    continue;
                }
                if(BoardPanel.board[i-k][j]==0){//black space
                    flag++;
                    break;
                }
            }
            for(k=1;i+k<RectX2 && k<5;k++){
                if(BoardPanel.board[i+k][j]==tcolor){
                    value++;
                }
                if(BoardPanel.board[i+k][j]==0){
                    flag++;
                    break;
                }
            }
            n=weight(value,flag);
            if(weightBoard[i][j]<n){
                weightBoard[i][j]=n;
            }
        }
        // '|' direction
        for(i=x,j=RectY1;j<=RectY2;j++){
            if(BoardPanel.board[i][j]!=0){
                continue;
            }
            value=0;flag=0;
            for(k=1;j-k>=RectY1 && k<5;k++){
                if(BoardPanel.board[i][j-k]==tcolor){
                    value++;
                    continue;
                }
                if(BoardPanel.board[i][j-k]==0){
                    flag++;
                    break;
                }
            }
            for(k=1;j+k<RectY2 && k<5;k++){
                if(BoardPanel.board[i][j+k]==tcolor){
                    value++;
                }
                if(BoardPanel.board[i][j+k]==0){
                    flag++;
                    break;
                }
            }
            n=weight(value,flag);
            if(weightBoard[i][j]<n){
                weightBoard[i][j]=n;
            }
        }
        // '\' direction
        for(i=RectX1,j=RectY1;i<=RectX2;i++,j++){
            if(BoardPanel.board[i][j]!=0){
                continue;
            }
            value=0;flag=0;
            for(k=1;i-k>=RectX1 && k<5 ;k++){
                if(BoardPanel.board[i-k][j-k]==tcolor){
                    value++;
                    continue;
                }
                if(BoardPanel.board[i-k][j-k]==0){
                    flag++;
                    break;
                }
            }
            for(k=1;i+k<RectX2 && k<5;k++){
                if(BoardPanel.board[i+k][j+k]==tcolor){
                    value++;
                }
                if(BoardPanel.board[i+k][j+k]==0){
                    flag++;
                    break;
                }
            }
            n=weight(value,flag);
            if(weightBoard[i][j]<n){
                weightBoard[i][j]=n;
            }
        }
        // '/' direction
        for(i=RectX2,j=RectY1;i>=RectX1;i--,j++){
            if(BoardPanel.board[i][j]!=0){
                continue;
            }
            value=0;flag=0;
            for(k=1;i+k<=RectX2 && k<5;k++){
                if(BoardPanel.board[i+k][j-k]==tcolor){
                    value++;
                    continue;
                }
                if(BoardPanel.board[i+k][j-k]==0){
                    flag++;
                    break;
                }
            }
            for(k=1;i-k>=RectX1 && k<5;k++){
                if(BoardPanel.board[i-k][j+k]==tcolor){
                    value++;
                }
                if(BoardPanel.board[i-k][j+k]==0){
                    flag++;
                    break;
                }
            }
            n=weight(value,flag);
            if(weightBoard[i][j]<n){
                weightBoard[i][j]=n;
            }
        }
    }
    /**
     * return weight
     * @param count
     * @param flag
     * @return
     */
    private int weight(int count, int flag)
    {
        int weight=0;
        switch(count){
            case 0:{
                if(flag>0) weight=200;
                else weight=0;
                break;
            }
            case 1:{
                if(flag>0) weight=1000;
                else weight=0;
                break;
            }
            case 2:{
                if(flag>0) weight=5000;
                else weight=0;
                break;
            }
            case 3:{
                if(flag>0) weight=8000;
                else weight=0;
                break;
            }
            case 4:{
                if(flag>0) weight=10000;
                else weight=0;
                break;
            }
        }
        return weight;
    }
    /**
     * search all board and find the better count position
     * @param count better position's number default value is 3
     */
    private void getBetter(int count){
        int [][] better = new int [count][2];
        int [][] tempArray = new int [15][15];
        for(int i=0;i<15;i++){
            for(int j=0;j<15;j++){
                tempArray[i][j]=weightBoard[i][j];
            }
        }
        for(int i=0;i<count;i++){
            getBiggest(tempArray,better[i][0],better[i][1]);
        }
        bestX=better[0][0];bestY=better[0][1];
    }
    /**
     *
     * @param arr
     * @param x
     * @param y
     */
    private void getBiggest(int [][] arr,int x,int y){
        int [] temp=new int[2];
        int swt=arr[0][0],tmp=0;
        for(int i=0;i<15;i++){
            for(int j=0;j<15;j++){
                if(arr[i][j]>swt){
                    temp[0]=i;temp[1]=j;
                    swt=arr[i][j];
                }
            }
        }
        x=temp[0];
        y=temp[1];
        arr[x][y]=0;
    }


}///:-)

⌨️ 快捷键说明

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