from itertools import product
def is_valid_position(_b):
b = [list(_b[:3]), list(_b[3: 6]), list(_b[6:])]
winner = lambda c:any(''.join(r) == c*3 for r in b) or any(''.join(r) == c*3 for r in zip(*b)) or f"{b[0][0]}{b[1][1]}{b[2][2]}" == c*3 or f"{b[0][2]}{b[1][1]}{b[2][0]}" == c*3
cnt = lambda c:''.join(''.join(r) for r in b).count(c)
more = cnt('X') in range(0, 6) and cnt('O') in range(0, 5)
diff = cnt('X') - cnt('O')
return ((winner('X') and not winner('O') and more and diff == 1) or (winner('O') and not winner('X') and more and diff == 0)) or (not winner('X') and not winner('O') and more and diff in (0, 1))
c = 0
for p in product(*(["XO."] * 9)):
b = ''.join(p)
if is_valid_position(b):
c += 1
print(c)
To embed this program on your website, copy the following code and paste it into your website's HTML: