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 _ukasz_Mas_owski
def checkio(game_result):
def fnVertical(sign,positionX,positionY,table):
count = 1
for i in range(positionY,3):
if table[i][positionX] == sign:
#print(table[i][positionX])
count = count + 1
return count
def fnHorizontal(sign,positionX,positionY,table):
count = 1
for i in range(1,3):
if table[positionY][i] == sign:
count = count + 1
#print(sign)
return count
def fnDiagonalDown(sign,positionX,positionY,table):
count = 1
for i in range(1,3):
if table[positionY+i][i] == sign:
count = count + 1
return count
#print(sign)
def fnDiagonalUp(sign,positionX,positionY,table):
count = 1
for i in range(1,3):
if table[positionY-i][i] == sign:
count = count + 1
return count
countX = 0;
countO = 0;
Xs = 'X'
Os = 'O'
winner = "D"
for i in range(3):
if game_result[0][i] == 'X':
if fnVertical(Xs,i,1,game_result) == 3:
winner = "X"
break
if game_result[0][i] == 'O':
if fnVertical(Os,i,1,game_result) == 3:
winner = "O"
break
if winner == "D":
for i in range(3):
if game_result[i][0] == 'X':
if fnHorizontal(Xs,1,i,game_result) == 3:
winner = "X"
break
if game_result[i][0] == 'O':
if fnHorizontal(Os,1,i,game_result) == 3:
winner = "O"
break
if winner == "D":
if game_result[0][0] == 'X':
if fnDiagonalDown(Xs,0,0,game_result) == 3:
winner = "X"
if game_result[0][0] == 'O':
if fnDiagonalDown(Os,0,0,game_result) == 3:
winner = "O"
if winner == "D":
if game_result[2][0] == 'X':
if fnDiagonalUp(Xs,0,2,game_result) == 3:
winner = "X"
if game_result[2][0] == 'O':
if fnDiagonalUp(Os,0,2,game_result) == 3:
winner = "O"
i = 0
print(winner)
return winner
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"
Jan. 24, 2017