I have a matrix filled with * and -
, where *
represents a virus and -
a free spot for a virus, I have to check in my matrix the neighbours of every virus, a valid neighbour is another virus and not a free spot, in order to establish their number. The neighbours I have to check are [row + 1][col]
, [row - 1][col]
, [row][col + 1]
and [row][col - 1]
, in total four neighbours. I made a function to check and cover all the cases, for example if the element that I am checking is one of the corners of the matrix. I came up with a really long 80 lines function that has a lot of if
statements.
Is there an efficient way(meaning the number of lines) to check all this besides having to write like 20 if
statements?
https://pastebin.com/2f7YpreZ Here is the code I've written