Java 检测没有递归或堆栈/队列的组
我试着用这个算法来检测网格上的相似碎片组。这是一个简单的游戏演示,在12x10网格上随机放置棋子,每次放置棋子后,都会检查网格中是否有三个或更多相邻棋子的任何组。我使用下面的代码试图在没有洪水填充、递归或堆栈/队列的情况下实现这一点。它似乎几乎可以工作,但有时会破坏不属于同一类型的方块,或者留下应该被破坏的方块。那么,是算法的逻辑错误,还是实现/编码错误 编辑:我想现在可以了。见评论Java 检测没有递归或堆栈/队列的组,java,pattern-matching,iteration,flood-fill,Java,Pattern Matching,Iteration,Flood Fill,我试着用这个算法来检测网格上的相似碎片组。这是一个简单的游戏演示,在12x10网格上随机放置棋子,每次放置棋子后,都会检查网格中是否有三个或更多相邻棋子的任何组。我使用下面的代码试图在没有洪水填充、递归或堆栈/队列的情况下实现这一点。它似乎几乎可以工作,但有时会破坏不属于同一类型的方块,或者留下应该被破坏的方块。那么,是算法的逻辑错误,还是实现/编码错误 编辑:我想现在可以了。见评论 public void checkMatches(int type) { /* * Step
public void checkMatches(int type)
{
/*
* Step 1: Iterate through each square to see how many of the same type are adjacent to it
*/
for (int i = 0; i < PIECES_WIDE; i++)
{
for (int j = 0; j < PIECES_TALL; j++)
{
if (grid[i][j].getType() == type) // EDITED IN CODE. Make sure current square is of correct type
{
if (i > 0) // Bounds checking
if (grid[i - 1][j].getType() == type)
grid[i][j].setAdj(grid[i][j].getAdj() + 1);
if (i < PIECES_WIDE - 1) // Bounds checking
if (grid[i + 1][j].getType() == type)
grid[i][j].setAdj(grid[i][j].getAdj() + 1);
if (j > 0) // Bounds checking
if (grid[i][j - 1].getType() == type)
grid[i][j].setAdj(grid[i][j].getAdj() + 1);
if (j < PIECES_TALL - 1) // Bounds checking
if (grid[i][j + 1].getType() == type)
grid[i][j].setAdj(grid[i][j].getAdj() + 1);
}
}
}
/*
* Step 2: If there are 2 or more adjacent squares with the same type then it is part of a blob and to be destroyed
*/
for (int i = 0; i < PIECES_WIDE; i++)
{
for (int j = 0; j < PIECES_TALL; j++)
{
if (grid[i][j].getAdj() >= 2)
grid[i][j].setDestroy(true);
}
}
/*
* Step 3: If there is only 1 adjacent, then check to see if any adjacent squares have been marked to be destroyed (part
* of a group). If so, set these to be destroyed as well.
*/
for (int i = 0; i < PIECES_WIDE; i++)
{
for (int j = 0; j < PIECES_TALL; j++)
{
if (grid[i][j].getAdj() == 1)
{
if (i > 0) // Bounds checking
if (grid[i - 1][j].isDestroy() == true)
{
grid[i][j].setDestroy(true);
break;
}
if (i < PIECES_WIDE - 1) // Bounds checking
if (grid[i + 1][j].isDestroy() == true)
{
grid[i][j].setDestroy(true);
break;
}
if (j > 0) // Bounds checking
if (grid[i][j - 1].isDestroy() == true)
{
grid[i][j].setDestroy(true);
break;
}
if (j < PIECES_TALL - 1) // Bounds checking
if (grid[i][j + 1].isDestroy() == true)
{
grid[i][j].setDestroy(true);
break;
}
}
}
}
/*
* Step 4: Iterate through grid and destroy the squares marked for destruction and reset all squares to 0 adjacent and
* destroy flag to false
*/
for (int i = 0; i < PIECES_WIDE; i++)
{
for (int j = 0; j < PIECES_TALL; j++)
{
if (grid[i][j].isDestroy())
destroyPiece(grid[i][j]);
grid[i][j].setAdj(0);
grid[i][j].setDestroy(false);
}
}
}
public void checkMatches(int类型)
{
/*
*第1步:遍历每个方块,看看有多少相同类型的方块与之相邻
*/
对于(int i=0;i<片宽;i++)
{
对于(int j=0;j<1;j++)
{
if(grid[i][j].getType()==type)//在代码中编辑。请确保当前正方形的类型正确
{
if(i>0)//边界检查
if(grid[i-1][j].getType()==type)
grid[i][j].setAdj(grid[i][j].getAdj()+1);
if(i0)//边界检查
if(grid[i][j-1].getType()==type)
grid[i][j].setAdj(grid[i][j].getAdj()+1);
if(j=2)
网格[i][j].setDestroy(true);
}
}
/*
*第3步:如果只有1个相邻方块,则检查是否有任何相邻方块被标记为要销毁(零件号
*如果是的话,也将其设置为销毁。
*/
对于(int i=0;i<片宽;i++)
{
对于(int j=0;j<1;j++)
{
if(grid[i][j].getAdj()==1)
{
if(i>0)//边界检查
if(grid[i-1][j].isDestroy()==true)
{
网格[i][j].setDestroy(true);
打破
}
if(i0)//边界检查
if(grid[i][j-1].isDestroy()==true)
{
网格[i][j].setDestroy(true);
打破
}
if(j
将项目排序到桶中时,您可以更快地找到组。您可以使用空间索引,例如kd树。我感谢您的建议。对于这个例子,我不太关心速度,但我现在就来看看,谢谢。我意识到我在初始迭代中从来没有检查平方,以确保它也是传递类型的参数。加上这些之后,它似乎起作用了!