Java 递归方法
我正在做一个砖块破解游戏,不知为什么我被一个讨厌的bug困住了,我无法找到它。游戏目前的状态: 下面是我想做的:假设我点击上图中圈出的红色按钮。我希望红色的砖块消失,红色上面的砖块适当地占据它们的位置 迄今为止的守则:Java 递归方法,java,swing,recursion,jpanel,jbutton,Java,Swing,Recursion,Jpanel,Jbutton,我正在做一个砖块破解游戏,不知为什么我被一个讨厌的bug困住了,我无法找到它。游戏目前的状态: 下面是我想做的:假设我点击上图中圈出的红色按钮。我希望红色的砖块消失,红色上面的砖块适当地占据它们的位置 迄今为止的守则: private void moveBrick(BrickHolder brickHolder) { Point brickHolderLocation = brickHolder.getBrickHolderLocation(); Brick contain
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);
}
从我所做的调试来看,我相信问题就在这里:
if (brickHolderLocation.y == 0) { // Should we be at the top row, there's no need to continue
return;
}
一些关注点:
- Brick-我创建的扩展JButton的类。没别的了。思考 它是一个带有背景的普通按钮
- 砌砖工-一级 创建以承载砖块。这个类扩展了JPanel。唯一的 加法是一个(点)位置变量,为方便起见而添加 操纵
编辑:谢谢大家!您的评论和/或回答为我指明了继续的正确道路 我没有足够的观点来评论,所以这更像是一个建议,但是如果你在最上面一排,你会不会仍然想移除砖块呢?这与您在调试中发现的问题相同。我没有足够的观点来评论,因此这更像是一个建议,但是如果您在最上面一行,您是否仍然希望移除砖块?这与您在调试中发现的问题相同。可能希望将此移到…我不确定我是否理解您的代码逻辑。你做两次传球吗?一个是移除相同颜色的相邻砖块,第二个是重新分配砖块?其逻辑是移除该JPanel中包含的按钮,然后使用上面的按钮将其移动到上一个JPanel,继续这样做,直到它到达最上面一行。你应该分两次这样做——1)首先移除所有相邻的红砖,然后2)在移除所有红砖后重新定位砖。我认为这将为您简化一些事情。为了更好地帮助您,请尽快发布,并可能希望将此移到…我不确定我是否理解您的代码逻辑。你做两次传球吗?一个是移除相同颜色的相邻砖块,第二个是重新分配砖块?其逻辑是移除该JPanel中包含的按钮,然后使用上面的按钮将其移动到上一个JPanel,继续这样做,直到它到达最上面一行。你应该分两次这样做——1)首先移除所有相邻的红砖,然后2)在移除所有红砖后重新定位砖。我认为这将为你简化事情。为了更好地帮助,请尽快发布,因此你建议我在移动JPanel后检查它的位置?我认为你应该使用brickHolder.remove(containedBrick);在if(brickHolder.y==0){return;}之前,您建议我在移动JPanel后检查它的位置是否正确?我认为您应该使用brickHolder.remove(containedBrick);在if(brickHolderLocation.y==0){return;}之前