Algorithm to find all legal moves in Reversi game Reversi Game:
The image from the Reversi game:

I have my algorithm to find all legal moves in Reversi game.
Here's the code with comments:
&quot;&quot;&quot;
-1    =&gt; no disc
0/1   =&gt; player's disc
&quot;&quot;&quot;

for row_idx, row in enumerate(board):
    for column_idx, column in enumerate(board):
        if board[row_idx][column_idx] == -1:
            &quot;&quot;&quot;
            Loop through square with chosen `-1` in middle
            5   5   5
            5  -1   5
            5   5   5
            &quot;&quot;&quot;
            for i in range(-1, 2, 1):
                for j in range(-1, 2, 1):
                    &quot;&quot;&quot;
                    Check if loop is not beyond borders
                    &quot;&quot;&quot;
                    if row_idx + i &lt; 0 or row_idx + 1 &gt; (len(board) - 1) or column_idx + j &lt; 0 or column_idx + j &gt; (len(board) - 1):
                        continue

                    &quot;&quot;&quot;
                    If there is a opponent disc, go straight in this direction (that way)
                    &quot;&quot;&quot;
                    if board[row_idx + i][column_idx + j] == self.opponent_color:
                        i2, j2 = i, j
                        for _ in range(8):
                            &quot;&quot;&quot;
                            Check if loop is not beyond borders
                            &quot;&quot;&quot;
                            if row_idx + i + i2 &lt; 0 or row_idx + i + i2 &gt; (len(board) - 1) or column_idx + j + j2 &lt; 0 or column_idx + j + j2 &gt; (len(board) - 1):
                                continue

                            &quot;&quot;&quot;
                            Break if the `no disc` is on `that way`
                            &quot;&quot;&quot;
                            if board[row_idx + i + i2][column_idx + j + j2] == -1:
                                break

                            &quot;&quot;&quot;
                            The legal move has been found, then break.
                            &quot;&quot;&quot;
                            if board[row_idx + i + i2][column_idx + j + j2] == self.my_color:
                                legal_moves.append((row_idx, column_idx))
                                break
           
                            i2 += i
                            j2 += j

Is there any better way to do it? I am not sure about those nested for loops.
Thanks for any help or any hint!
Iteration
This:
for row_idx, row in enumerate(board):
    for column_idx, column in enumerate(board):
        if board[row_idx][column_idx] == -1:

is effectively incorrect. Is your board in row-major or column-major order? Whichever is true, you cannot iterate over the outer index of the board and get both a row and a column. What you likely meant instead is:
for row_idx, row in enumerate(board):
    for column_idx, cell in enumerate(row):
        if cell == -1:

Another problematic iteration is this:
i2, j2 = i, j
for _ in range(8):
    # ...
    i2 += i
    j2 += j

should simply be
for delta in range(1, 8):
    # ... use i*delta instead of i2
    # ... use j*delta instead of j2

Combined range checking
if row_idx + i &lt; 0 or row_idx + 1 &gt; (len(board) - 1) or column_idx + j &lt; 0 or column_idx + j &gt; (len(board) - 1):
                

should be
if not (0 &lt;= row_idx + i &lt; len(board)) or not (0 &lt;= column_idx &lt; len(board)):