我正在构建俄罗斯方块,并尝试实现一种方法,该方法迭代网格 [][] 并检查每行的每一列,从底部开始,向上工作(认为如果我开始检查会更快从底部开始,因为这是需要清除大多数行的地方)。
我对此的理解是-为每一行创建一个双循环,检查该行中的所有列是否已满(不为空)。如果是,我将实现一个 clear (这实际上是设置当前行 = 上面的行)。现在,我只是想输出“完整”。
我的System.out.println(grid[row][col] + ", " + row + ", " + col);
检查正确地显示它从底行开始,然后迭代每一列......但if (grid[row][col] != null) {
检查似乎并没有停留在该行......
public void checkBottomFull() {
int columnsFilled = 0;
//loops through rows only after each column has finished
for(int row = grid.length-2; row > 0; row--) {
//loops through all columns per row, then row decrements
for(int col = 0; col < grid[row].length; col++) {
System.out.println(grid[row][col] + ", " + row + ", " + col);
if (grid[row][col] != null) {
columnsFilled++;
if (columnsFilled == grid.length-1) {
System.out.println("full");
}
}
}
}
}
有什么想法吗?
编辑
public void checkBottomFull() {
for(int row = grid.length-1; row >= 0; row--) {
//loops through columns of each individual row
if (isFull(row)) {
System.out.println("full");
clearRow(row);
}
}
}
public boolean isFull(int row) {
for (int col = 0; col < grid[row].length-1; col++) {
if(grid[row][col] == null) {
System.out.println(grid[row][col] + "... " + row + ", " + col);
return false;
}
}
return true;
}
public void clearRow(int row) {
for (int col = 0; col < grid[row].length-1; col++) {
System.out.println("clearing...");
grid[row][col] = grid[row-1][col];
}
}
System.out.println(grid[row][col] + "... " + row + ", " + col);
输出:为什么列不递增?
null... 9, 0
null... 8, 0
null... 7, 0
null... 6, 0
null... 5, 0
null... 4, 0
null... 3, 0
null... 2, 0
null... 1, 0
null... 0, 0