do {
const randX = randomNumberBetween(0, 9);
const randY = randomNumberBetween(0, 9);
if ((randX === x1 && randY === y1) || (randX === x2 && randY === y2)) {
continue;
}
matrix[randX][randY] = 'Obstacle';
placedBlocks += 1;
} while (placedBlocks < blocks);
matrix[x1][y1] = 'Start';
matrix[x2][y2] = 'Goal';
return matrix;
}
我需要用其他逻辑替换我的continue
语句,因为我的ESLint规则不允许我使用它。
我的整个任务是通过带有块字段的矩阵创建寻路算法,并且一切正常。我只需要用其他东西代替continue;
。
答案 0 :(得分:2)
不是跳过if
条件下的循环,只需反转if
条件并在if
条件下进行工作,
do {
const randX = randomNumberBetween(0, 9);
const randY = randomNumberBetween(0, 9);
if (!((randX === x1 && randY === y1) || (randX === x2 && randY === y2))) {
matrix[randX][randY] = 'Obstacle';
placedBlocks += 1;
}
} while (placedBlocks < blocks);
matrix[x1][y1] = 'Start';
matrix[x2][y2] = 'Goal';
return matrix;
}
答案 1 :(得分:0)
您的条件语句当前检查randX
和randY
坐标对是否与两个点之一重叠。您可以反转代码中的逻辑,在continue
内用if
移动要跳过的块。
反演是检查randX
和randY
是否不重叠(emem)重叠两个点之一,方法是否定先前的有条件条件,或者更干净一点地翻转{{ 1}}至===
和!==
至&&
:
||
这是逻辑对等的有效演示:
do {
const randX = randomNumberBetween(0, 9);
const randY = randomNumberBetween(0, 9);
if ((randX !== x1 || randY !== y1) && (randX !== x2 || randY !== y2)) {
matrix[randX][randY] = 'Obstacle';
placedBlocks += 1;
}
} while (placedBlocks < blocks);
matrix[x1][y1] = 'Start';
matrix[x2][y2] = 'Goal';
return matrix;