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 tartavull
def checkio(game_result):
coords = []
#Add rows
coords.append( ((0,0),(0,1),(0,2)) )
coords.append( ((1,0),(1,1),(1,2)) )
coords.append( ((2,0),(2,1),(2,2)) )
#Add cols
coords.append( ((0,0),(1,0),(2,0)) )
coords.append( ((0,1),(1,1),(2,1)) )
coords.append( ((0,2),(1,2),(2,2)) )
#Add daigs
coords.append( ((0,0),(1,1),(2,2)) )
coords.append( ((0,2),(1,1),(2,0)) )
strings = []
X_won = False
O_won = False
for coord_a, coord_b, coord_c in coords:
a = game_result[coord_a[0]][coord_a[1]]
b = game_result[coord_b[0]][coord_b[1]]
c = game_result[coord_c[0]][coord_c[1]]
if a == b == c == 'X':
X_won = True
if a == b == c == 'O':
O_won = True
if (not X_won and not O_won) or ( X_won and O_won):
return 'D'
if X_won:
return 'X'
if O_won:
return 'O'
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, 2015
Comments: