2
def isWinner (square) :
    (square[0] and square[1] and square[2])
    (square[3] and square[4] and square[5]) 
    (square[6] and square[7] and square[8]) 
    (square[0] and square[3] and square[6]) 
    (square[1] and square[4] and square[7]) 
    (square[2] and square[5] and square[8]) 
    (square[0] and square[4] and square[8]) 
    (square[2] and square[4] and square[6]) 

This is as far as I have come but I do not really know were to go from here. Any help at all would be aprecciated. :)

Loui
  • 21
  • 3
  • where is your if statement? Are you checking if all these are True? where is your return statement. Can you provide more details of the isWinner function. Is that it? – Joe Ferndz Feb 07 '21 at 20:00

1 Answers1

1

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
Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33