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 AdamToth
def checkio(game_result):
#check rows
for row in game_result:
if row.count('X') == 3:
return 'X'
if row.count('O') == 3:
return 'O'
#check cols
#rotate the result
game_result = list(zip(*game_result))
for row in game_result:
if row.count('X') == 3:
return 'X'
if row.count('O') == 3:
return 'O'
#check diags
diags = [game_result[0][0]+game_result[1][1]+game_result[2][2],
game_result[0][2]+game_result[1][1]+game_result[2][0]]
for row in diags:
if row.count('X') == 3:
return 'X'
if row.count('O') == 3:
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"
Jan. 30, 2015
Comments: