如何制作嵌套IF语句的For循环?

时间:2014-01-18 13:42:34

标签: actionscript-3

我正在尝试为我正在练习编码的Bejeweled克隆中的这个嵌套if语句创建一个for循环。正如你所看到的那样,这样做非常愚蠢,因为我想循环更多的匹配,所以我想知道是否有一种更有效的方式,而不是通过并使更愚蠢如果语句嵌套约7由于网格是8乘8,每个方向的时间和。

if(i!== 0)位用于防止出现空错误。我认为如果要使用For语句,它可以是if(i< loopvar)而不是

谢谢:3

            if (i !== 0 && map[i][j].name == map[i-1][j].name) // this nest of if statements are gonna check the one to the left of the jewel we are looping through, and see if they match.
                {
                    map[i][j].jewelsWest++;
                    if ( i !== 1 && map[i-1][j].name == map[i-2][j].name)
                    {
                        map[i][j].jewelsWest++;
                    }
            }

1 个答案:

答案 0 :(得分:1)

在这种情况下,我觉得递归可能很方便:

map[i][j].jewelsWest = countMe(i, j, -1, 0);
map[i][j].jewelsEast = countMe(i, j, 1, 0);
map[i][j].jewelsNorth = countMe(i, j, 0, -1);
map[i][j].jewelsSouth = countMe(i, j, 0, 1);


private function countMe(x, y, xDiff, yDiff):int
{
    if(map[x+xDiff] && map[x+xDiff][y+yDiff] && map[x][y].name == map[x+xDiff][y+yDiff].name)
    {
        return 1 + countMe(x+xDiff, y+yDiff, xDiff, yDiff);
    }
    else
    {
        return 0;
    }
}
相关问题