Can't spot the bug in my Java Tetris game
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.
- Legend.java: http://pastebin.com/mWL7wACW
- Listener.java: http://pastebin.com/8A2BgicD
- TetrisFrame.java: http://pastebin.com/1HKgc1uS (90% of the code is here)
- Grid.java: http://pastebin.com/6HgKZsaF
- Tetris.java: http://pastebin.com/kHdnvHyT
- Piece.java: http://pastebin.com/k5PZpCzd
Oh, and here is an excerpt from a debug log to give you more info.
http://pastebin.com/9VKhF8CR
It includes placing a piece succ开发者_如何学Goessfully (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.
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
.
精彩评论