Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Lists comprehension solution in Clear category for Xs and Os Referee by amigozz
def check_horizontal(result):
for line in result:
if ( ( line[0] == line[1] == line[2] ) & ( line[0] != '.' ) ):
return line[0]
def check_vertical(result):
vertical_result = []
vertical_result = [[row[x] for row in result] for x in range(3)]
for line in vertical_result:
if ( (line[0] == line[1] == line [2]) & ( line[0] != '.') ):
return line[0]
def check_diagonal(result):
first_diagonal = [result[x][x] for x in range(3)]
second_diagonal = [result[2-x][x] for x in range(2, -1, -1)]
if ((len(set(first_diagonal))== 1) & (set(first_diagonal) != {'.'})): return first_diagonal[0]
if ((len(set(second_diagonal)) == 1) & (set(second_diagonal) != {'.'})): return second_diagonal[0]
def checkio(result):
if check_horizontal(result): return check_horizontal(result)
elif check_vertical(result): return check_vertical(result)
elif check_diagonal(result): return check_diagonal(result)
else: 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"
Dec. 23, 2014
Comments: