Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Prettier 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:
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
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
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
#========================================main==========================================
Xs = 'X'
Os = 'O'
winner = "D"
#---------check vertical-----------------------------------
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
#--------check horizontal----------------------------------
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
#-------checck diagonal from upper left to lower right------
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"
#-------check diagonal from lower left to upper right------
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"
#------if none of the above were right, returns Draw------------
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