-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathp16_4.py
71 lines (55 loc) · 1.78 KB
/
p16_4.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from typing import List, Optional
from enum import Enum
class Dir(Enum):
LEFT = (0, -1)
RIGHT = (0, 1)
UP = (-1, 0)
DOWN = (1, 0)
UPRIGHT = (-1, 1)
UPLEFT = (-1, -1)
DOWNRIGHT = (1, 1)
DOWNLEFT = (1, -1)
def check_in_dir(m: List[List[chr]],
start_poz: tuple,
direction: Dir) -> Optional[chr]:
sr, sc = start_poz
dr, dc = direction.value
winner_sign = m[sr][sc]
# print(f"Checking from {start_poz}, going in dir {dr} {dc}, {winner_sign}")
# If not complete on this direction, no winner
if not winner_sign:
return None
row = sr
col = sc
while 0 <= row < len(m) and 0 <= col < len(m[0]):
elem_now = m[row][col]
if elem_now != winner_sign:
return None
row += dr
col += dc
return winner_sign
def check_xo(board: List[List[chr]]):
rows = len(board)
cols = len(board[0])
row_winners = [check_in_dir(board, (row, 0), Dir.RIGHT)
for row in range(rows)]
col_winners = [check_in_dir(board, (0, col), Dir.DOWN)
for col in range(cols)]
diag_winners = [check_in_dir(board, (0, 0), Dir.DOWNRIGHT),
check_in_dir(board, (0, cols-1), Dir.DOWNLEFT)]
# print(f"Row winners {row_winners}, "
# f"col {col_winners} and diags: {diag_winners}")
possible_winners = set(x for x in row_winners +
col_winners + diag_winners if x)
if len(possible_winners) > 1:
raise Exception("Contested board detected")
else:
return list(possible_winners)[0]
if __name__ == "__main__":
board = [
['x', 'o', 'o'],
['o', 'o', 'o'],
['x', 'o', 'x']
]
winrar = check_xo(board)
print(f"Winner of the board is {winrar}")