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 shiumachi
def win(arr):
chars = list(set(arr))
if len(chars) == 1 and chars[0] != '.':
return chars[0]
return '.'
def checkio(game_result):
game_lines = []
for i in range(3):
horizontal_line = game_result[i]
game_lines.append(horizontal_line)
for i in range(3):
vertical_line = [game_result[x][i] for x in range(3)]
game_lines.append(vertical_line)
diagnoal_right = [game_result[0][0], game_result[1][1], game_result[2][2]]
game_lines.append(diagnoal_right)
diagnoal_left = [game_result[2][0], game_result[1][1], game_result[0][2]]
game_lines.append(diagnoal_left)
for line in game_lines:
w = win(line)
if w != '.': return w
return 'D'
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio([
"X.O",
"XX.",
"XOO"]) == "X", "Xs wins"
assert checkio([
"OO.",
"XOX",
"XOX"]) == "O", "Os wins"
assert checkio([
"OOX",
"XXO",
"OXX"]) == "D", "Draw"
Feb. 15, 2014
Comments: