简体   繁体   English

Java Tetris-遍历双数组

[英]Java Tetris - iterating through double array

I'm building tetris and am trying to implement a method which iterates through grid[][] of tiles and checks each column per row, starting from the bottom, working its way up (figured it'd be a faster check if I started from the bottom since that's where most rows will need to be cleared). 我正在构建tetris,并尝试实现一种方法,该方法可通过tile的grid [] []进行迭代并从底部开始检查每行的每一列,然后逐步向上移动(图如果我开始,则会更快从底部开始,因为这是需要清除大多数行的位置)。

My understanding of this is- create a double for loop, for each row, check if all columns in that row are full (not null). 我对此的理解是-为每个行创建一个double for循环,检查该行中的所有列是否已满(不为null)。 If they are, I will implement a clear (which is really setting the current row = rows above). 如果是这样,我将实现一个清除方法(实际上是将当前行设置为上面的行)。 For now, I am just trying to output "Full". 现在,我只是想输出“ Full”。

My System.out.println(grid[row][col] + ", " + row + ", " + col); 我的System.out.println(grid[row][col] + ", " + row + ", " + col); check correctly shows that it's starting at the bottom row, then iterating each column... but the if (grid[row][col] != null) { check doesn't seem to be staying on that row... check正确显示它从底部开始,然后迭代每一列...但是if (grid[row][col] != null) { check似乎不停留在该行...

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");     
                }
            }
        }
    }
}

Any thoughts? 有什么想法吗?


EDIT 编辑

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); outputs: Why isn't column incrementing? 输出:为什么列不递增?

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

Few problems here: 这里的几个问题:

  1. You should move int columnsFilled = 0 inside the outer for loop since it's suppose to be a check of the current row only. 您应该将int columnsFilled = 0移到外部for循环内,因为它仅是对当前行的检查。 As it is now, after you get past the first row the count will be incorrect. 现在,经过第一行后,计数将不正确。
  2. You are checking if (columnsFilled == grid.length-1) when in fact this should be if (columnsFilled == grid[row].length-1) . 您正在检查if (columnsFilled == grid.length-1)实际上应该是if (columnsFilled == grid[row].length-1) Your check compares the number of filled columns against the number of rows, instead of against the number of columns in the given row. 您的检查将填充的列数与行数进行比较,而不是与给定行中的列数进行比较。

You might consider adding a convenience function similar to 您可以考虑添加类似于以下内容的便利功能

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;
}

This would help debug your code, as well as make it (slightly but not noticeably) faster. 这将有助于调试您的代码,并使其(略微但不明显)更快。

Then your checkButtomFull() function could look like 然后,您的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
        }
    }
}

Lastly, as a minor point, my guess is that 最后,作为次要点,我的猜测是

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

could/should be written as 可以/应该写成

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

There were a few logic errors in the code you posted, the attached code fixes them without implementing everything for you. 您发布的代码中存在一些逻辑错误,附件的代码修复了这些错误,而没有为您实现所有功能。

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"};
}

} }

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");     
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM