The 'canReplace' method should be fairly straight-forward. If the horizontal and vertical lists of matching actors are empty, then replace with that random actor:
Now, the last thing we did was create a list of actors that need to be removed and replaced. We need to iterate over those actors and determine a new random tile type for each of their locations. This should do the job:
private boolean canReplace(Tile actor, int x, int y)
{
if (getHorizontalMatchCount(x, y, actor.getClass()) > 1) return false;
if (getVerticalMatchCount(x, y, actor.getClass()) > 1) return false;
return true;
}for (Tile tile : tiles)
{
int x = tile.getX(), y = tile.getY();
Tile actor = null;
while (!canReplace(actor, x, y)) actor = getRandomTile();
addObject(actor, x, y);
}
