奇怪的行清除问题

奇怪的行清除问题

本文介绍了Java Tetris - 奇怪的行清除问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Tetris 中遇到了一些奇怪的清除行的事情...

I’m encountering a couple weird things with clearing rows in Tetris...

如果我将电路板的宽度和高度设置为相同(10 和 10):

If I set my board’s width and height to be the same (10 and 10):

board = new Board(10, 10, 35);

由以下因素决定:

public Board(int w, int h, int ts) {
    width = w;
    height = h;
    tilesize = ts;
    grid = new Tile[width][height];
...
}

public int getWidth() {
       return width * tilesize;
}

public int getHeight() {
       return height * tilesize;
}

行清除似乎工作正常(虽然,我不确定为什么它下面有一个隐藏的行......它不应该完全适合 JFrame 吗?):

Row clearing seems to work fine (although, I’m not sure why there is a hidden row beneath it… shouldn’t it fit perfectly into the JFrame?):

但如果我设置不同的宽度和高度(这里是 10 和 12),行将不会清除.

But if I set my width and height differently (here it’s 10 and 12), the rows won’t clear.

board = new Board(10, 12, 35);

行清除:

public void checkBottomFull() {
    System.out.println(grid.length);
    int lines = 0;
    for(int row = grid.length-1; row >= 0; row--) {
        while (isFull(row)) {
            lines++;
            clearRow(row);
        }
    }
}

public boolean isFull(int row) {
    for (int col = 0; col <= grid[row].length-1; col++) {
        while(grid[col][row] == null) {
             return false;
        }
    }
    return true;
}

public void clearRow(int rowToClear) {
     for(int row = rowToClear; row > 0; row--) {
         for(int col = 0; col < grid[row].length; col++) {
            grid[col][row] = grid[col][row-1];
         }
     }
}

任何想法为什么具有相同的尺寸可以清除行?为什么这很重要?

Any thoughts why having equal dimensions allows the rows to clear? Why should that matter?

谢谢!

我想我发现了问题,但是当我尝试解决它时,它现在抛出一个错误.

I think I found the issue, but when I tried to resolve it, it now throws an error.

之前,我有 checkBottomFull() 循环遍历 grid.length-1,它等于 10 ( for(int row = grid.length-1; row >= 0; row--) {) 但是我把高度(行)设为 12 gameBoard = new Board(10, 12, 35);,所以我应该循环遍历 12 行,而不是10. 我应该每行循环 10 列...

Before, I had checkBottomFull() looping through grid.length-1, which was equal to 10 ( for(int row = grid.length-1; row >= 0; row--) {) But I put height (rows) as 12 gameBoard = new Board(10, 12, 35);, so I should be looping through 12 rows, not 10. And I should be looping through 10 columns per row...

所以我硬编码了下面的 12 行和 10 列......

So I hardcoded the rows at 12 and columns at 10 below...

//loop through all rows (12 rows)
public void checkBottomFull() {
    System.out.println(grid.length);
    for(int row = 12; row > 0; row--) {
        while (isFull(row)) {
            clearRow(row);
        }
    }
}
//loop through all columns in that row (10 columns)
public boolean isFull(int row) {
    System.out.println(grid[row].length);
    for (int col = 0; col <= 10; col++) {
        if(grid[col][row] == null) {
             return false;
        }
    }
    return true;
}

但是它抛出了一个 IndexOutOfBounds 异常...

But it's throwing an IndexOutOfBounds exception...

不知道为什么

推荐答案

您可能在某处换掉了循环的网格/列最大值.当它是方形时它工作正常,因为它们是相同的.如果它不是正方形(即行/列的最大值不同),则一侧不会处理整个行/列,而另一侧会溢出.

You probably have the grid/column maxes for your loops swapped out somewhere. It works fine when it's square as they would be the same. If it's not square (ie the row/column maxes are different) then for one side it wouldn't process the entire row/column and the other would overflow.

不相关,但是 isFull(...) 方法中的 while(grid[col][row] == null) 循环有什么意义呢?这应该是 if 吗?

Unrelated, but what's the point of the while(grid[col][row] == null) loop in the isFull(...) method? Should this be an if instead?

这篇关于Java Tetris - 奇怪的行清除问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 15:00