I'm trying to write console minesweeper, but cant figure out how to iterate over 2d array, and count amount of "-1" around each of element. My array looks like
[0 0 0 0 0]
[0 0 -1 0 0]
[0 0 0 0 0]
[0 0 0 -1 -1]
[0 0 0 -1 -1]
Where "-1" that is mine. I've tried to simply iterate over it with code
for i := 0; i < row; i++ {
numb := 0
for j := 0; j < col; j++ {
if ary[i-1][j-1] == -1 {
numb ++ }
}
if ary[i-1][j] == 1 {
numb ++ }
//rest of code omitted
}
But that resulted in huge amout of code, which is hard to read and understand. Is there more flexible way, to check all elements around current
in for-iterator
, so in result it would look like
[0 1 1 1 0]
[0 1 -1 1 0]
[0 1 2 2 1]
[0 0 1 -1 -1]
[0 0 1 -1 -1]