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 Rounin
def checkio(game_result):
# Check rows
for row in game_result:
if row == 'XXX':
return 'X'
elif row == 'OOO':
return 'O'
# Check columns
for col in range(len(game_result)):
c = ''
for row in game_result:
c += row[col:col+1]
if c == 'XXX':
return 'X'
elif c == 'OOO':
return 'O'
# Check diagonals
d1 = ''
d2 = ''
for d in range(len(game_result)):
d1 += game_result[d][d:d+1]
d2 += game_result[d][len(game_result)-1-d:len(game_result)-d]
if d1 == 'XXX':
return 'X'
elif d1 == 'OOO':
return 'O'
elif d2 == 'XXX':
return 'X'
elif d2 == 'OOO':
return 'O'
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"
July 19, 2017