c-继续声明的替代方案



我正在寻找一种方法来替换此函数中的continue语句。内部规则规定不能使用它们,但我很难实现一个不会导致其他代码错误运行的替换。

bool neighCheck (int i, int j, int a[][COLLENGTH])
{
bool neighbourOnFire;
int x, y, neighX, neighY, curreNeigh;
/* Bool set up to change after neighbours looped*/
neighbourOnFire = false;
/* The neighbours -looping from -1 -> 1 to get index of each neighbour*/
for (x = -1; x < 2; x++) {
for (y = -1; y < 2; y++) {
/* Disregards current (middle) cell*/
if ((x == 0) && (y == 0)) {
continue;
}
/* Get indexes of the neighbour we're looking at */
neighX = i + x;
neighY = j + y;
/* Checks for edges*/
if (neighX >= 0 && neighY >= 0 && neighX < ROWLENGTH
&& neighY < COLLENGTH) {
/* Get the neighbour using the indexes above */
curreNeigh = a[neighX][neighY];
/* Test to see if the neighbour is burning */
if (curreNeigh == fire) {
neighbourOnFire = true;
continue;
}
}
}
}
return neighbourOnFire;
}

第一个continue;可以通过反转条件并将其余代码放入if语句来替换。

第二个CCD_ 3可以简单地移除,因为在那之后没有要执行的代码。

bool neighCheck (int i, int j, int a[][COLLENGTH])
{
bool neighbourOnFire;
int x, y, neighX, neighY, curreNeigh;
/* Bool set up to change after neighbours looped*/
neighbourOnFire = false;
/* The neighbours -looping from -1 -> 1 to get index of each neighbour*/
for (x = -1; x < 2; x++) {
for (y = -1; y < 2; y++) {
/* Disregards current (middle) cell*/
if (!((x == 0) && (y == 0))) {
/* Get indexes of the neighbour we're looking at */
neighX = i + x;
neighY = j + y;
/* Checks for edges*/
if (neighX >= 0 && neighY >= 0 && neighX < ROWLENGTH
&& neighY < COLLENGTH) {
/* Get the neighbour using the indexes above */
curreNeigh = a[neighX][neighY];
/* Test to see if the neighbour is burning */
if (curreNeigh == fire) {
neighbourOnFire = true;
}
}
}
}
}
return neighbourOnFire;
}

最新更新