I am trying to create a game (tic tac toe) and I have to create a board (called tab) using 3 tuples that each has 3 elements that represent a position on a line. A "X" is represented by the number 1, a "O" by -1 and an empty space by 0. I am trying to create a function that receives an argument (in this case the board is the argument) and says if it represents a valid board or not, however it always gives me the wrong answer (in this case says it's false) and I don't know why.
tab = ((1, -1, 0), (0, 0, 1), (0, -1, 1))
def eh_tabuleiro(tab):
if len(tab) == 3:
if len(tab[0]) == 3 and len(tab[1]) == 3 and len(tab[2]) == 3:
if tab[0][0] or tab[0][1] or tab[0][2] or tab[1][0] or tab[1][1] or tab[1][2] or tab[2][0] or tab[2][1] or tab[2][2] != 1 or -1 or 0:
return False
else:
return True
else:
return False
else:
return False
For a board to be valid it needs to be a tuple with three tuples and 3 elements inside each smaller tuple. If it has 2 elements or instead of 1/-1/0 has a "-1" or any other thing the function returns False. As you can see my board is correct and it says it is not. Can anyone help me?
orexpression is wrong. Do 2 for loops, one for the rows, other nested for the columns, and check if the current position is different from 1, -1 or 0.ifdoesn't evaluate as you want it to. A more Pythonic way to express that condition is:if not all(tab[i][j] in (1, -1, 0) for i in range(3) for j in range(3))