forked from bcorfman/raven-checkers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goalformation.py
195 lines (157 loc) · 6.32 KB
/
goalformation.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
__author__ = 'brandon_corfman'
import multiprocessing
from abc import ABCMeta, abstractmethod
from goal import Goal
from utils import argmin_score
from formation import BLACK_MAP
from globalconst import FIRST, LAST, ACTIVE, INACTIVE, COMPLETED
def get_score_move(board):
def score_move(move):
player = board.to_move
board.make_move(move, False, False)
score = board.utility(player)
board.undo_move(move, False, False)
return score
return score_move
def partition_moves(move_list, domain):
primary_moves = []
secondary_moves = []
for move in move_list:
start = move.affected_squares[FIRST][0]
dest = move.affected_squares[LAST][0]
if start in domain and dest in domain:
primary_moves.append(move)
else:
secondary_moves.append(move)
return primary_moves, secondary_moves
def generate_common_domain(formation):
domain = set()
for pos in formation:
domain = domain.union(BLACK_MAP[pos])
return domain
def calc_best_move(formation, owner, term_event, child_conn):
term_event.clear()
board = owner.board
game = owner.game
primary, secondary = partition_moves(game.legal_moves(), generate_common_domain(formation))
if not primary and not secondary:
print primary
print secondary
print "Error"
child_conn.send(None)
return
score_func = get_score_move(board)
primary_move, primary_score = None, 999
if primary:
primary_move, primary_score = argmin_score(primary, score_func)
primary_score -= 1 # bonus for primary moves since they are within the formation
secondary_move, secondary_score = None, 0
if secondary:
secondary_move, secondary_score = argmin_score(secondary, score_func)
if term_event.is_set(): # a signal means terminate
term_event.clear()
child_conn.send(None)
return
move = primary_move if primary_score <= secondary_score else secondary_move
child_conn.send(move)
class GoalFormation(Goal):
__metaclass__ = ABCMeta
def __init__(self, owner):
Goal.__init__(self, owner)
self.child_conn = owner.controller.child_conn
self._term_event = multiprocessing.Event()
self._process = None
def activate(self):
self.status = ACTIVE
@abstractmethod
def process(self):
pass
def terminate(self):
self.status = INACTIVE
class GoalShortDyke(GoalFormation):
def __init__(self, owner):
GoalFormation.__init__(self, owner)
def __repr__(self):
return "GoalShortDyke"
def process(self):
self.activate_if_inactive()
self._process = multiprocessing.Process(target=calc_best_move,
args=(self.owner.board.short_dyke,
self.owner,
self._term_event,
self.child_conn))
self._process.daemon = True
self._process.start()
return COMPLETED
class GoalLongDyke(GoalFormation):
def __init__(self, owner):
GoalFormation.__init__(self, owner)
def __repr__(self):
return "GoalLongDyke"
def process(self):
self.activate_if_inactive()
self._process = multiprocessing.Process(target=calc_best_move,
args=(self.owner.board.long_dyke,
self.owner,
self._term_event,
self.child_conn))
self._process.daemon = True
self._process.start()
return ACTIVE
class GoalPyramid(GoalFormation):
def __init__(self, owner):
GoalFormation.__init__(self, owner)
def __repr__(self):
return "GoalPyramid"
def process(self):
self.activate_if_inactive()
self._process = multiprocessing.Process(target=calc_best_move,
args=(self.owner.board.pyramid,
self.owner,
self._term_event,
self.child_conn))
self._process.daemon = True
self._process.start()
return ACTIVE
class GoalPhalanx(GoalFormation):
def __init__(self, owner):
GoalFormation.__init__(self, owner)
def __repr__(self):
return "GoalPhalanx"
def process(self):
self.activate_if_inactive()
self._process = multiprocessing.Process(target=calc_best_move,
args=(self.owner.board.phalanx,
self.owner,
self._term_event,
self.child_conn))
self._process.daemon = True
self._process.start()
class GoalMill(GoalFormation):
def __init__(self, owner):
GoalFormation.__init__(self, owner)
def __repr__(self):
return "GoalMill"
def process(self):
self.activate_if_inactive()
self._process = multiprocessing.Process(target=calc_best_move,
args=(self.owner.board.mill,
self.owner,
self._term_event,
self.child_conn))
self._process.daemon = True
self._process.start()
class GoalEchelon(GoalFormation):
def __init__(self, owner):
GoalFormation.__init__(self, owner)
def __repr__(self):
return "GoalEchelon"
def process(self):
self.activate_if_inactive()
self._process = multiprocessing.Process(target=calc_best_move,
args=(self.owner.board.echelon,
self.owner,
self._term_event,
self.child_conn))
self._process.daemon = True
self._process.start()