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

📄 othello.java

📁 自己编制的othello程序
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                    i++;
                }
                
                m_socket.m_client[id] = buf;
                chat.m_obj.insert(m_socket.m_client[id],0);
                
                break;
            }
        case 6:
            {
                //某客户断开链接,删除
            //  MessageBox("有客户断开链接" + txt);
                i=0;
                while(i<len)
                {
                    buf+=txt.charAt(i);
                    i++;
                }

                int id = Integer.decode(buf).intValue();

                if(state == WITH_NET )
                if ( rival.equals(m_socket.m_client[id]) )
                {
                    MessageBox.createMessageBox("你与 " + m_socket.m_client[id] +
                          " 的链接已中断,对战中断。","对战中断");
                    m_bLoc.setImage(this.with_loc[0],this.with_loc[1],this.with_loc[2]);
                    OnButtonNew();
                    m_bLoc.repaint();
                    state = WITH_LOC;
                    m_meput = true;
                    m_side = BLACK;
                }                
                chat.m_obj.remove(m_socket.m_client[id]);
                chat.m_text.append("     " + m_socket.m_client[id] + "已经离开主机\n");
                m_socket.m_client[id] = null ;

                break;
            }
        case 7:
            {
                //受到挑战请求 (对方执黑)
                if ( state != WITH_NET )
                {
                    if( MessageBox.createMessageBox("" + txt + 
                        " 向你发出挑战要求 !! (他执黑先行) ","挑战 !!",1))
                    {
                        //接收
                        this.OnButtonNew();
                        m_socket.Send(9,txt,chat.m_name);
                        state = WITH_NET;
                        rival = txt;
                        m_meput = false;
                        m_side = BLACK;
                        DrawSide(BLACK);
                        m_bLoc.setImage(this.with_net[0],
                        	this.with_net[1],this.with_net[2]);
                        m_bLoc.repaint();
                    }
                    else
                    {
                        m_socket.Send(10,txt,chat.m_name+" 拒绝与你对战.");
                    }
                }
                else
                {
                    m_socket.Send(10,txt,chat.m_name+ " 正在对战中. ");
                }
                break;
            }
        case 8:
            {
                //收到挑战请求 (对方执白)
                if ( state != WITH_NET )
                {
                    //接收
                    if( MessageBox.createMessageBox( "   " + txt + 
                    		" 向你发出挑战要求 !! (你执黑先行) ","挑战 !!",1))
                    {
                        this.OnButtonNew();
                        state = WITH_NET;
                        rival = txt;
                        m_socket.Send(9,txt,chat.m_name);
                        m_side = BLACK;
                        DrawSide(BLACK);
                        m_meput = true;
                        m_bLoc.setImage(this.with_net[0],this.with_net[1],
                        		this.with_net[2]);
                        m_bLoc.repaint();

                    }
                    else
                    {
                        m_socket.Send(10,txt,chat.m_name +" 拒绝与你对战.");
                    }
                    
                }
                else
                {
                    
                    m_socket.Send(10,txt,chat.m_name+ " 正在对战中. ");
                }
                break;
            }
        case 9:
            {
                // 对方接收挑战
                if ( txt.equals(rival))
                {
                    MessageBox.createMessageBox( "  " + rival + 
                    		" 接受你的挑战 !! 对战开始 !!","对战开始");
                    if ( m_side == WHITE )
                    {
                        this.OnButtonNew();
                        state = WITH_NET;
                        m_side = - rival_side;
                        m_meput = false;
                        DrawSide(BLACK);
                    }
                    else
                    {
                        this.OnButtonNew();
                        state = WITH_NET;
                        m_side = - rival_side;
                        m_meput = true;
                        DrawSide(BLACK);
                    }
                    m_bLoc.setImage(this.with_net[0],this.with_net[1],this.with_net[2]);
                    m_bLoc.repaint();
                }
                else
                {
                    m_socket.Send(10,txt,chat.m_name+ " 正在对战中. ");
                }
                break;
            }

        case 10:
            {
                //对方拒绝应战
                MessageBox.createMessageBox("  "+ rival + " 拒绝接受挑战。","拒绝挑战");
                break;
            }
        case 21:
            {
                //对方走棋消息
                if(state == WITH_NET)
                {
                    if ( !m_meput )
                    {
                        buf = "";
                        i =0;
                        while(txt.charAt(i) != '\\')
                        {
                            buf+= txt.charAt(i);
                            i++;
                        }
                        i++;
                        int x = Integer.decode(buf).intValue();

                        buf = "";
                        while(txt.charAt(i) != '\\')
                        {
                            buf+= txt.charAt(i);
                            i++;
                        }
                        int y = Integer.decode(buf).intValue();
                        
                        Put(m_chess,x,y,m_side);
                        m_meput = true;
                        m_side = - m_side;
                        if( this.JudgeEnd() )
                        {
                            // 网络对弈结束
                            m_bLoc.setImage(this.with_loc[0],this.with_loc[1],
                                            this.with_loc[2]);
                            m_bLoc.repaint();
                        }

                    }
                }
                break;
            }

        case 11:
            {
                //服务器已满
                MessageBox.createMessageBox("链接被拒绝,服务器已满","链接失败");
                chat.OnDisconnect();
                return;
                
            }
        case 12:
            {
                //名字冲突
              MessageBox.createMessageBox("已经有人用这个名字登录到服务器,换个名字吧",
                                            "登录失败");
                chat.OnDisconnect();
                return;
            }
        case 2:
            {
                // 得到主机发来的名字列表
                
                i=0;
                int list = 0;

                while(i<len)
                {
                    // get name
                    buf = "";
                    while(txt.charAt(i) != '\\')
                    {
                        buf += txt.charAt(i);
                        i++;
                    }
                    list = Integer.decode(buf).intValue();
                    i++;
                    
                    m_socket.m_client[list] = new String();
                    while (txt.charAt(i)!='\\')
                    {
                        m_socket.m_client[list]+=txt.charAt(i);
                        i++;
                    }
                    i++;  // charAt(i) ==0
                    this.chat.m_obj.insert(m_socket.m_client[list],list); // insert to choice
                    System.out.print ("新加入用户数 ");
                    System.out.println(list);
                }
        
                this.chat.m_text.append("       成功链接到主机 "
                                        + m_socket.m_socket.getInetAddress() + "\n");
                System.out.println("登录用户处理完毕");
                
                break;
            }
