-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm794.py
49 lines (41 loc) · 1.49 KB
/
m794.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
38
39
40
41
42
43
44
45
46
47
48
49
class Solution:
def validTicTacToe(self, board: List[str]) -> bool:
xes, oes = 0, 0
xWin, oWin = False, False
# checking rows and getting number of each move
for row in board :
if all(x == 'X' for x in row) :
xWin = True
elif all(x == 'O' for x in row) :
oWin = True
xes += row.count('X')
oes += row.count('O')
# checking columns
for i in range(len(board[0])) :
if all(board[x][i] == 'X' for x in range(len(board))) :
xWin = True
if all(board[x][i] == 'O' for x in range(len(board))) :
oWin = True
# checking diagonals
if all(board[x][x] == 'X' for x in range(len(board))) :
xWin = True
if all(board[x][x] == 'O' for x in range(len(board))) :
oWin = True
if all(board[x][len(board) - x - 1] == 'X' for x in range(len(board))) :
xWin = True
if all(board[x][len(board) - x - 1] == 'O' for x in range(len(board))) :
oWin = True
# only one can win
if (xWin == True and oWin == True) :
return False
# X goes first
if not (1 >= (xes - oes) >= 0) :
return False
# O can't place after X wins
if xes == oes and xWin :
return False
# X can't place after O wins
if oWin and xes > oes :
return False
# Valid board
return True