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

📄 othello.java

📁 自己编制的othello程序
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/*******************
*                  *
  Othello.java
*                  *
********************/
import java.awt.*;
import java.applet.*;
//import MySocket;
//import ChatFrm;
//import MessageBox;

// Othello 主类
public class Othello extends Applet
{
    /************************
        My Parameter

    **************************/
    int m_value[][];//=new int [9][9];
    Graphics g;
    Image m_chessGif,m_backgdGif,m_swordGif,m_faceGif;
    Image with_net[],with_loc[],with_com[];
    
    
    int m_tempx,m_tempy; //标志可以放子的位置
    int m_side; //当前方
    int m_step; //当前步数
    int m_currentx,m_currenty; //当前位置
    int  m_chess[][]; //棋盘状况
    int m_trunkmin ,m_branchmin;//  设置最大分支树 最大子树
    
    boolean m_exit; //
    int m_backup_x[]; //存储每一步棋
    int m_backup_y[];

    int m_whichside;
    boolean m_meput; //是否该自己走棋
    MySocket m_socket;
    ChatFrm chat;
    int state; //当前状态
    String rival; //网络对战时的对手的名字
    int rival_side; //网络对战时的对手黑白棋子的选择
    
    final static int FREE = 0; //
    final static int WITH_NET = 1; //网络对战中
    final static int WITH_COM = 2; //人机对战中
    final static int WITH_LOC = 3; //本地两人对战
        
    // 关于棋盘画面的一些数值
    static int SEARCH_DEEPNESS;
    static int WHITE,BLACK,TEMP,NONE;
    
    static int SWORD_B_OFF_X,SWORD_B_OFF_Y,SWORD_W_OFF_X,SWORD_W_OFF_Y;
    static int SWORD_WIDTH,SWORD_HEIGHT;
    
    static int CHESS_OFF_X,CHESS_GRILLE_X,CHESS_OFF_Y,CHESS_GRILLE_Y;
    static int CHESS_WIDTH,CHESS_HEIGHT;
    
    static int BACK_OFF_X,BACK_OFF_Y;
    static int BACK_WIDTH,BACK_HEIGHT;
    
    // 自定义的按钮
    MyButton m_bNew,m_bPass,m_bBack,m_bNet,m_bLoc;
    
    int ComputePut;
    int I_Put;
    int PASS ;
    
    
    /* ********************
            初始化函数
        ***********************/
        public void init()
    {
        resize(524,440);
        InitData();
        InitGra();
        m_socket = null ;
        chat = new ChatFrm(this);
        state = WITH_LOC;
        paint(g);
    }

    void InitGra()
    {
        g= this.getGraphics();
        with_net = new Image[3];
        with_loc = new Image[3];
        with_com = new Image[3];
        
        //调入位图按钮的图片
        m_chessGif = getImage (getDocumentBase(),"image\\chess.gif ");
        m_swordGif = getImage (getDocumentBase(),"image\\sword.gif");
        //m_faceGif =  getImage (getDocumentBase(),"image\\face.gif");
        m_backgdGif =    getImage (getDocumentBase(),"image\\back.jpg");
        
        with_net[0] = getImage(getDocumentBase(),"image\\withnetc.gif");
        with_net[1] = getImage(getDocumentBase(),"image\\withneth.gif");
        with_net[2] = getImage(getDocumentBase(),"image\\withnetd.gif");
                   
        with_com[0] = getImage(getDocumentBase(),"image\\withcomc.gif");
        with_com[1] = getImage(getDocumentBase(),"image\\withcomh.gif");
        with_com[2] = getImage(getDocumentBase(),"image\\withcomd.gif");
                   
        with_loc[0] = this.getImage(getDocumentBase(),"image\\withlocc.gif");
        with_loc[1] = this.getImage(getDocumentBase(),"image\\withloch.gif");
        with_loc[2] = this.getImage(getDocumentBase(),"image\\withlocd.gif");
                   
        Image cold,hot,down;
        cold = this.getImage(getDocumentBase(),"image\\newc.Gif");
        hot = this.getImage (getDocumentBase(),"image\\newh.gif");
        down = this.getImage(getDocumentBase(),"image\\newd.gif");
        m_bNew.setImage(cold,hot,down);
        
        cold = this.getImage(getDocumentBase(),"image\\backc.gif");
        hot = this.getImage (getDocumentBase(),"image\\backh.gif");
        down = this.getImage(getDocumentBase(),"image\\backd.gif");
        m_bBack.setImage(cold,hot,down);
        
        cold = this.getImage(getDocumentBase(),"image\\passc.gif");
        hot = this.getImage (getDocumentBase(),"image\\passh.gif");
        down = this.getImage(getDocumentBase(),"image\\passd.gif");
        m_bPass.setImage(cold,hot,down);
        
        cold = this.getImage(getDocumentBase(),"image\\netc.gif");
        hot = this.getImage(getDocumentBase(),"image\\neth.gif");;
        down = this.getImage(getDocumentBase(),"image\\netd.gif");
        m_bNet.setImage(cold,hot,down);
        
        m_bLoc.setImage(with_loc[0],with_loc[1],with_loc[2]);

    }
    
