zhaodongxx 2015-05-18 01:57 采纳率: 50%
浏览 1760

java新人求助,五子棋胜利判定无效。

private MouseListener playChessHandler=new MouseAdapter(){

    public void mousePressed(MouseEvent e){
        int x=e.getX();
        int y=e.getY();

        //******放一颗棋子
        if(canplay=true){
        if(x<=grids*space&&x>=0&&y<=grids*space&&y>=0){
            if(chesses[round(x)][round(y)]==0){
                chesses[round(x)][round(y)]=currColor;
                //currColor=currColor==1?2:1;   //切换棋子颜色
                if(currColor==1){

                    currColor=2;
                    message="白棋先行";     //使上方提示信息随回合转变。
                }
                else{   

                    currColor=1;
                    message="黑棋先行";
                }   
                repaint();
            }
            else{

                JOptionPane.showMessageDialog(null,"此处已有棋子,请重下!");

            }
        }//*****
    /*    
        if(currColor ==chesses[round(x)][round(y) + i]){ 
           count++;
           i++;
        }    
        i = 1; 
        if(currColor == chesses[round(x)][round(y) - i]){ 
            count++; 
            i++; 
        }    
        if (count >= 5) {  
            flag = true;   
        }
        if(flag==true){
            JOptionPane.showMessageDialog(null,"游戏结束");
            canplay=false;
        }   */
    }
        else{
            JOptionPane.showMessageDialog(null,"游戏结束,大侠请重新来过!");
        }
    }
    public void mouseClicked(MouseEvent e){     
        int x=e.getX();
        int y=e.getY();
        int count =1 ;
        int i = 1 ; 

        if(currColor ==chesses[round(x)][round(y) + i]){ 
           count++;
           i++;
        }    
        i = 1; 
        if(currColor == chesses[round(x)][round(y) - i]){ 
            count++; 
            i++; 
        }    
        if (count >= 5) {  
            canplay = false;   
        }
        if(canplay==false){
            JOptionPane.showMessageDialog(null,"游戏结束");
        }
    }
};
  • 写回答

7条回答 默认 最新

  • danielinbiti 2015-05-18 02:06
    关注
     public void mouseClicked(MouseEvent e){     
            int x=e.getX();
            int y=e.getY();
            int count =1 ;//count是这里定义的1
            int i = 1 ; 
    
            if(currColor ==chesses[round(x)][round(y) + i]){ 
               count++;//这里最多加1
               i++;
            }    
            i = 1; 
            if(currColor == chesses[round(x)][round(y) - i]){ 
                count++; //这里最多加1
                i++; 
            }    
            if (count >= 5) {  //到这里如果上面条件都符合count最大也就只有3,怎么可能大于等于5呢
                canplay = false;   
            }
            if(canplay==false){
                JOptionPane.showMessageDialog(null,"游戏结束");
            }
        }
    

    mousePressed也一样,看count能不能符合条件。每次click都清成1了

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号