Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Processing flat string and positions in it solution in Clear category for Xs and Os Referee by assargin
from typing import List, Tuple
# prepair all available symbols positions in 3x3 field to check for winner
game_field_positions = list(range(0, 9))
win_plans = [
# diagonals
[0, 4, 8],
[2, 4, 6],
]
for n in range(0, 3):
# add column
win_plans.append(game_field_positions[n::3])
# add row
win_plans.append(game_field_positions[n*3:n*3+3])
def get_first_winner(string_results: str, available_winners: Tuple[str] = ('X', 'O')) -> str:
for plan in win_plans:
symbols = set([string_results[n] for n in plan])
if len(symbols) == 1:
winner = symbols.pop()
if winner in available_winners:
return winner
return None
def checkio(game_result: List[str]) -> str:
return get_first_winner(''.join(game_result)) or '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"
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
Feb. 19, 2020