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 vampirelower
def checkio(game_result):
result = check_result(game_result) or check_columns(game_result) or check_cross(game_result)
return result or "D"
def check_result(game_result):
for line in game_result:
if line.count("X") == 3:
return "X"
elif line.count("O") == 3:
return "O"
return False
def check_columns(game_result):
game_result_column = []
for row in zip(*game_result):
new_row = []
for element in row:
new_row.append(element)
game_result_column.append(new_row)
return check_result(game_result_column)
def check_cross(game_result):
cross = ["", ""]
i = 0
for line in game_result:
cross[0] += line[i]
cross[1] += line[2 - i]
i += 1
return check_result(cross)
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"
May 14, 2015
Comments: