-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmttt.py
126 lines (114 loc) · 2.79 KB
/
mttt.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import gamesman as G
WIDTH = 3
HEIGHT = 3
BORDER = 'B'
X = 'X'
O = 'O'
BLANK = '_'
initialPosition = BLANK * 9
def toLoc(i):
x = i % WIDTH
y = i // WIDTH
return (x, y)
def toIndex(loc):
x, y = loc
return x + (y * WIDTH)
def findSpaces(pos):
for i, p in enumerate(pos):
if p == BLANK:
yield i
def findNonSpaces(pos):
for i, p in enumerate(pos):
if p != BLANK:
yield i
def getPiece(pos, x, y):
if x < 0 or x > 2 or y < 0 or y > 2:
return 'B'
else:
return pos[toIndex((x, y))]
def getPlayer(pos):
if pos.count(O) >= pos.count(X):
return X
else:
return O
def primitive(pos):
'''
>>> primitive(BLANK * 9)
'undecided'
>>> primitive(X * 9)
'lose'
>>> primitive(X + X + O +
... O + O + X +
... X + O + X)
'tie'
>>> primitive(X + X + X +
... O + O + X +
... X + O + O)
'lose'
>>> primitive(O + X + X +
... O + O + X +
... X + O + X)
'lose'
>>> primitive(X + O + O +
... O + X + X +
... X + O + X)
'lose'
>>> primitive(O + O + X +
... O + X + X +
... X + X + O)
'lose'
'''
for x, y in [toLoc(i) for i in
findNonSpaces(pos)]:
piece = getPiece(pos, x, y)
if ((getPiece(pos, x + 1, y) == piece and
getPiece(pos, x + 2, y) == piece) or
(getPiece(pos, x, y + 1) == piece and
getPiece(pos, x, y + 2) == piece) or
(getPiece(pos, x + 1, y + 1) == piece and
getPiece(pos, x + 2, y + 2) == piece) or
(getPiece(pos, x - 1, y + 1) == piece and
getPiece(pos, x - 2, y + 2) == piece)):
return G.LOSE
if BLANK in pos:
return G.UNDECIDED
else:
return G.TIE
def generateMoves(pos):
'''
>>> len(generateMoves('_' * 9))
9
>>> generateMoves('_' * 9)[:3]
[(0, 0), (1, 0), (2, 0)]
>>> generateMoves('_' * 9)[3:6]
[(0, 1), (1, 1), (2, 1)]
>>> generateMoves('_' * 9)[6:]
[(0, 2), (1, 2), (2, 2)]
>>> generateMoves('XOX'
... 'OXO'
... 'X__')
[(1, 2), (2, 2)]
'''
return [toLoc(i) for i in findSpaces(pos)]
def doMove(position, move):
'''
>>> doMove('_', (0, 0))
'X'
>>> doMove('X_O', (1, 0))
'XXO'
>>> doMove('XOX'
... 'OXO'
... 'X__', (1, 2))
'XOXOXOXO_'
>>> doMove('XOX'
... 'OXO'
... 'X__', (2, 2))
'XOXOXOX_O'
>>> doMove('XOX'
... 'OXO'
... '___', (0, 2))
'XOXOXOX__'
'''
player = getPlayer(position)
index = toIndex(move)
return position[:index] + player + position[index + 1:]