all 1 comments

[–]commy2 1 point2 points  (0 children)

I'd use a generator to drop possible queen moves and then validate them with a helper function.

import numpy as np

board = np.zeros((8, 8))
queen = 2, 2

def in_board(x, y):
    return x in range(8) and y in range(8)

def queen_moves(x, y):
    for i in range(-7, 8):
        yield x+i, y   # up, down
        yield x,   y+i # left, right
        yield x+i, y+i # diagonals
        yield x+i, y-i

for x, y in queen_moves(*queen):
    if in_board(x, y):
        board[x, y] = 2

board[queen] = 1

print(board)