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 lukasz.bogaczynski
def checkio(board):
columns_hist = []
for i in range(0, len(board[0])):
columns_hist.insert(i, [])
row_length = len(board[0])
l_diag = [board[0][0], True]
r_diag = [board[0][-1], True]
current_diag = 0
for index, row in enumerate(board):
circle_count = 0
cross_count = 0
if l_diag[1]:
l_diag[1] = (l_diag[0] == row[current_diag])
if r_diag[1]:
r_diag[1] = (r_diag[0] == row[-(current_diag + 1)])
current_diag = current_diag + 1
for index, symbol in enumerate(row):
if symbol == 'X':
cross_count = cross_count + 1
if cross_count == row_length:
return 'X'
elif symbol == 'O':
circle_count = circle_count + 1
if circle_count == row_length:
return 'O'
else:
continue
if len(columns_hist[index]) == 0:
columns_hist[index].insert(0, symbol)
columns_hist[index].insert(1, 1)
elif columns_hist[index][1] != -1:
if columns_hist[index][0] == symbol:
columns_hist[index][1] = columns_hist[index][1] + 1
if (columns_hist[index][1]) == row_length:
return columns_hist[index][0]
else:
columns_hist[index][1] = -1
if l_diag[1] and l_diag[0] != '.':
return l_diag[0]
if r_diag[1] and r_diag[0] != '.':
return r_diag[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"
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
Nov. 16, 2017