    void InitData()
    {
        //常量初始化
        BLACK = -1;
        WHITE = 1;
        TEMP = 2;
        NONE = 0;
        ComputePut = -1;
        I_Put = 1;
        SEARCH_DEEPNESS = 4;
        PASS = -1;

        BACK_OFF_X = 0;
        BACK_OFF_Y = 0;

        SWORD_B_OFF_X = 51 + BACK_OFF_X;
        SWORD_B_OFF_Y = 111 + BACK_OFF_Y;
        SWORD_W_OFF_X = 51 + BACK_OFF_X;
        SWORD_W_OFF_Y = 250 + BACK_OFF_Y;
        SWORD_WIDTH = 32;
        SWORD_HEIGHT = 32;
    
        CHESS_OFF_X = 168 + BACK_OFF_X;
        CHESS_OFF_Y = 62 + BACK_OFF_Y;
        CHESS_GRILLE_X = 35;
        CHESS_GRILLE_Y = 35;
        CHESS_WIDTH = 26;
        CHESS_HEIGHT = 26;

        BACK_WIDTH = 520;
        BACK_HEIGHT = 420;

        m_chess = new int[10][10];
        m_backup_x = new int[82];
        m_backup_y = new int[82];

        this.m_value  = new int[10][10];

        int i;
        int j;
        
        for (i=1;i<=8;i++)
        for (j=1;j<=8;j++)
            m_value[i][j] = 1;

        //棋盘各位置的权值
        m_value[1][1] = m_value[8][8] = m_value[1][8] = m_value[8][1] = 10;
        m_value[2][2] = m_value[7][7] = m_value [2][7] = m_value [7][2] = -6;
        m_value[2][1] = m_value [7][1] = m_value[1][2] = m_value[1][7] =m_value[7][1] = m_value[7][7] = m_value[2][8] = m_value[7][8] = -6;
        
        for(i=3;i<=6;i++)
        {
            m_value[1][i] = m_value[i][1] = m_value [8][i] =m_value[i][8] =4;
        }
/*      int m_value[][];//=new int [9][9];
           {{  0 , 1 , 2 ,3 , 4 ,5 ,6 ,7,8},
            {  1 ,10 ,-4 ,4 ,4 ,4 ,4 ,-4 ,10},
            {  2 ,-4 ,-4 ,1 ,1 ,1 ,1 ,-4 ,-4},
            {  3 ,4 ,1 ,1 ,1 ,1 ,1 ,1 ,4},
            {  4 ,4 ,1 ,1 ,1 , 1 ,1 ,1 ,4},
            {  5 ,4 ,1 ,1 ,1 ,1 ,1 ,1 ,4},
            {  6 ,4 ,1 ,1 ,1 ,1 ,1 ,1 ,4},
            {  7 ,-4 ,-4 ,1 ,1 ,1 ,1 ,-4 , -4},
            {  8 ,10 ,-4 ,4 , 4 ,4 ,4 ,-4 ,10 }};
        */

        RefreshData();
        
        // 5个按钮,new back ,pass, net ,local
        m_bNew = new MyButton("新游戏");
        m_bPass = new MyButton("Pass");
        m_bBack = new MyButton("悔棋");
        m_bNet = new MyButton("联网游戏");
        m_bLoc = new MyButton("与电脑对战");
        
        this.setLayout(null);

        m_bNew.setBounds(20,5,40,60);
        m_bPass.setBounds(330,5,40,60);
        m_bBack.setBounds(400,5,40,60);
        m_bNet.setBounds( 65,5,40,60);
        m_bLoc.setBounds( 130,5,40,60);
        
        add(m_bNew);
        add(m_bPass);
        add(m_bBack);
        add(m_bLoc);
        add(m_bNet);
        
        SymAction lSymAction = new SymAction();
        m_bNew.addActionListener(lSymAction);
        m_bPass.addActionListener(lSymAction);
        m_bBack.addActionListener(lSymAction);
        m_bNet.addActionListener(lSymAction);
        m_bLoc.addActionListener(lSymAction);

    }
    
