无法发现我的 Java 俄罗斯方块游戏中的错误

发布于 2024-11-08 19:29:00 字数 3652 浏览 0 评论 0原文

我和我的朋友用 Java 制作了一个类似俄罗斯方块的游戏,它运行良好一段时间,然后突然在每次总块数大约相同的情况下出错。例如,一次可能会出现 42 件错误,下一次会出现 46 件错误,之后又会出现 44 件错误。

[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][2][2][ ][ ][ ][ ][ ][ ][7]
[ ][2][2][2][3][3][ ][4][ ][7]
[2][2][1][3][3][1][ ][4][ ][7]

上面是错误发生之前的示例。不要尝试过多地阅读上面的示例,因为错误不依赖于棋子位置等。

这是错误发生后网格的样子...

[ ][ ][ ][2][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][2][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][2][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][2][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][2][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][2][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][2][ ][ ][ ][ ][ ][ ]
[ ][2][2][ ][ ][ ][ ][ ][ ][7]
[ ][2][2][2][3][3][ ][4][ ][7]
[2][2][1][3][3][1][ ][4][ ][7]

注意,两个一直到顶部网格的?这就是正在发生的事情。

这就是事情变得有趣的地方。我们将其限制为特定的函数和特定的代码行。

grid[x+legend[piece.type].fetch(i, 0, piece.rotate)]
    [y+legend[piece.type].fetch(i, 1, piece.rotate)]
    .type = piece.type+1;

上面的代码行正是被认为导致问题的代码行。问题是这行代码没有任何问题。我已经调试整个程序大约一周了,并且在每一次更改之后我都记录了网格。

上面两个网格之间的区别在于对该行的一次调用。它不可能分配网格中的所有其他单元格。调试日志显示变量是:

i: 0
legend[piece.type].block.length: 4
legend[piece.type].fetch(i, 0, piece.rotate): 0
legend[piece.type].fetch(i, 1, piece.rotate): 0
piece.type: 1
piece.rotate: 3
x: 3
y: 6

这些是影响上面第二个网格的变量。有问题的代码行应该是这样的:

grid[3+0][6+0].type = 1+1;

我们是否有可能发现 Java 本身的错误?我花了几个小时调试这段代码,而且从来没有遇到过找不到的错误(不过,我通常用 python 编写代码......Java 对我来说相当新)

此外,该错误似乎受到网格的大小。如果我将网格的高度设置为较低的数字,则导致错误的部分就会减少。同样的道理,如果我制作一个更高的网格,则需要更多的块。

希望 Java 专家能够帮助阐明这一情况。

ps 我正在 Mac 上使用 JRE JVM 1.6 在 Eclipse 中进行编程。我也尝试过 JRE JVM 1.4,同样的事情发生了。

附加信息

private Grid grid[][] = new Grid[dimx][dimy];

上面的代码是在创建网格时的。网格是一个类:

public class Grid {
    int type;

    public Grid(int type) {
    }
}

这是大量调试日志代码的一部分...这是打印基于文本的网格的函数。

public void printGrid(int line) {
    System.out.print("\n\n\n"+line+":\n");
    for (int y = 0;y < grid[0].length;y++) {
        for (int x = 0;x < grid.length;x++) {
            if (grid[x][y].type == 0) {
                System.out.print("[ ]");
            } else {
                System.out.print("["+grid[x][y].type+"]");
            }

        }
        System.out.println();
    }
    System.out.println("\n\n");
}

变量“line”并不重要,它只是告诉我们函数 printGrid() 是从哪个行号调用的。

如果你们需要更多信息,请告诉我。

更多附加信息:

以下是该程序的所有文件。

哦,这里是调试日志的摘录,可为您提供更多信息。

http://pastebin.com/9VKhF8CR

包括成功放置一块(bug之前放置的最后一块)和然后它显示错误正在发生。该函数的所有变量都被打印出来,没有一个出现不合适的地方。该错误发生在放置在网格上任何位置的任何棋子上。每次调用我上面提到的线路后都会打印网格。如果你们需要更多信息,请告诉我。我希望这个错误被消灭。

My friend and I have made a tetris-like game in Java and it works fine for a while and then suddenly bugs out at around the same number of total pieces every time. For example, it might bug out at 42 pieces one time, 46 the next, and 44 the time after that.

