java吧 关注:1,234,165贴子:12,700,450
  • 12回复贴,共1

自编的java黑白棋小游戏为什么落子时会报错?

只看楼主收藏回复

在知道上也发了同样的问题。。。但是知道既不能发度盘链接,也没法发图片,所以来贴吧请求各位大佬帮助报错图镇楼,楼下发详细代码和完整代码压缩包度盘链接

完整报错代码:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 8
at cn.dom678.cdu.BWBoard.judgeRow(BWBoard.java:62)
at cn.dom678.cdu.BWBoard.ifReverse(BWBoard.java:156)
at cn.dom678.cdu.BWBoard.makeMove(BWBoard.java:165)
at cn.dom678.cdu.Frame$1.mousePressed(Frame.java:33)
at java.awt.Component.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Window.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)


IP属地:四川1楼2018-01-05 15:01回复
    这楼放完整工程压缩包链接


    IP属地:四川2楼2018-01-05 15:06
    回复
      我自己觉得可能是错因的几个关键位置代码:
      ①鼠标监控器获取鼠标点击位置
      addMouseListener(new MouseAdapter(){
      @Override
      public void mousePressed(MouseEvent e){
      if(!isGameOver){
      int x=e.getX();
      int y=e.getY();
      if((x>=30&&x<=350)&&(y>=40&&y<=360)){
      int col=Math.round((x-30)/40.0f);
      int row=Math.round((y-40)/40.0f);
      if(BWBoard.makeMove(row,col,isBlackTurn)){
      isBlackTurn=!isBlackTurn;
      repaint();
      }
      }
      }
      }
      });
      }


      IP属地:四川来自Android客户端3楼2018-01-05 15:15
      回复
        ②判断能否下子
        public boolean makeMove(int row, int col, boolean isBlack) {
        /*
        * 判断能否下子
        */
        if ((board[row][col] == BoardState.EMPTY)&&(ifReverse(row,col,isBlack))) {
        board[row][col] = isBlack ? BoardState.BLACK : BoardState.WHITE;
        lastCol=col;
        lastRow=row;
        return true;
        }
        return false;
        }


        IP属地:四川来自Android客户端4楼2018-01-05 15:15
        回复
          ③判断假设下子后能否翻转
          public boolean ifReverse(int row, int col, boolean isBlack) {
          /*
          * 判断本次下子是否产生翻转
          */
          boolean ifReverse=false;
          BoardState state=isBlack?BoardState.BLACK:BoardState.WHITE;
          if(judgeRow(row,col,state)||judgeCol(row,col,state)||judgeX1(row,col,state)||judgeX2(row,col,state))
          ifReverse=true;
          return ifReverse;
          }


          IP属地:四川来自Android客户端5楼2018-01-05 15:15
          回复
            ④逐个方向判断能否翻转(这里只给出竖向判断的,其他方向的代码和竖向都差不多,需要全部方向可以看完整代码压缩包)
            public boolean judgeRow(int row, int col,BoardState state) {
            /*
            * 竖排能否反转
            */
            int rowIndex=row;
            boolean judge=false;
            boolean judgeUp=true;
            boolean judgeDown=true;
            if(board[row+1][col]==state)
            judgeUp=false;//此子相邻上方为同色则上方必然无法反转
            if(board[row-1][col]==state)
            judgeDown=false;//此子相邻下方为同色则下方必然无法反转
            while((rowIndex-1>=0)&&judgeDown) {
            if(board[rowIndex-1][col]==state)
            judge=true;
            rowIndex-=1;
            }
            while((rowIndex+1<=board.length)&&judgeDown) {
            if(board[rowIndex+1][col]==state)
            judge=true;
            rowIndex+=1;
            }
            return judge;
            }


            IP属地:四川来自Android客户端6楼2018-01-05 15:16
            回复
              Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 8
              at cn.dom678.cdu.BWBoard.judgeRow(BWBoard.java:62)
              这句话不是告诉你了吗


              IP属地:辽宁8楼2018-01-05 15:36
              收起回复
                微我m3323755 发我代码给你看看


                来自Android客户端9楼2018-01-05 22:44
                回复
                  确定代码是你写的,报错这么明显该不会改


                  IP属地:广东来自Android客户端11楼2018-01-06 15:25
                  回复
                    错误说你数组下标超过边界,你仔细想想


                    IP属地:湖南来自Android客户端12楼2018-01-07 18:38
                    回复
                      emmm谢谢大家,我已经找到错因了是我递归查找数组的时候>多加了一个=主要还是不太懂java各种报错代码的涵义吃了大亏谢谢大家的帮助了


                      IP属地:四川来自Android客户端13楼2018-01-07 19:09
                      回复
                        只看到了数组越界


                        IP属地:河南来自Android客户端14楼2018-01-26 23:48
                        回复