    void RefreshData()
    {

        //棋盘初始化
        int x,y;

        for (x=1;x<=8;x++)
        for (y=1;y<=8;y++)
            m_chess[x][y]=0;
            m_chess[4][4]=BLACK;
            m_chess[5][5]=BLACK;
            m_chess[4][5]=WHITE;
            m_chess[5][4]=WHITE;

        for(x=0;x<=40;x++)
        {
            m_backup_x[x] = 0;
            m_backup_y[x] = 0;
        }

        m_tempx = m_tempy = 0;
        m_currentx = m_currenty=0;
        
        m_side=BLACK;
        m_step=1;
        m_whichside=BLACK;

        m_meput = true;

    }

    void TRACE(String s)
    {    update(g);
        g.drawString(s,20,300);
        
    }
    void TRACE_2(String s)
    {   update(g);
        g.drawString(s,20,330);
        
    }

    /* *******************************
    
    AI 算法函数
    
      ********************************/
    
    void  Put (int chess[][],int x,int y,int side)
    {
        //在某位置上放置棋子
        
        int i,j,ii,jj;
        if (!((x<=8) && (x>=1) && (y<=8) && (y>=1) && (chess[x][y] == 0) ))
        {
            Integer _px,_py,_c;
            _px = new Integer(x);
            _py = new Integer (y);
            _c = new Integer  (chess[x][y]);
            String s = "Put Error in : "+_px.toString () + '.' +_py.toString () +
            	       "chess[][]  is " +_c.toString();
            MessageBox.createMessageBox (s,"出错");

            return;
        }

        chess[x][y]=side;

        if (y<=6)
        {
            if (chess[x][y+1]==-side)   // down
            {
                for (j=y+2;j<=8;j++)
                if(chess[x][j]==side)
                {
                    for(jj=y+1;jj<=j-1;jj++)
                    chess[x][jj]=side;
                    break;
                }
                else if(chess[x][j]==0) break;
            }

            if (chess[x+1][y+1]==-side)   // right down
            {
                j = y+2;
                for(i=x+2;(i<=8 && j<=8);i++)
                {
                    if( chess[i][j]==side)
                    {
                        jj = y+1;
                        for(ii=x+1;(ii<=i-1 && jj<=j-1);ii++)
                        {
                            chess[ii][jj]=side;
                            jj++;
                        }
                        break;
                    }
                    else if(chess[i][j]==0) break;
                    j++;
                }
            }

            if (chess[x-1][y+1]==-side)
            {
                j = y+2;
                for(i=x-2;(i>=1 && j<=8);i--)
                {
                    if( chess[i][j]==side)
                    {
                        jj = y+1;
                        for(ii=x-1;(ii>=i+1 && jj<=j-1);ii--)
                        {
                            chess[ii][jj]=side;
                            jj++;
                        }
                        break;
                    }
                    else if(chess[i][j]==0) break;
                    j++;
                }
            }
        }


        if (y>=2)
        {
            if (chess[x][y-1]==-side)
            {
                for(j=y-2;j>=1;j--)
                if (chess[x][j]==side)
                {
                    for(jj=y-1;jj>=j+1;jj--)
                    chess[x][jj]=side;
                    break;
                }
                else if(chess[x][j]==0) break;
            }

            if (chess[x+1][y-1]==-side)
            {
                j = y-2;
                for(i=x+2;(i<=8 && j>=1);i++)
                {
                    if( chess[i][j]==side)
                    {
                        jj = y-1;
                        for(ii=x+1;(ii<=i-1 && jj>=j+1);ii++)
                        {
                            chess[ii][jj]=side;
                            jj--;
                        }
                        break;
                    }
                    else if(chess[i][j]==0) break;
                    j--;
                }
            }

            if (chess[x-1][y-1]==-side)
            {
                j = y-2;
                for(i=x-2;(i>=1 && j>=1);i--)
                {
                    if (chess[i][j]==side)
                    {
                        jj = y-1;
                        for(ii=x-1;(ii>=i+1 && jj>=j+1);ii--)
                        {
                            chess[ii][jj]=side;
                            jj--;
                        }
                        break;
                    }
                    else if(chess[i][j]==0) break;
                    j--;
                }
            }

⌨️ 快捷键说明

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