NxN array check neighbors cells

95 views Asked by At

I have an NxN array (t_field), with cells that are filled with either 0 or 1.

I want to check if any neighbors of a Entity in range is filled or not.

Entity is a sequence of 1's that have a (row, start), (row, end), and I want to check the row above, the row bellow, but within the constrains of start-1 and end + 1:

contacts = [t_field[row_n - 1][start-1:end+2], 
            t_field[row_n + 1][start-1:end+2]]

But if I'm at row 0 I don't want it to check row-1, since this would look at the end of the grid. in that case it would only check row 1; or vice versa, if row is n(the size of the grid), it would trow and IndexError when checking row+1

The same goes for start/end, if the start is 0 I don't want to check -1, since it would look at the opposite side of the grid. I just want check end+2. Or vice versa.

start = column_n - length + 1
end = column_n

upperrow = row_n - 1
lowerrow = row_n + 1

if upperrow < 0:
    upperrow = None
if lowerrow >= len(t_field):
    lowerrow = None

leftside = start - 1
rightside = end + 2
if leftside < 0:
    leftside = None
if rightside >= len(t_field):
    rightside = None

if upperrow is not None and lowerrow is not None:
    if leftside is not None and rightside is not None:
        contacts = [t_field[upperrow][leftside:rightside],
                    t_field[lowerrow][leftside:rightside]]
    elif leftside is None and rightside is not None:
        contacts = [t_field[upperrow][0:rightside],
                    t_field[lowerrow][0:rightside]]
    elif leftside is not None and rightside is None:
        contacts = [t_field[upperrow][leftside:end + 1],
                    t_field[lowerrow][leftside:end + 1]]
elif upperrow is None and lowerrow is not None:
    if leftside is not None and rightside is not None:
        contacts = [t_field[lowerrow][leftside:rightside]]
    elif leftside is None and rightside is not None:
        contacts = [t_field[lowerrow][0:rightside]]
    elif leftside is not None and rightside is None:
        contacts = [t_field[lowerrow][leftside:end + 1]]
elif upperrow is not None and lowerrow is None:
    if leftside is not None and rightside is not None:
        contacts = [t_field[upperrow][leftside:rightside]]
    elif leftside is None and rightside is not None:
        contacts = [t_field[upperrow][0:rightside]]
    elif leftside is not None and rightside is None:
        contacts = [t_field[upperrow][leftside:end + 1]]

if any(itertools.chain(*contacts)):
   return True

This works fine, but it's utterly ugly. Very repetitive code, and very hard to read/understad what's going on.

As you can see, there's 3 lines that is what I want, and at least 30 that are what I had to write to make those 3 lines work.

Grid

In this image I show What I'm looking for. Entity is the gray area, the neighbors are the red area. I want to check if the neighbors are filled or not.

What's the proper way to get those neighbors?

1

There are 1 answers

1
Eric Levieil On BEST ANSWER

Assuming you put your code in a function:

 leftside = max(0, start -1)
 rightside = min(end + 1, len(t_field) - 1)

 if row >= 1:
     if any(t_field[row -1][leftside:rightside]): return True
 if row < len(t_field) - 1:
     if any(t_field[row + 1][leftside:rightside]): return True
 return False