Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java俄罗斯方块游戏:在移动网格中的块时遇到问题_Java_Tetris - Fatal编程技术网

Java俄罗斯方块游戏:在移动网格中的块时遇到问题

Java俄罗斯方块游戏:在移动网格中的块时遇到问题,java,tetris,Java,Tetris,我正在用爪哇制作俄罗斯方块。我很难让我的区块向下移动,一旦一行被删除(因为它被填满)。我的代码抛出arrayIndexOutOfBounds错误。代码的其余部分起作用,它找到哪一行已满并将该行设置为0。但在靠近底部的if语句中,这就是它不起作用的地方。谢谢!:) //删除所有已填充的行 公共静态整型函数() { //变数 int numorrows=tetrisGrid.getnumorrows(); int nummofcols=tetrisGrid.getnummofcols(); int

我正在用爪哇制作俄罗斯方块。我很难让我的区块向下移动,一旦一行被删除(因为它被填满)。我的代码抛出arrayIndexOutOfBounds错误。代码的其余部分起作用,它找到哪一行已满并将该行设置为0。但在靠近底部的if语句中,这就是它不起作用的地方。谢谢!:)

//删除所有已填充的行
公共静态整型函数()
{
//变数
int numorrows=tetrisGrid.getnumorrows();
int nummofcols=tetrisGrid.getnummofcols();
int numRowsRemoved=0;
int[][]grid=tetrisGrid.getGrid();
//查找已填充的行数
for(int行=0;行
您是否尝试过使用调试器?当您编写
行++然后
col++,可以将这些索引移到网格之外。
grid[row][col]=1并不奇怪将引发异常。您能解释一下这行代码吗:shiftRow<2;在for loop why 2中?当我玩俄罗斯方块时,我没有使用2D数组。相反,我使用ArrayList来保存1D数组。然后,删除行时,只需从ArrayList中删除该行,并在ArrayList的开头添加一个空行。这样,您就不必担心每次删除一行时都会移动所有数据。有关基本工作示例,您可以查看:
// Removes any rows that are filled 
public static int removeRows() 
{
    // Variables
    int numOfRows = tetrisGrid.getNumOfRows(); 
    int numOfCols = tetrisGrid.getNumOfCols();
    int numRowsRemoved = 0;
    int [][] grid = tetrisGrid.getGrid();

    // Finds the number of rows that are filled 
    for (int row = 0; row < numOfRows; row++)
    {
      for (int col = 0; col < numOfCols; col++)
      {
        int blockValue = tetrisGrid.getBlockValue(row, col);

        if (blockValue == 0) 
        {
          break;
        }

        if (col == numOfCols - 1) 
        {
          numRowsRemoved++;

          // Sets the rows filled to zero
          for (int change = 0; change < numOfCols; change++)
          {
            grid [row][change] = 0; 
          }   

          // Shifts everything down until it hits blocks below
          for (int shiftRow = 0; shiftRow < 2; shiftRow++)
          {
            for (int shiftCol = 0; shiftCol < numOfCols; shiftCol++)
            {
              if (blockValue == 1)
              {
                grid [row][col] = 0;
                row++;
                col++;
                grid [row][col] = 1;   
                row--;
                col--; 
              }
              else if (blockValue == 0)
                break;

            }
          }

          // TAKE OUT -------------------------
          System.out.println ("--------"); 
          tetrisGrid.printGrid(); 
          System.out.println ("--------");
          // -------------------------
        } 
      }
    }  
    tetrisGrid.setGrid(grid);
    return numRowsRemoved; 
  }