Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Combos and Sets solution in Clear category for Xs and Os Referee by jrebane
def checkio(game_result):
'''
Inspiration drawn from the following page:
http://stackoverflow.com/questions/15839247/how-to-improve-a-code-for-tic-tac-toe-check-victory-in-python
'''
results = [list(x) for x in game_result]
victory_combos = [
# horizontal
((0,0), (1,0), (2,0)),
((0,1), (1,1), (2,1)),
((0,2), (1,2), (2,2)),
# vertical
((0,0), (0,1), (0,2)),
((1,0), (1,1), (1,2)),
((2,0), (2,1), (2,2)),
# across
((0,0), (1,1), (2,2)),
((2,0), (1,1), (0,2))
]
for coordinates in victory_combos:
letters = [results[y][x] for x,y in coordinates]
if len(set(letters)) == 1:
# Omit combos with three blanks in a row
if letters[0] != '.':
return letters[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 10, 2015