Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Xs and Os Referee by MatthiasWiesmann
# migrated from python 2.7
def winner_l(line):
for p in ('X', 'O'):
if p * 3 == line:
return p
return None
def winner(lines):
for line in lines:
w = winner_l(line)
if w:
return w
return None
def diagonal(lines):
d = [
[lines[0][0], lines[1][1], lines[2][2]],
[lines[2][0], lines[1][1], lines[0][2]]
]
return winner(list(map(''.join, d)))
def checkio(game_result):
w = winner(game_result)
if w:
return w
r = list(map(''.join, list(zip(*game_result))))
w = winner(r)
if w:
return w
return diagonal(game_result) or '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"
assert checkio([
"O.X",
"XX.",
"XOO"]) == "X", "Xs wins again"
March 3, 2014
Comments: