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 Moff
def check_list(array):
if all(x == 'X' for x in array):
return 1
elif all(x == 'O' for x in array):
return 2
else:
return 0
def winner(field):
result = 0
for row in range(3):
result = max(result, check_list([field[row][col] for col in range(3)]))
for col in range(3):
result = max(result, check_list([field[row][col] for row in range(3)]))
result = max(result, check_list([field[i][i] for i in range(3)]))
result = max(result, check_list([field[i][2 - i] for i in range(3)]))
return result
def checkio(game_result):
return {1: 'X', 2: 'O', 0: 'D'}.get(winner(game_result))
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"
July 16, 2015