-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreeDTTT.py
142 lines (131 loc) · 5.32 KB
/
ThreeDTTT.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from TTTPoint import *
from TTTLine import *
class ThreeDTTT(object):
def __init__(self, size):
super(ThreeDTTT, self).__init__()
self.size = size
self.points = []
self.lines = []
self.maxCoord = self.size / 2
for i in xrange(-self.maxCoord, self.maxCoord + 1, 1):
for j in xrange(-self.maxCoord, self.maxCoord + 1, 1):
for k in xrange(-self.maxCoord, self.maxCoord + 1, 1):
self.points.append(TTTPoint([i, j, k]))
self.__construct_lines___()
def __construct_lines___(self):
# plane x
# vertical & horizontal lines
for k in xrange(-self.maxCoord, self.maxCoord + 1, 1):
# k=0
for i in xrange(-self.maxCoord, self.maxCoord + 1, 1):
vertpointsonline = []
horpointsonline = []
for j in xrange(-self.maxCoord, self.maxCoord + 1, 1):
vertpointsonline.append(TTTPoint([k, i, j]))
horpointsonline.append(TTTPoint([k, j, i]))
self.lines.append(TTTLine(vertpointsonline))
self.lines.append(TTTLine(horpointsonline))
# diagonal lines
maindiagline = []
altdiagline = []
for i in xrange(-self.maxCoord, self.maxCoord + 1, 1):
maindiagline.append(TTTPoint([k, i, i]))
altdiagline.append(TTTPoint([k, -i, i]))
self.lines.append(TTTLine(maindiagline))
self.lines.append(TTTLine(altdiagline))
# plane y
for k in xrange(-self.maxCoord, self.maxCoord + 1, 1):
for i in xrange(-self.maxCoord, self.maxCoord + 1, 1):
pointsonline = []
for j in xrange(-self.maxCoord, self.maxCoord + 1, 1):
pointsonline.append(TTTPoint([j, k, i]))
self.lines.append(TTTLine(pointsonline))
# diagonal lines
maindiagline = []
altdiagline = []
for i in xrange(-self.maxCoord, self.maxCoord + 1, 1):
maindiagline.append(TTTPoint([i, k, i]))
altdiagline.append(TTTPoint([-i, k, i]))
self.lines.append(TTTLine(maindiagline))
self.lines.append(TTTLine(altdiagline))
# plane z
for k in xrange(-self.maxCoord, self.maxCoord + 1, 1):
# diagonal lines
maindiagline = []
altdiagline = []
for i in xrange(-self.maxCoord, self.maxCoord + 1, 1):
maindiagline.append(TTTPoint([i, i, k]))
altdiagline.append(TTTPoint([-i, i, k]))
self.lines.append(TTTLine(maindiagline))
self.lines.append(TTTLine(altdiagline))
# largest diagonals
maindiagline = []
altdiagline1 = []
altdiagline2 = []
altdiagline3 = []
for k in xrange(-self.maxCoord, self.maxCoord + 1, 1):
maindiagline.append(TTTPoint([k, k, k]))
# (-1,1,-1),(0,0,0),(1,-1,1)
altdiagline1.append(TTTPoint([k, -k, k]))
altdiagline2.append(TTTPoint([-k, k, k]))
altdiagline3.append(TTTPoint([k, k, -k]))
self.lines.append(TTTLine(maindiagline))
self.lines.append(TTTLine(altdiagline1))
self.lines.append(TTTLine(altdiagline2))
self.lines.append(TTTLine(altdiagline3))
def displayWins(self, moves=[]):
ans=''
for i in xrange(-self.maxCoord, self.maxCoord + 1, 1):
for j in xrange(-self.maxCoord, self.maxCoord + 1, 1):
for k in xrange(-self.maxCoord, self.maxCoord + 1, 1):
currentPoint=TTTPoint([i,j,k])
ans+=str(self.countwinlines(currentPoint,moves)) + '\t'
ans+="\n"
ans+="\n"
ans+="\n\n"
print ans
def displayGame(self, playerOneMoves, playerTwoMoves=[]):
ans = ''
for i in xrange(-self.maxCoord, self.maxCoord + 1, 1):
for j in xrange(-self.maxCoord, self.maxCoord + 1, 1):
for k in xrange(-self.maxCoord, self.maxCoord + 1, 1):
if TTTPoint([i, j, k]) in playerOneMoves:
ans += 'x' + '\t'
elif TTTPoint([i, j, k]) in playerTwoMoves:
ans += 'o' + '\t'
else:
ans+='_'+'\t'
ans += "\n"
ans += "\n"
ans += "\n\n"
return ans
def winner(self,moves):
for lines in self.lines:
for point in line:
if not point in line:
break
return True
return False
def countwinlines(self, point, moves=[]):
ans = 0
#moves=set(moves)
for line in self.lines:
if len([x for x in moves if x in line])==0:
if line.pointOnLine(point):
ans += 1
return ans
def __str__(self):
return self.size
def __eq__(self, o):
return self.size == o.size
if __name__ == "__main__":
x = ThreeDTTT(3)
print(len(x.points))
print(len(x.lines))
# for line in x.lines:
# print(line)
for point in x.points:
print(str(point) + " " + str(x.countwinlines(point)))
center=TTTPoint([0,0,0])
x.displayWins([center])
print(x.countwinlines(center,[center]))