Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Xs and Os Referee by natsuki
def checkio(game_result):
def get(p):
"""
p is position
0 1 2
3 4 5
6 7 8
"""
return game_result[p % 3][p // 3]
def get_line_winner(p1, p2, p3):
if get(p1) == get(p2) == get(p3) != '.':
return get(p1) # 'X' or 'O'
else:
return None
lines = [
# horizontal
(0, 1, 2), (3, 4, 5), (6, 7, 8),
# vertical
(0, 3, 6), (1, 4, 7), (2, 5, 8),
# diagonal
(0, 4, 8), (2, 4, 6)]
winners = {get_line_winner(*l) for l in lines} - {None}
if len(winners) == 1: # {'X'} or {'O'}
return winners.pop()
elif len(winners) == 2: # {'X', 'O'}
# such as ['XXX','OOO','XOX']
return 'D' # or raise exception
else:
return 'D'
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio([
"X.O",
"XX.",
"XOO"]) == "X", "Xs wins"
assert checkio([
"OO.",
"XOX",
"XOX"]) == "O", "Os wins"
assert checkio([
"OOX",
"XXO",
"OXX"]) == "D", "Draw"
Feb. 16, 2014
Comments: