All possible unique combinations

an anonymous user · November 22, 2023
import itertools

# Function to print a combination as a matrix
def print_combination(combination):
    for row in combination:
        print(row)
    print()

# Generate all possible combinations of placing 6 pieces in 9 spaces
all_positions = list(itertools.combinations(range(9), 6))

# Define the tic-tac-toe board size
board_size = 3

# Iterate through all combinations and print them as matrices
for positions in all_positions:
    matrix = [[0] * board_size for _ in range(board_size)]
    for position in positions:
        row = position // board_size
        col = position % board_size
        matrix[row][col] = 1
    print_combination(matrix)
Output

Comments

Please sign up or log in to contribute to the discussion.