[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][2][2][ ][ ][ ][ ][ ][ ][7]
[ ][2][2][2][3][3][ ][4][ ][7]
[2][2][1][3][3][1][ ][4][ ][7]

The above is an example of what it looks like right before the bug happens. Don't try to read into the above sample too much as the bug is not dependent on piece positions, etc.

Here is what the grid looks like right after the bug occurs...

[ ][ ][ ][2][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][2][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][2][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][2][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][2][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][2][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][2][ ][ ][ ][ ][ ][ ]
[ ][2][2][ ][ ][ ][ ][ ][ ][7]
[ ][2][2][2][3][3][ ][4][ ][7]
[2][2][1][3][3][1][ ][4][ ][7]

Notice the twos going all the way up to the top of the grid? That is what is happening.

Here's where things get interesting. We have limited it down to a specific function and a specific line of code.

grid[x+legend[piece.type].fetch(i, 0, piece.rotate)]
    [y+legend[piece.type].fetch(i, 1, piece.rotate)]
    .type = piece.type+1;

The above line of code is the exact line of code believed to be causing the problem. The problem is that there is nothing wrong with that line of code. I've been debugging the whole program for about a week and I have it log the grid after every single change.

The difference between the two grids above was one call to that line. It should not be possible for it to have assigned all the other cells in the grid. The debug log said the variables were:

i: 0
legend[piece.type].block.length: 4
legend[piece.type].fetch(i, 0, piece.rotate): 0
legend[piece.type].fetch(i, 1, piece.rotate): 0
piece.type: 1
piece.rotate: 3
x: 3
y: 6

Those were the variables that influenced the second grid above. The buggy line of code would have read:

grid[3+0][6+0].type = 1+1;

Is it possible that we uncovered a bug in Java itself? I've spent hours debugging this code and I've never had a bug I couldn't find (I usually code in python, though... Java is fairly new to me)

Also, it seems that the bug is influenced by the size of the grid. If I set the height of the grid to a lower number, it takes less pieces to cause the bug. On the same token, it requires more pieces if I make a taller grid.

Hopefully the Java experts out there can help shed some light on the situation.

p.s. I'm programming in Eclipse on a Mac using JRE JVM 1.6. I also tried JRE JVM 1.4 and the same thing happens.

Additional info:

private Grid grid[][] = new Grid[dimx][dimy];

the above code is when the grid is created. Grid is a class:

public class Grid {
    int type;

    public Grid(int type) {
    }
}

Here is part of the extensive debug log code... This is the function that prints the text-based grid.

public void printGrid(int line) {
    System.out.print("\n\n\n"+line+":\n");
    for (int y = 0;y < grid[0].length;y++) {
        for (int x = 0;x < grid.length;x++) {
            if (grid[x][y].type == 0) {
                System.out.print("[ ]");
            } else {
                System.out.print("["+grid[x][y].type+"]");
            }

        }
        System.out.println();
    }
    System.out.println("\n\n");
}

The variable 'line' is not important and merely tells us what line number the function printGrid() was called from.

Let me know if you guys need any more information.

More Additional Info:

Here are all the files for this program.

Oh, and here is an excerpt from a debug log to give you more info.

http://pastebin.com/9VKhF8CR

It includes placing a piece successfully (the last piece placed before the bug) and then it shows the bug happening. All the variables for the function are printed and none appear out of place. The bug happens to any piece being placed at any spot on the grid. The grid was printed after each call to the line I talked about above. Let me know if you guys need any more info. I want this bug squashed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水波映月 2024-11-15 19:29:00

从问题的描述来看,它看起来就像 grid[0]grid[1]grid[6]< /code> 都是对相同行数组的引用(但 grid[7] 及以后是对不同数组的引用)。这会导致您描述的症状,其中更改单个元素会导致它在所有这些行中出现更改。

尝试:

System.err.println(grid[0] == grid[1]);

看看你得到的是 true 还是 false。您可能会得到true

From the description of your problem, it appears as though grid[0], grid[1] through grid[6] are all references to the same row array (but grid[7] and beyond are references to different arrays). This would cause the symptom you describe, where changing a single element causes it to appear to change in all those rows.

Try:

System.err.println(grid[0] == grid[1]);

and see whether you get true or false. You'll probably get true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文