python-checkio/Home/x-o-referee.py
2019-12-18 17:30:47 +01:00

71 lines
1.9 KiB
Python

from typing import List
def checkio(game_result: List[str]) -> str:
h = horizontal(game_result)
v = vertical(game_result)
d = diagonal(game_result)
if h != "D":
return h
if v != "D":
return v
if d != "D":
return d
return "D"
def horizontal(game_result: List[str]) -> str:
for row in game_result:
cells = list(row)
cells.sort()
if cells[0] == '.':
continue
if cells[0] == cells[-1:][0]:
return cells[0]
return "D"
def vertical(game_result: List[str]) -> str:
new_game_result = []
for i in range(len(game_result)):
new_game_result.append(game_result[0][i] + game_result[1][i] + game_result[2][i])
return horizontal(new_game_result)
def diagonal(game_result: List[str]) -> str:
if game_result[1][1] == '.':
return "D"
if game_result[0][0] == game_result[1][1] and game_result[1][1] == game_result[2][2]:
return game_result[1][1]
if game_result[0][2] == game_result[1][1] and game_result[1][1] == game_result[2][0]:
return game_result[1][1]
return "D"
if __name__ == '__main__':
print("Example:")
print(checkio(["X.O",
"XX.",
"XOO"]))
# These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio([
"X.O",
"XX.",
"XOO"]) == "X", "Xs wins"
assert checkio([
"OO.",
"XOX",
"XOX"]) == "O", "Os wins"
assert checkio([
"OOX",
"XXO",
"OXX"]) == "D", "Draw"
assert checkio([
"O.X",
"XX.",
"XOO"]) == "X", "Xs wins again"
assert checkio([".O.", "XXX", ".O."]) == "X", "Xs wins again"
assert checkio(["...", "XXX", "OO."]) == "X", "Xs wins again"
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")