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 kristo
# migrated from python 2.7
def checkio(game_result):
# Inspired by Daniel Dou's elegant solution
# http://www.checkio.org/mission/x-o-referee/publications/DanielDou/python-27/first/
# join the board into one string
result = "".join(game_result)
# possible win combinations within the result string
win_positions = [
[0, 1, 2], [3, 4, 5], [6, 7, 8],
[0, 3, 6], [1, 4, 7], [2, 5, 8],
[0, 4, 8], [2, 4, 6]
]
# Check all
for w in win_positions:
if (result[w[0]] == result[w[1]] == result[w[2]]) and result[w[0]] != ".":
return result[w[0]]
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"
June 26, 2014