I would like to generate a text file containing all 19,683 Tic-Tac-Toe board layouts in the structure of 0 = Blank, 1 = X, and 2 = O. Unfortunately math is not my strong su
The total possible moves is not 3^9 as it includes many non allowed move in Tic Tac Toe. (X's - O's) or (O's - X's) must be equal to 1 always. As https://stackoverflow.com/a/25358690/13557570 mentions the total possible moves are 5477
Python code using numpy with states reduced to 5814:
import numpy as np
StatesMatrix = np.zeros((3**9,9))
for i in range(3**9):
c = i
for j in range(9):
StatesMatrix[i][j] = c % 3
c //= 3
StatesMatrix1 = np.zeros((5814,9))
k = 0
for i in range(0,StatesMatrix.shape[0]):
if (np. count_nonzero(StatesMatrix[i] == 1) - np. count_nonzero(StatesMatrix[i] == 2)) == 1 or (np. count_nonzero(StatesMatrix[i] == 2) - np. count_nonzero(StatesMatrix[i] == 1))== 1:
StatesMatrix1[k] = StatesMatrix[i]
k = k + 1
print(StatesMatrix1)
print(k)