forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind Winner on a Tic Tac Toe Game.py
37 lines (33 loc) · 1.04 KB
/
Find Winner on a Tic Tac Toe Game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution:
def tictactoe(self, moves: List[List[int]]) -> str:
wins = [
[(0, 0), (0, 1), (0, 2)],
[(1, 0), (1, 1), (1, 2)],
[(2, 0), (2, 1), (2, 2)],
[(0, 0), (1, 0), (2, 0)],
[(0, 1), (1, 1), (2, 1)],
[(0, 2), (1, 2), (2, 2)],
[(0, 0), (1, 1), (2, 2)],
[(0, 2), (1, 1), (2, 0)],
]
def checkWin(S):
for win in wins:
flag = True
for pos in win:
if pos not in S:
flag = False
break
if flag:
return True
return False
A, B = set(), set()
for i, (x, y) in enumerate(moves):
if i % 2 == 0:
A.add((x, y))
else:
B.add((x, y))
if checkWin(A):
return 'A'
elif checkWin(B):
return 'B'
return "Draw" if len(moves) == 9 else "Pending"