Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Using "array of bits". solution in Clear category for Xs and Os Referee by kaniabi
from typing import List
# Victory conditions stores all valid victory conditions as integers. We build
# these using an array of bits.
VICTORY_CONDITIONS = [
'100100100',
'010010010',
'001001001',
'111000000',
'000111000',
'000000111',
'100010001',
'001010100',
]
VICTORY_CONDITIONS = [int(i, 2) for i in VICTORY_CONDITIONS]
def player_as_int(game_result: str, player: str) -> int:
"""
Returns an integer representing the position of the given player as an array
of bits.
"""
return int(''.join(['1' if i == player else '0' for i in game_result]), 2)
def check_victory(game_result: str, player: str) -> bool:
"""
Checks if the given game_result matches any victory conditions for the given
player.
"""
c = player_as_int(game_result, player)
for v in VICTORY_CONDITIONS:
if (c & v) == v:
return True
return False
def checkio(game_result: List[str]) -> str:
game_result = ''.join(game_result)
if check_victory(game_result, 'X'):
return 'X'
if check_victory(game_result, 'O'):
return 'O'
return 'D'
June 30, 2018