Check if Move is Legal LeetCode solution in Python

1
# Runtime: 82 ms (Top 35.62%) | Memory: 14 MB (Top 54.79%)
2
class Solution:
3
def checkMove(
4
self, board: List[List[str]], rMove: int, cMove: int, color: str
5
) -> bool:
6
directions = [False] * 8
7
moves = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]
8
opposite_color = "W" if color == "B" else "B"
9

10
for d in range(8):
11
r, c = rMove + moves[d][0], cMove + moves[d][1]
12
if 0 <= r < 8 and 0 <= c < 8 and board[r][c] == opposite_color:
13
directions[d] = True
14

15
for step in range(2, 8):
16
if not any(d for d in directions):
17
return False
18
for d in range(8):
19
if directions[d]:
20
r, c = rMove + step * moves[d][0], cMove + step * moves[d][1]
21
if 0 <= r < 8 and 0 <= c < 8:
22
if board[r][c] == color:
23
return True
24
elif board[r][c] == ".":
25
directions[d] = False
26
else:
27
directions[d] = False
28
return False

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0