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

📄 othello.java

📁 自己编制的othello程序
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                        SWORD_B_OFF_X+SWORD_WIDTH , 
						SWORD_B_OFF_Y+SWORD_HEIGHT,
                        this);
            g.drawImage(m_backgdGif,
                        SWORD_W_OFF_X , SWORD_W_OFF_Y , 
                        SWORD_W_OFF_X+SWORD_WIDTH ,
						SWORD_W_OFF_Y+SWORD_HEIGHT,
                        SWORD_W_OFF_X , SWORD_W_OFF_Y , 
                        SWORD_W_OFF_X+SWORD_WIDTH , 
						SWORD_W_OFF_Y+SWORD_HEIGHT,
                        this);
        }
        
    }

    //画一个棋子
    void DrawChess(int x,int y,int type)
    {
        int off;
        
        if (m_chessGif == null ) return;
        
        if ( type == WHITE )    off = 0;
        else
        {
            if ( type == BLACK )    off = 1;
            else
            {
                
                    if (type == NONE )
                    {
                        g.drawImage(m_backgdGif,
                                    CHESS_OFF_X+ x*CHESS_GRILLE_X ,
                                    CHESS_OFF_Y+y*CHESS_GRILLE_Y, 
                                    CHESS_OFF_X+(x+1)*CHESS_GRILLE_X , 
                                    CHESS_OFF_Y+(y+1)*CHESS_GRILLE_Y,
                                    (CHESS_OFF_X-BACK_OFF_X)+x*CHESS_GRILLE_X ,
                                    (CHESS_OFF_Y-BACK_OFF_Y)+y*CHESS_GRILLE_Y, 
                                    (CHESS_OFF_X-BACK_OFF_X)+(x+1)*CHESS_GRILLE_X , 
                                    (CHESS_OFF_Y-BACK_OFF_Y)+(y+1)*CHESS_GRILLE_Y,
                                    this);
                            return;
                    }
                    else return;
               
            }
        }
        
        g.drawImage(m_backgdGif,
                    CHESS_OFF_X+ x*CHESS_GRILLE_X ,
					CHESS_OFF_Y+y*CHESS_GRILLE_Y, 
                    CHESS_OFF_X+(x+1)*CHESS_GRILLE_X, 
					CHESS_OFF_Y+(y+1)*CHESS_GRILLE_Y,
                    (CHESS_OFF_X-BACK_OFF_X)+ x*CHESS_GRILLE_X ,
                    (CHESS_OFF_Y-BACK_OFF_Y)+y*CHESS_GRILLE_Y, 
                    (CHESS_OFF_X-BACK_OFF_X)+(x+1)*CHESS_GRILLE_X , 
                    (CHESS_OFF_Y-BACK_OFF_Y)+(y+1)*CHESS_GRILLE_Y,
                    this);

        g.drawImage(m_chessGif,
                    CHESS_OFF_X+x*CHESS_GRILLE_X ,
					CHESS_OFF_Y+y*CHESS_GRILLE_Y, 
                    CHESS_OFF_X+x*CHESS_GRILLE_X+CHESS_WIDTH , 
                    CHESS_OFF_Y+y*CHESS_GRILLE_Y+CHESS_HEIGHT,
                    off*CHESS_WIDTH, 0 , (off+1)*CHESS_WIDTH , CHESS_HEIGHT,
                    this);
                
    }

    //画背景
    void DrawBack()
    {
        g = this.getGraphics();
        if(m_backgdGif == null ) return;
        g.drawImage(m_backgdGif,
                    BACK_OFF_X ,BACK_OFF_Y,
                    BACK_OFF_X+BACK_WIDTH ,BACK_OFF_Y+BACK_HEIGHT,
                    0,0,BACK_WIDTH,BACK_HEIGHT,this);

    }
    
    //重画所有棋子
    
    void DrawChessBoard()
    {   int countb3=0 ;
    	int countw3=0 ;
        g = this.getGraphics();
        if(m_backgdGif == null ) return;
        
        for( int i = 1; i<= 8 ; i++)
        for( int j = 1; j<= 8 ; j++)
        {
            if (m_chess[i][j] != NONE)
            DrawChess(i,j,m_chess[i][j]);
        
            if ( m_chess[i][j] == WHITE )
               countw3++;
           if ( m_chess[i][j] == BLACK )
              countb3++;
         }
       
        g.drawString("黑棋个数"+String.valueOf(countb3),100,100);
        g.drawString("白棋个数"+String.valueOf(countw3),100,250);
        if(countw3==0)
         {	MessageBox.createMessageBox(" 黑方胜 !!  "+countb3+" vs "+countw3,"胜利");
         	return ;
         }
       else  if(countb3==0)
          	{	MessageBox.createMessageBox(" 白方胜 !!  "+countw3+" vs "+countb3,"胜利");
		   		return;	
		    }
		    
		
		}
    
    public void paint(Graphics g)
    {
        this.DrawBack();
        this.DrawChessBoard();
        this.DrawSide(m_side);
    }

    public void update(Graphics g)
    {
        paint(g);
    }
    
    /* ********************
        
        消息响应函数
    
    ***********************/
    class SymAction implements java.awt.event.ActionListener
    {
        public void actionPerformed(java.awt.event.ActionEvent event)
        {
            Object object = event.getSource();
            if (object == m_bNew)
                OnButtonNew();
            if (object == m_bPass)
                OnButtonPass();
            if (object == m_bBack)
                OnButtonBack();
            if (object == m_bNet)
                OnButtonNet();
            if (object == m_bLoc)
                OnButtonLoc();
        }
    }


    public boolean mouseMove(Event event,int px, int py)
    {
        //得到在棋盘中的坐标
        int x = (px-CHESS_OFF_X)/ CHESS_GRILLE_X;
        int y = (py-CHESS_OFF_Y)/ CHESS_GRILLE_Y;
        
        if((x<=8)&&(x>=1)&&(y<=8)&&(y>=1))//鼠标是否在棋盘内
        {
            if( !m_meput )
            {
                //对方放子
                if(!( (m_tempx<=8)&&(m_tempx>=1)&&(m_tempy<=8)&&(m_tempy>=1) ))
                    setCursor( new Cursor(Cursor.WAIT_CURSOR) );
            }
            else
                      
            m_tempx=x;
            m_tempy=y;
        }
        else
            setCursor( new Cursor(Cursor.DEFAULT_CURSOR) );
            
        return super.mouseMove (event,x,y);
    }

    public boolean mouseDown(Event event,int px, int py)
    {
        //得到在棋盘中的坐标
        int x= (px-CHESS_OFF_X)/ CHESS_GRILLE_X;
        int y= (py-CHESS_OFF_Y)/ CHESS_GRILLE_Y;

        Integer _px,_py;
        _px = new Integer(x);
        _py = new Integer (y);
        String s = "鼠标按下在: : "+_px.toString () + '.' +_py.toString ();
    
        if((x<=8)&&(x>=1)&&(y<=8)&&(y>=1))//鼠标是否在棋盘内
        {
            //在棋盘上
            if(m_meput)
            if (Judge(m_chess,x,y,m_side))
            {
                //放置棋子
                Put(m_chess,x,y,m_side); //放置棋子
                DrawChessBoard(); //重画棋盘
                update(g);
                m_tempx=x;
                m_tempy=y;
                
                m_currentx=x;
                m_currenty=y;
                m_backup_x[m_step]=x;
                m_backup_y[m_step]=y;
                m_step++; //存储
                
                OtherSide();
            }
        }
        return super.mouseDown(event,px,py);
    }

    void OnButtonNew()
    {
        // 新局
        //重新初始化数据
        RefreshData();
        update(g);
        // 重画棋盘
        for (int i=0 ; i <= 8 ;i++)
        for (int j=0 ; j <= 8 ;j++)
        
        DrawChess(i,j,m_chess[i][j]);
        this.DrawSide(BLACK);
    }

    
    void OnButtonBack()
    {
        //悔棋
        if (m_step<=1 || state == WITH_NET)
        {
            //不能悔棋
            MessageBox.createMessageBox("不能悔棋!!! ","警示!!");
            return ;
        }
        else
        {

            int  _step;
            int _x, _y, side;
            
            // !@#$#@!%#$%
            _x=0;
            _y=0;

            // 棋盘恢复初始状态 :
            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;
            
            m_tempx=0;
            m_tempy=0;
            
            side=BLACK;

            for (_step=1;_step<=m_step-3;_step++)
            {
                _x=m_backup_x[_step];
                _y=m_backup_y[_step];

                if (! ( ((_x<=8) && (_x>=1) && (_y<=8) && (_y>=1))
                    ||((_x==PASS)&&(_y==PASS))  ) )
                {
                    MessageBox.createMessageBox("储存出错","出错");
                    return;
                }
                else
                {
                    if(!(_x==PASS))
                    {
                        if( Judge( m_chess,_x , _y ,side))  Put(m_chess,_x,_y,side);
                        else
                        {
                            MessageBox.createMessageBox("储存出错","出错");
                            return;
                        }
                    }
                    side=-side;
                }
            }
            m_step-=2;
            m_currentx=_x;
            m_currenty=_y;

            for (int i=0 ; i <= 8 ;i++)
            for (int j=0 ; j <= 8 ;j++)
                DrawChess(i,j,m_chess[i][j]);
        }
    }
    
    void OnButtonPass()
    {

        //无法走棋,略过
        char i,j;
        boolean Passable=true;

        for (i=1;i<=8;i++)
        for (j=1;j<=8;j++)
            if (Judge(m_chess,i,j,m_side)) {  Passable=false; break;}

        if (Passable)
        {
            m_backup_x[m_step]=PASS;
            m_backup_y[m_step]=PASS;

            m_step++;
            DrawSide(-m_side);
            //   ComputerSide(chess,side);
            OtherSide();
        }
        else  { MessageBox.createMessageBox("你还可以走棋","提示"); return ; }

    }

    void OnButtonNet()
    {
        chat.show();
    }

    void OnButtonLoc()
    {
        
        if(this.state == this.WITH_COM )
        {
            OnButtonNew();
            this.m_bLoc.setImage( with_loc[0],with_loc[1],with_loc[2]);
            this.m_bLoc.repaint();
            state = this.WITH_LOC;
            m_meput = true;
        }
        else
     
        if(this.state == this.WITH_LOC )
        {
            OnButtonNew();
            this.m_bLoc.setImage( with_com[0],with_com[1],with_com[2]);
            this.m_bLoc.repaint();

            state = this.WITH_COM;
            m_meput = true;

        }
        else
        if(this.state == this.WITH_NET )
        {
            OnButtonNew();
            this.state = this.WITH_LOC;
            this.m_bLoc.setImage( with_loc[0],with_loc[1],with_loc[2]);
            this.m_bLoc.repaint();
            m_meput = true;

        }

    }

    //对网络发来的消息的处理
    public void DealInput(int type,String txt)
    {
        String msg = new String();
        msg = "收到消息, type : " +type +  "  ,正文 : " + txt;

        String buf  = new String();
        int i;
        int len = txt.length();

        switch (type){
          case 20:
            {
                // chat 消息
                txt+='\n';
                chat.m_text.append(txt);
                break;
            }

        case 22:
            {
                // chat 消息
                txt+='\n';
                chat.m_text.append(txt);
                break;
            }

        case 4:
            {
                //新登录者的名字
                
                int id ;
                i=0;
                buf = "";
                while(txt.charAt(i) != '\\' )
                {
                    buf+= txt.charAt(i);
                    i++;
                }
                id = Integer.decode(buf).intValue();
                i++;
                
                buf = "";
                while(i<len)
                {
                    buf+=txt.charAt(i);

⌨️ 快捷键说明

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