1958. Check if Move is Legal (Medium) - TengnanYao/daily_leetcode GitHub Wiki
class Solution:
def checkMove(self, board: List[List[str]], rMove: int, cMove: int, color: str) -> bool:
directions = [(-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1)]
opposite = "B" if color == "W" else "W"
arr = []
for i, j in directions:
x, y = i + rMove, j + cMove
if 0 <= x < 8 and 0 <= y < 8 and board[x][y] == opposite:
arr.append((x, y, i, j))
for x, y, i, j in arr:
while 0 <= x < 8 and 0 <= y < 8:
if board[x][y] == opposite:
x += i
y += j
elif board[x][y] == color:
return True
else:
break
return False