-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictac.py~
62 lines (51 loc) · 1.44 KB
/
tictac.py~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
LIMIT = 8
def returnPermutationArray():
from itertools import permutations
return list(permutations(range(9)))
def inserted( board , pos , value ):
newboard = board[0:pos] + value + board[pos+1:]
assert len(newboard) == len(board)
return newboard
def convertToBoard(board, limit):
newBoard = '---------'
mark = ['X','O']
for i in range(limit):
newBoard = inserted(newBoard,board.index(i),mark[i%2])
return newBoard
def convertToBoards(board, limit):
boards = []
for i in range(1,limit+1):
newBoard = convertToBoard(board,i)
boards.append(newBoard)
return boards
def isRowWin(board):
assert len(board) == 9
for i in range(0,9,3):
if board[i] == board[i+1] == board[i+2] != '-':
return board[i]
return False
def isColWin(board):
assert len(board) == 9
for i in range(3):
if board[i] == board[i+3] == board[i+6] != '-':
return board[i]
return False
def isDiaWin(board):
assert len(board) == 9
if board[0] == board[4] == board[8] != '-':
return board[0]
if board[2] == board[4] == board[6] != '-':
return board[2]
return False
def isBoardWin(board):
return isDiaWin(board) or isRowWin(board) or isColWin(board)
def makeBoards():
database = {}
perms = returnPermutationArray()
for perm in perms:
for board in convertToBoards(perm,LIMIT):
if isBoardWin(board):
continue
database[board] = [9]*9
return database
print(len(makeBoards()))