Java俄罗斯方块 - 迭代双数组

时间:2013-04-01 15:57:53

标签: java swing

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

我对此的理解是 - 为每一行创建一个双循环,检查该行中的所有列是否已满(非空)。如果是,我将实现一个clear(实际上是设置当前行=上面的行)。现在,我只是想输出“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

4 个答案:

答案 0 :(得分:4)

这里几乎没有问题:

  1. 您应该在外部for循环中移动int columnsFilled = 0,因为它只是检查当前行。就像现在一样,在你越过第一行后,计数将是不正确的。
  2. 您正在检查if (columnsFilled == grid.length-1),实际上这应该是if (columnsFilled == grid[row].length-1)。您的检查将填充列数与行数进行比较,而不是与给定行中的列数进行比较。

答案 1 :(得分: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--) {

答案 2 :(得分: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"};
}

}

答案 3 :(得分: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");     
            }
        }
    }
}