令人困惑的阵列超出了游戏板的范围

时间:2016-11-15 09:36:56

标签: java arrays

这是一种代码中的一种方法,用于稍微复杂一些的tic tac toe。问题是,每当计算机试图进行移动时,它就会给出一个数组超出界限-1的错误,这实在令我感到困惑,因为我确保在代码中指明它不会这样做。

    public void computerMove()
    {
       boolean moveMade=false;
       while(!moveMade)
       {                                                                                                                                                                   
          int i = (int)(Math.random()*5);
          int j = (int)(Math.random()*4);
          int p = (int)(Math.random()*5+1); 
          //whether the piece will move up, down, left or right
           if (grid[i][j].getText().equals("0")){
             if (p==1&&grid[i+1][j].getText().equals("")
             &&i<3&&moveMade==false) 
             //here i specify when i<3 to make sure it wont try to go off the board
              {
                grid[i][j].setText("");
                grid[i+1][j].setText("0");
                turn++; checkXHorizontal(); check0Horizontal(); 
                checkXVertical(); check0Vertical(); checkXDF();
                check0DF(); checkXDB(); check0DB();
                whoseTurn.setText("Player's Turn");
                moveMade=true;
             }
             else if (p==2&&grid[i][j+1].getText().equals("")
             &&j<3&&moveMade==false)
             {
                grid[i][j].setText("");
                grid[i][j+1].setText("0");
                turn++; checkXHorizontal(); check0Horizontal(); 
                checkXVertical(); check0Vertical(); checkXDF(); 
                check0DF(); checkXDB(); check0DB();
                whoseTurn.setText("Player's Turn");
                moveMade=true;
             }
             else if (p==3&&grid[i][j-1].getText().equals(""
             )&&j>0&&moveMade==false)
             {
                grid[i][j].setText("");
                grid[i][j-1].setText("0");
                turn++; checkXHorizontal(); check0Horizontal(); 
                checkXVertical(); check0Vertical(); checkXDF(); 
                check0DF(); checkXDB(); check0DB();
                whoseTurn.setText("Player's Turn");
                moveMade=true;
             }
             else if (p==4&&grid[i-1][j].getText().equals("")&&
             i>1&&moveMade==false)
             {
                grid[i][j].setText("");
                grid[i-1][j].setText("0");
                turn++; checkXHorizontal(); check0Horizontal(); 
                checkXVertical(); check0Vertical(); checkXDF();
                check0DF(); checkXDB(); check0DB();
                whoseTurn.setText("Player's Turn");
                moveMade=true;
             }
             else {
                computerMove(); 
             }
          }
       }

1 个答案:

答案 0 :(得分:0)

当你这样做时

Error: Exception in thread "main" java.sql.SQLException: [Teradata Database] [TeraJDBC 15.10.00.22] [Error 3706] [SQLState 42000] Syntax error: expected something between '(' and the 'insert' keyword.

 else if (p==3&&grid[i][j-1].getText().equals(""
             )&&j>0&&moveMade==false)

else if (p==4&&grid[i-1][j].getText().equals("")&& i>1&&moveMade==false) grid[i][j-1]可能超出范围,因为java会分别在grid[i-1][j]j>0之前测试它们。

尝试以这种方式开头移动它们

i>1

else if (p==3&&j>0&&grid[i][j-1].getText().equals(""
                 )&&moveMade==false)