I am making a BrickBreaker game, and for an unknown reason I am stuck with an annoying bug, that I am unable to track down. The game’s state so far:

Here is what I want to do: Suppose I click on the Red button circled in the image above. I want the red bricks to disappear, and the ones above red to take their position appropriately.
The code so far:
private void moveBrick(BrickHolder brickHolder) {
Point brickHolderLocation = brickHolder.getBrickHolderLocation();
Brick containedBrick = getBrickByXAndY(brickHolderLocation.x, brickHolderLocation.y); // getting the Brick at that location
if (containedBrick == null) {
// If in any case there should be no brick at that position, just go on with the Brick above
if (brickHolderLocation.y == 0) { // Should we be at the top row, there's no need to continue
return;
} else {
BrickHolder nextBrickHolder = getPanelByXAndY(brickHolderLocation.x, brickHolderLocation.y - 1);
moveBrick(nextBrickHolder);
}
}
if (brickHolderLocation.y == 0) { // Should we be at the top row, there's no need to continue
return;
}
// Removing the current Contained Brick
brickHolder.remove(containedBrick);
// Getting the Brick I want to move, normally hosted at the above Panel
Brick theOneToBeMoved = getBrickByXAndY(brickHolderLocation.x, brickHolderLocation.y - 1);
if (theOneToBeMoved == null) {
// If in any case the Panel above doesn't contain a Brick, then continue with the Panel above.
BrickHolder nextBrickHolder = getPanelByXAndY(brickHolderLocation.x, brickHolderLocation.y - 1);
moveBrick(nextBrickHolder);
}
// Getting the Panel above the current one, so that we may move the Brick hosted there,
// To the current Panel
BrickHolder toHoldTheNewBrick = getPanelByXAndY(brickHolderLocation.x, brickHolderLocation.y - 1);
brickHolder.add(theOneToBeMoved); // Moving the Brick at the current Panel
toHoldTheNewBrick.remove(theOneToBeMoved); // Removing that same brick from the Panel above
theOneToBeMoved.setBrickLocation(brickHolderLocation); // Setting the Brick's new location.
// Since we have gotten so far, we assume that everything worked perfectly and that it's time to continue
// with the Panel above
BrickHolder theNextOne = getPanelByXAndY(brickHolder.getBrickHolderLocation().x, brickHolder.getBrickHolderLocation().y - 1);
moveBrick(theNextOne);
}
From the debugging I did, I believe the issue is here somewhere:
if (brickHolderLocation.y == 0) { // Should we be at the top row, there's no need to continue
return;
}
Some points of interest:
- Brick – A class I created that Extends JButton. Nothing more. Think
of it as a plain JButton with a BackGround - BrickHolder – A class I
created to host the Bricks. This class extends JPanel. The only
addition is a (Point) location variable, added for easier
manipulation.
EDIT: Thank you everyone! Your comments and/or answers showed me the right path to continue!
I do not have enough points to comment so this is more of a suggestion, but if you were on the top row, wouldn’t you still want to remove the brick regardless? That would be in the same area as you identified as being problematic in your debugging.