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 kendriu
def checkio(game_result):
def horizontal(gr, player):
return any((r.count(player)==3 for r in gr))
def vertical(gr, player):
for i in range(3):
col = (row[i]==player for row in gr)
if all(col):
return True
def diagonal(gr, player):
diag1 = []
diag2 = []
for i,row in enumerate(gr):
diag1.append(row[i]== player)
diag2.append(row[2-i]== player)
return any((
all(diag1),
all(diag2),
))
def winner(gr, player):
return any((
vertical(gr, player),
horizontal(gr, player),
diagonal(gr, player),
))
if winner(game_result, 'X'):
return 'X'
elif winner(game_result, 'O'):
return 'O'
else:
return 'D'
if __name__ == '__main__':
#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"
Aug. 30, 2014