default:
            break;
        }
        
    }
    
  public void destory()
    {
        this.chat.dispose();
        super.destroy();
    }


//位图按钮类
class MyButton extends Button
{
    boolean self_draw;
    int state;
    static final int HOT = 1;
    static final int COLD = 0;
    static final int DOWN = 2;
    Image m_cold,m_hot,m_down;

    public MyButton(String str)
    {
        self_draw = false;
        this.setLabel(str);
        mouse1 listener=new mouse1();
        this.addMouseMotionListener(listener);
        mouse2 listener2 = new mouse2();
        this.addMouseListener(listener2);
        state = COLD;
    }
    
    public void setImage(Image cold ,Image hot, Image down)
    {
        m_cold = cold;
        m_hot = hot;
        m_down = down;
        self_draw = true;
    }
    
    public void update(Graphics g)
    {
        paint(g);
    }
        
    public void paintAll(Graphics g)
    {
        paint(g);
    }
    
    public void repaint()
    {
        Graphics g = this.getGraphics();
        paint(g);
    }
    
    public void paint(Graphics g)
    {
        if ( self_draw)
        {
            Image img = null ;
            if (this.state == COLD) img = m_cold;
            else
            if (this.state == HOT ) img = m_hot;
            else
            if (this.state == DOWN) img = m_down;
            
            g.drawImage(img,
                        0,0,this.getBounds().width,this.getBounds().height,
                        0,0,this.getBounds().width,this.getBounds().height,
                        this);
        }
    }
    
    public void _mouseDown()
    {
        if (self_draw)
        {
            state = DOWN;
            this.repaint();
        }
    }

    public void _mouseMove()
    {
        if (self_draw)
        {
            state = HOT;
            this.repaint();
        }
    }

    public void _mouseUp()
    {
        if (self_draw)
        {
            state = COLD;
            this.repaint();
        }
    }
    
    public void _mouseEnter()
    {
        
        if (self_draw)
        {
            state = HOT;
            this.repaint();
        }
    }
    
    public void _mouseExit()
    {
        if (self_draw)
        {
            state = COLD;
            this.repaint();
        }
    }
    
    class mouse1 extends java.awt.event.MouseMotionAdapter
    {
        public void mouseMoved(java.awt.event.MouseEvent  e)
        {
            Object obj=e.getSource();
            if(obj==MyButton.this)
                _mouseMove();
            System.runFinalization();//运行所有未结束的对象的runFinalization方法
        }
                    
    }
    
    class mouse2 extends java.awt.event.MouseAdapter
    {
        public void mouseReleased(java.awt.event.MouseEvent event)
        {
            Object object = event.getSource();
            if (object == MyButton.this)
                _mouseUp();
        }

        public void mousePressed(java.awt.event.MouseEvent event)
        {
            Object object = event.getSource();
            if (object == MyButton.this)
                _mouseDown();
        }
        
        public void mouseExited(java.awt.event.MouseEvent event)
        {
            Object object = event.getSource();
            if (object == MyButton.this)
                _mouseExit();
        }

        public void mouseEntered(java.awt.event.MouseEvent event)
        {
            Object object = event.getSource();
            if (object == MyButton.this)
            {
                _mouseEnter();
            }
        }
    }

}
}

⌨️ 快捷键说明

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