私はマインスイーパを練習用に作り直しており、IndexOutOfBounds エラーを回避するためにこのコードを書きました。これを回避する方法はありますか? if ステートメントを明示的に書き出す必要はありません。各配列のインデックスを 2 つ大きくし、最初と最後のインデックスを無視することを考えました。明らかな何かが欠けていますか?
if (row > 0 && col > 0)
ray[row - 1][col - 1] += 1;
if (row > 0)
ray[row - 1][col] += 1;
if (row > 0 && col < height - 1)
ray[row - 1][col + 1] += 1;
if (col > 0)
ray[row][col - 1] += 1;
if (col < height - 1)
ray[row][col + 1] += 1;
if (row < width - 1 && col > 0)
ray[row + 1][col - 1] += 1;
if (row < width - 1)
ray[row + 1][col] += 1;
if (row < width - 1 && col < height - 1)
ray[row + 1][col + 1] += 1;