I've been working on a tetris clone to learn openGL but I've ran into a nasty bug. I've been staring at it for 2 days already and I just can't figure it out. So I've decided to post here and hope someone can figure it out.
When I start a game everything works nicely , but when I've gone through 20 or so blocks things start to mess up. It seems the program is filling my array of blocks with random blocks for some unknown reason.
I *think* I've pinned down the problem to this part of my code in Game.java
Sprite parts[]=currentBlock.getParts();
for(int i=0;i<parts.length;i++)
{
//determin row and column of part
int row=(((int)parts[i].location.y)/32)-1;
int column=(((int)parts[i].location.x)-SCREEN_BORDER_LEFT)/32-1;
rowsOfBlocks[row][column]=parts[i];
}
now this only should set one cell of the array to that block, but somehow it fills up the cells directly above it aswell.
I get the following output:
Block 23
X
XXXXXXX
XXXXXXX
XXXXXXXX
XX XXXXXXX
Block 24
XX
XX
XX
XX
XX
XX
XX
XXX
XXXXXXX
XXXXXXX
XXXXXXXX
XX XXXXXXX
as you can see everything is normal till we reach block 24.
It doesn't matter if I make a row or not (note in this example that we haven't made a row), and I've already checked out the function that clears the rows.
I have no idea what the problem could be, and I'd be grateful if any of you could test it, and take a look at the code.
Only Game.java should be of interest.
Note that this game isn't finished yet: rotation needs to be changed, no loss checking etc. art is just temp aswell :)
You can find the code here:
http://users.pandora.be/piele/tetris.zip
you might be better off posting this to the javagaming boards (JGO):
http://www.javagaming.org/cgi-bin/JGNetForums/YaBB.cgi
Thanks for the link. I knew this was more a general question, but this is the only java game programming forum that I know :)
Anyway I found out what the problem is and I think it's a bug in the JVM.
I've optimised some of my code and I noticed that I could get around 30 blocks in before it happened instead of 20.
The problem happened when I added some parts to the array, Instead of adding just 1 part it filled up the the cells above it with random Sprite objects aswell. And it even altered the data of some that were already in the array, making them disappear from the screen.
At first I thought my RAM was going bad, but when tested on my brand new laptop with 512mb RAM it still did it (eventhough I got further on it than on my PC: 30 versus 20 on my comp).
So anyway, the problem was this line of code:
//move the rows above this one down
for(int i=row;i+1<rowsOfBlocks.length;i++)
{
rowsOfBlocks[i]=rowsOfBlocks[i+1];
}
When I changed it to this
//move the rows above this one down
for(int i=row;i+1<rowsOfBlocks.length;i++)
{
for(int j=0;j<rowsOfBlocks[0].length;j++)
rowsOfBlocks[i][j]=rowsOfBlocks[i+1][j];
}
The problem went away.
So is this a bug in the JVM or am I missing something really obvious? I've never had something like this happen before.
Don't worry, it's just a logic error on your part :) No VM bug.
Cas :)