Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Simple solution solution in Clear category for Xs and Os Referee by wnormandin
def checkio(game_result):
g = game_result
# Define win conditions
wins = [
((0,0),(1,0),(2,0)),
((0,1),(1,1),(2,1)),
((0,2),(1,2),(2,2)),
((0,0),(0,1),(0,2)),
((1,0),(1,1),(1,2)),
((2,0),(2,1),(2,2)),
((0,0),(1,1),(2,2)),
((0,2),(1,1),(2,0))
]
# Loop through the players
for l in ['X','O']:
# Check each set of winning coordinates
for win in wins:
# Sum the number of characters in those coordinates
# which match the player's letter
s = sum([1 for cond in win if g[cond[0]][cond[1]]==l])
# If all coordinates in a win condition match, the sum will
# be three, return this letter and break
if s == 3: return l
# If both players and all win conditions are checked
# without a winner, the game was a draw
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"
Dec. 9, 2015