I have a program like this one:
one_tuple = ((0, 1, 2),
(3, 4, 5),
(6, 7, 8),
(0, 3, 6),
(1, 4, 7),
(2, 5, 8),
(0, 4, 8),
(2, 4, 6))
def function():
for tuples in one_tuple:
for variables in tuple:
see if ceratin conditions are true....
this is an artificial intelligence for a program called “Tic tac toe” and this part checks every winning combination, nested for loops seemed like the easiest solution for that (that’s why sub tuples have 3 elements) and I need to go through each element and check them (for example if a sub tuple has 2 noughts or 2 crosses and 1 empty field then it needs to append that field to a list, in my example the program will never know if someone can win by taking that empty field)
2
Answers
You can use
all
for each sub-tuple to check your condition for each element of that tuple. For example, say you want to check if all the elements of the tuple are greater than2
Use a dict for the board and store winning combinations. if two out of three positions are filled we know the remaining position is a winning one. This is a quick example of the logic where
one_tuple
is replaced by winning combs: