-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTTTAnalyzer.py
53 lines (45 loc) · 1.47 KB
/
TTTAnalyzer.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
from ThreeDTTT import *
from random import choice
class TTTAnalyzer(object):
def __init__(self, dim):
super(TTTAnalyzer, self).__init__()
self.game=ThreeDTTT(dim)
self.moves=[]
self.playerOneMoves=[]
self.playerTwoMoves=[]
def greedyAlg(self):
# type: () -> list
moves=[]
bestMoves=self.bestMoves()
while len(bestMoves)>0:
moves.append(choice(bestMoves))
bestMoves=self.bestMoves((moves))
return moves
def bestMoves(self,moves=[]):
'''Takes in a set of moves places that are not available and that block wins.
And returns a list of moves with the highest number of possible wins.'''
max=0
bestmoves=[]
for point in self.game.points:
if point not in moves:
score=self.game.countwinlines(point, moves)
if score==max:
bestmoves.append(point)
if score>max:
max=score
bestmoves=[point]
if max==0:
bestmoves=[]
return bestmoves
if __name__ == "__main__":
analyzer=TTTAnalyzer(5)
#print(analyzer.bestMoves())
#print(analyzer.bestMoves([TTTPoint([0,0,0])]))
for i in range(20):
ans=analyzer.greedyAlg()
bestmoves=analyzer.greedyAlg()
if len(ans)>len(bestmoves):
ans=bestmoves
print(ans)
print(analyzer.game.displayGame(ans))
print(len(ans))