我正在構建俄羅斯方塊,並試圖實現一種方法,它通過tile [] []迭代並檢查每行的每列,從底部開始,一直向上(figured如果我從底部開始,那麼這將是一個更快的檢查,因爲這是大多數行需要清除的地方)。Java俄羅斯方塊 - 迭代通過雙數組
我對此的理解是 - 爲每一行創建一個double for循環,檢查該行中的所有列是否已滿(非空)。如果是,我會實現一個清晰的(這實際上是設置當前行=上面的行)。目前,我只是想輸出「Full」。
我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
什麼是電流輸出?你確定要從'row = grid.length -2'開始而不是'row = grid.length - 1'嗎? – nattyddubbs
放一個System.out。在調用顯示行的值的第一個for循環之後的println。 – bakoyaro
@bakoyaro'grid = new Tile [width] [height];'Tile只是一個類,其寬度和高度由Board.java確定 - 在用paintComponent填充行時,我填充每個tile,例如:'g.fillRect (row * tilesize,col * tilesize,tilesize,tilesize);' – Growler