Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Using exception solution in Creative category for Xs and Os Referee by nickie
def check(r):
if r[0] in "XO" and all(c == r[0] for c in r):
raise Exception(r[0])
def checkio(b):
try:
for i in range(3):
check([b[i][j] for j in range(3)]) # row
check([b[j][i] for j in range(3)]) # column
check([b[j][j] for j in range(3)]) # first diagonal
check([b[j][2-j] for j in range(3)]) # second diagonal
except Exception as w:
return w.args[0]
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"
Oct. 12, 2013
Comments: