我用Java制作俄罗斯方块。一旦删除了一行(因为它已填满),我很难让我的块向下移动。我的代码抛出了一个arrayIndexOutOfBounds错误。其余的代码工作,它找到哪一行已满并将该行设置为0.但在底部附近的if语句中,它不起作用。谢谢 ! :)
// 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;
}