I am making a tic tac toe game and I have run across an issue. I want to make a list that when someone inputs an integer that integer is appended into a list. Once the list contains 3 elements (numbers) I want to generate a list of permutations of those numbers and check it against the 8 winning combinations possible in tic tac toe. However, while I am able to create permutations of the numbers in the list and check the list for the winning combinations the code is not recognizing that the key is in the list. Can you help me figure this out? Here's the code, thanks.
player_moves = [1, 3, 2]
for combo in permutations(player_moves, 3):
player_moves = combo
print(player_moves)
computer_moves = []
winning_combination = [(1, 2, 3), (4, 5, 6), (7, 8, 9), (1, 4, 7), (2, 5, 8), (3, 6, 9), (1, 5, 9), (3, 6, 9)]
for key in winning_combination:
for moves in player_moves:
if moves == key:
print(str(key) " was found")
break
else:
print(str(key) " not found")
CodePudding user response:
you have to trap all your moves in list:
initial_move = [1, 3, 2]
player_moves = []
for combo in itertools.permutations(initial_move, 3):
player_moves.append(combo)
print(player_moves)
winning_combination = [(1, 2, 3), (4, 5, 6), (7, 8, 9), (1, 4, 7), (2, 5, 8), (3, 6, 9), (1, 5, 9), (3, 6, 9)]
for key in winning_combination:
for moves in player_moves:
if moves == key:
print(str(key) " was found")
break
else:
print(str(key) " not found")
CodePudding user response:
You could get a quicker (and arguably simpler) mechanism if you turn the played position into a set and check if it is a superset of any of the winning combinations:
winning_combination = [(1, 2, 3), (4, 5, 6), (7, 8, 9), (1, 4, 7),
(2, 5, 8), (3, 6, 9), (1, 5, 9), (3, 6, 9)]
keys = filter(set(player_moves).issuperset,winning_combination)
print(keys or "no key", "found")
CodePudding user response:
It's not clear to me what the goal is of you're trying to accomplish, but here's a way of doing it that doesn't require storing the permutations of player moves at all—contrary to the suggestion in my comment—because you can just generate them iteratively via itertools.permutations()
as needed:
from itertools import permutations
player_moves = [1, 3, 2]
computer_moves = []
winning_combinations = [(1, 2, 3), (4, 5, 6), (7, 8, 9), (1, 4, 7), (2, 5, 8), (3, 6, 9),
(1, 5, 9), (3, 6, 9)]
for key in winning_combinations:
for moves in permutations(player_moves, 3):
if moves == key:
print(str(key) " WAS FOUND")
break
else:
print(str(key) " not found")
Based on your comments, I now suggest you make winning_combinations
a set
instead of a list
because they provide a way to do very fast membership testing that doesn't require two for
loops (which means the break
will halt further searching).
from itertools import permutations
winning_combinations = {(1, 2, 3), (4, 5, 6), (7, 8, 9), (1, 4, 7), (2, 5, 8), (3, 6, 9),
(1, 5, 9), (3, 6, 9)}
player_moves = [1, 3, 2]
for moves in permutations(player_moves, 3):
if moves in winning_combinations:
print(str(moves) " is a winning combination")
break
else:
print("no permutation of moves was a winning combination")