I think you are trying to send the list square
to the function isWinner
and want to check if the tic-tac-toe values match on rows or columns or diagonal.
To do this, you need to change the function as follows:
def isWinner (square):
if (square[0] == square[1] == square[2]) \
or (square[3] == square[4] == square[5]) \
or (square[6] == square[7] == square[8]) \
or (square[0] == square[3] == square[6]) \
or (square[1] == square[4] == square[7]) \
or (square[2] == square[5] == square[8]) \
or (square[0] == square[4] == square[8]) \
or (square[2] == square[4] == square[6]):
return True
else: return False
Assumption here is that square is a list or dictionary and the values in the index or key value represents a O
or X
.
A few examples:
print ('''0,0,0
X,0,X
X,X,X''')
print (isWinner(['0','0','0','X','0','X','X','X','X']))
print ('''X,0,0,
X,0,X,
X,0,X''')
print (isWinner(['X','0','0','X','0','X','X','0','X']))
print ('''X,0,0,
0,X,X,
X,0,0''')
print (isWinner(['X','0','0','0','X','X','X','0','0']))
Output:
#last row has all X. Result = True
0,0,0
X,0,X
X,X,X
True
#first column has all X, so is second column with all 0. Result = True
X,0,0,
X,0,X,
X,0,X
True
#no match row wise, column wise, or diagonal. Result = False
X,0,0,
0,X,X,
X,0,0
False