1

我正在构建俄罗斯方块,并尝试实现一种方法,该方法迭代网格 [][] 并检查每行的每一列,从底部开始,向上工作(认为如果我开始检查会更快从底部开始,因为这是需要清除大多数行的地方)。

我对此的理解是-为每一行创建一个双循环,检查该行中的所有列是否已满(不为空)。如果是,我将实现一个 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
4

4 回答 4

4

这里有几个问题:

  1. 您应该int columnsFilled = 0在外部 for 循环内移动,因为它只是对当前行的检查。就像现在一样,在您通过第一行后,计数将不正确。
  2. 您正在检查if (columnsFilled == grid.length-1)实际上应该是什么时候if (columnsFilled == grid[row].length-1)。您的检查将填充的列数与行数进行比较,而不是与给定行中的列数进行比较。
于 2013-04-01T16:09:27.100 回答
2

您可能会考虑添加一个类似于

public boolean isFullRow(Tile[][] grid, int row)
{
    for(int i=0; i<grid[row].length; i++)
    {
        if(grid[row][i] == null){ return false; }
    }
    return true;
}

这将有助于调试您的代码,并使其(稍微但不明显)更快。

然后你的checkButtomFull()功能可能看起来像

public void checkBottomFull() {
    //loops through rows only after each column has finished
    for(int row = grid.length-2; row > 0; row--) {
        if(isFullRow(grid, row)){
            // Do something if full row
        }
    }
}

最后,作为一个小问题,我的猜测是

for(int row = grid.length-2; row > 0; row--)

可以/应该写成

for(int row = grid.length-1; row >= 0; row--) {
于 2013-04-01T16:12:01.550 回答
1

您发布的代码中有一些逻辑错误,附加的代码修复了它们,但没有为您实现一切。

public class SampleClass {

static String[][] grid = new String[4][10];

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    loadGrid();
    checkBottomFull();
}
public static void checkBottomFull() {
    int columnsFilled = 0;
    //loops through rows only after each column has finished
    for(int row = grid.length-1; row >= 0; row--) {//needed 'greater than or equal to'
        columnsFilled=0; //need to reinitialize this before every iteration
        for(int col = 0; col <= grid[row].length-1; col++) { //needed 'less than or equal to'
           System.out.println(row + ", " + col+", "+grid[row][col]);       
            if (grid[row][col] != null) {
                columnsFilled++;
            }
        }
        System.out.println("columns that have a character" + columnsFilled);
        System.out.println("Needs to be "+grid[row].length+" to remove row");
        if (columnsFilled == grid[row].length) {
            System.out.println("full");   //this is where you should remove a row  
        }
    }
}

private static void loadGrid() {
    grid[0] = new String[] {"X","X","X","X","X","X","X","X","X","X"};
    grid[1] = new String[] {"X","X",null,"X","X","X",null,"X","X","X"};
    grid[2] = new String[] {"X","X","X",null,"X","X",null,"X","X","X"};
    grid[3] = new String[] {"X","X",null,"X","X","X",null,"X","X","X"};
}

}

于 2013-04-01T16:28:28.160 回答
0
public static void checkBottomFull() {

    //loops through rows only after each column has finished
    for(int row = grid.length-1; row >= 0; row--) {
        //loops through all columns per row, then row decrements
        int columnsFilled = 0;
        for(int col = 0; col < grid[row].length; col++) {
            System.out.println(grid[row][col] + ", " + row + ", " + col);       
            if (grid[row][col] != null && ++columnsFilled == grid[row].length) {
                System.out.println("full");     
            }
        }
    }
}
于 2013-04-01T16:09:40.987 回答