-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine.py
78 lines (63 loc) · 2.36 KB
/
engine.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
#!/usr/bin/env python
"""
engine utilises a function policy to choose the best move using minimax
"""
from noughts_crosses import *
from node import *
class Engine:
def __init__(self, policy, searchDepth, discount):
# policy : fn board -> [-1.0, 1.0]'
# searchDepth : int
self.policy = policy
self.searchDepth = searchDepth
self.discount = discount
def create_search_tree(self, board, player):
'create search tree from board'
node = Node(board)
if player == players[0]:
self.maximise(node, self.searchDepth, True)
else:
self.minimise(node, self.searchDepth, True)
return node
def minimax(self, board, player):
'find self.bestMove using minimax and principal variation'
return self.create_search_tree(board, player).pv.board
def maximise(self, node, depth, rootNode):
'maximise policy score for players[0]'
if (depth == 0) or (node.reward is not None):
return self.policy(node.board)
moves = move_all(players[0], node.board)
score = -2.0
for m in moves:
daughter = Node(m)
newScore = self.minimise(daughter, depth-1, False)
if (newScore > score):
if node.pv is not None:
node.other.append(node.pv)
score = newScore
node.pv = daughter
else:
node.other.append(daughter)
return self.discount * score
def minimise(self, node, depth, rootNode):
'minimise policy score for players[1]'
if (depth == 0) or (node.reward is not None):
return self.policy(node.board)
moves = move_all(players[1], node.board)
score = 2.0
for m in moves:
daughter = Node(m)
newScore = self.maximise(daughter, depth-1, False)
if (newScore < score):
if node.pv is not None:
node.other.append(node.pv)
score = newScore
node.pv = daughter
else:
node.other.append(daughter)
return self.discount * score
if __name__ == "__main__":
e = Engine(optimal, 9, 0.7)
tree = e.create_search_tree(initialBoard, players[0])
assert(len(tree.other) == 8)
pretty_print(tree.pv.board)