-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.py
354 lines (297 loc) · 15.5 KB
/
state.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
from game_details import *
from weights import *
import csv
import itertools
from copy import deepcopy
VERIFY_EVAL_ADJUST = False
DEBUG = False
class State():
'''
Represents the current state of the board
players_pieces: maps players to sets of their pieces
players_exited: maps players to their number of exited pieces
turn: the player whose current turn it is
'''
def __hash__(self):
return self._hash
def set_hash(self):
self._hash = hash(tuple((self.players_exited[player], frozenset(self.players_pieces[player])) for player in PLAYERS))
def __init__(self, players_pieces=None, players_exited=None, turn=None):
# Set default values
if players_pieces is None:
players_pieces = deepcopy(START_COORDS)
if players_exited is None:
players_exited = PlayerDict(int)
if turn is None:
turn = NEXT_PLAYER[FIRST_PLAYER]
self.players_pieces = players_pieces
self.players_exited = players_exited
self.players_stats = self.calc_players_stats()
self.set_hash()
self.turn = turn
def __getitem__(self, key):
return self.players_pieces.__getitem__(key)
def __contains__(self, item):
return any(item in pieces for pieces in self.players_pieces.values())
def __eq__(self, other):
if type(self) == type(other):
for player in PLAYERS:
if self.players_exited[player] != other.players_exited[player]:
return False
if self.players_pieces[player] != other.players_pieces[player]:
return False
return True
return False
# get the best strategies for each player to use
def getStrategies(self):
playerStratWeights = {}
for player in PLAYERS:
if len(self.players_pieces[player]) + self.players_exited[player] < NUM_TO_WIN:
if len(self.players_pieces[player]) == 1:
playerStratWeights[player] = LAST_AND_NEED_MORE
else:
playerStratWeights[player] = NEED_MORE
elif len(self.players_pieces[player]) + self.players_exited[player] > NUM_TO_WIN:
if len(self.players_pieces[player]) + self.players_exited[player] >= NUM_TO_WIN + 3:
playerStratWeights[player] = HAVE_LOTS_EXTRA
if len(self.players_pieces[player]) + self.players_exited[player] >= NUM_TO_WIN + 2:
playerStratWeights[player] = HAVE_SOME_EXTRA
else:
playerStratWeights[player] = HAVE_1_EXTRA
else:
playerStratWeights[player] = STANDARD
return playerStratWeights
# calculate weighted sum of state features
# this is essentially the evaluation function
def get_raw_eval(self, playerStrats = None):
raw_dict = PlayerDict(float)
modeWeights={}
if playerStrats is None:
playerStrats = self.getStrategies()
for player in PLAYERS:
playerStratWeights = STRATEGIES[playerStrats[player]]
for stat_name in WEIGHTS:
modeWeights[stat_name] = WEIGHTS[stat_name] * playerStratWeights[stat_name]
raw_dict[player] = modeWeights[NUM_PIECES] * len(self.players_pieces[player])
raw_dict[player] += modeWeights[NUM_EXITED] * self.players_exited[player]
for stat_name, stat in self.players_stats[player].items():
# Reweight being in danger according to whose turn it is
if stat_name == NUM_DANGERED:
if self.turn == player:
raw_dict[player] += (modeWeights[stat_name]*stat)/2
else:
raw_dict[player] += (modeWeights[stat_name]*stat)*2
elif stat_name == NUM_THREATS:
if self.turn == player:
raw_dict[player] += (modeWeights[stat_name]*stat)*2
else:
raw_dict[player] += (modeWeights[stat_name]*stat)/2
# convert distance into average distance
elif stat_name == TOTAL_DIST and len(self.players_pieces[player]):
raw_dict[player] += (modeWeights[stat_name]*stat)/len(self.players_pieces[player])
else:
raw_dict[player] += modeWeights[stat_name]*stat
return raw_dict
# Prioritise details of the terminal states, then normalize the raw evaluations of each player to each other
def get_relative_eval(self):
evaluation = self.get_raw_eval()
#return {player: (self.utility(player), player_eval) for player, player_eval in evaluation.items()}
relative_eval = {}
for player, player_eval in evaluation.items():
opponent_raw = 0
opponent_raw += 0.45 * evaluation[min(OPPONENTS[player], key=evaluation.__getitem__)] + 0.55 * evaluation[max(OPPONENTS[player], key=evaluation.__getitem__)]
relative_eval[player] = (self.utility(player), player_eval - opponent_raw)
return relative_eval
# Slices players pieces dictionary to be just the opponents
def iter_opponents_pieces(self, player):
return ((opponent, opponent_pieces) for opponent, opponent_pieces in self.players_pieces.items() if opponent!=player)
def is_terminal(self):
return any(map(int(NUM_TO_WIN).__le__, self.players_exited.values()))
def is_winner(self, player):
return self.players_exited[player] >= NUM_TO_WIN
def utility(self, player):
if self.is_terminal():
return 1 if self.is_winner(player) else -1
return 0
def calc_players_stats(self):
players_stats = {player: {stat_name: int() for stat_name in (TOTAL_DIST,
NUM_CAN_EXIT,
NUM_THREATS,
NUM_DANGERED,
NUM_PROTECTS)} for player in PLAYERS}
for player, pieces in self.players_pieces.items():
players_stats[player][TOTAL_DIST] = sum(EXIT_DIST[player][piece] for piece in self.players_pieces[player])
for piece in pieces:
if piece in EXIT_COORDS[player]:
players_stats[player][NUM_CAN_EXIT] += 1
for move, jump in ALL_NEIGHBOURS[piece]:
if jump is not None and jump not in self:
for opponent, opponents_pieces in self.iter_opponents_pieces(player):
if move in opponents_pieces:
players_stats[player][NUM_THREATS] += 1
players_stats[opponent][NUM_DANGERED] += 1
break
if move in self.players_pieces[player]:
players_stats[player][NUM_PROTECTS] += 1
return players_stats
def apply_action(self, acting_player, action):
action_type, details = action
if action_type == "EXIT":
exit_coord = details
self.players_pieces[acting_player].remove(exit_coord)
self.players_exited[acting_player] += 1
self.adjust_eval_remove_piece(acting_player, exit_coord)
elif action_type == "MOVE":
from_coord, to_coord = details
self.players_pieces[acting_player].remove(from_coord)
self.adjust_eval_remove_piece(acting_player, from_coord)
self.players_pieces[acting_player].add(to_coord)
self.adjust_stats_add_piece(acting_player, to_coord)
elif action_type == "JUMP":
from_coord, to_coord = details
between_coord = coord_between(from_coord, to_coord)
self.players_pieces[acting_player].remove(from_coord)
self.adjust_eval_remove_piece(acting_player, from_coord)
for opponent in OPPONENTS[acting_player]:
if between_coord in self.players_pieces[opponent]:
self.players_pieces[opponent].remove(between_coord)
self.adjust_eval_remove_piece(opponent, between_coord)
self.players_pieces[acting_player].add(between_coord)
self.adjust_stats_add_piece(acting_player, between_coord)
break
self.players_pieces[acting_player].add(to_coord)
self.adjust_stats_add_piece(acting_player, to_coord)
self.turn = NEXT_PLAYER[acting_player]
self.set_hash()
if VERIFY_EVAL_ADJUST:
correct_stats = self.calc_players_stats()
for player in PLAYERS:
for stat_name, stat_val in self.players_stats[player].items():
assert(stat_val == correct_stats[player][stat_name])
def adjust_stats_add_piece(self, player, coord):
return self.adjust_eval_add_OR_remove_piece(player, coord, addMode=True)
def adjust_eval_remove_piece(self, player, coord):
return self.adjust_eval_add_OR_remove_piece(player, coord, addMode=False)
def adjust_eval_add_OR_remove_piece(self, player, coord, addMode):
opponents = OPPONENTS[player]
addORremoveFactor = (1 if addMode else -1)
self.players_stats[player][TOTAL_DIST] += addORremoveFactor * EXIT_DIST[player][coord]
if coord in EXIT_COORDS[player]:
self.players_stats[player][NUM_CAN_EXIT] += addORremoveFactor
for move, jump in ALL_NEIGHBOURS[coord]:
if move in self.players_pieces[player]:
self.players_stats[player][NUM_PROTECTS] += 2 * addORremoveFactor
if jump is not None:
if jump not in self:
# Add/Remove threats/dangers regarding coord-piece jumping over neighbouring opponent
#
# attacker
# .-'-._.-'-._.-'-._
# |coord|move |jump |
# '-._.-'-._.-'-._.-'
# victim
for opponent in opponents:
if move in self.players_pieces[opponent]:
self.players_stats[opponent][NUM_DANGERED] += addORremoveFactor
self.players_stats[player][NUM_THREATS] += addORremoveFactor
break
else:
# Add/Remove threats/dangers regarding jump-piece jumping over move-piece and landing on coord-spot
#
# attacker
# .-'-._.-'-._.-'-._
# |coord|move |jump |
# '-._.-'-._.-'-._.-'
# victim
for victim, victim_pieces in self.players_pieces.items():
if move in victim_pieces:
for attacker, attacker_pieces in self.iter_opponents_pieces(victim):
if jump in attacker_pieces:
self.players_stats[attacker][NUM_THREATS] -= addORremoveFactor
self.players_stats[victim][NUM_DANGERED] -= addORremoveFactor
break
break
# Add/Remove threats/dangers regarding jump-piece jumping over move-piece and landing on coord-spot
#
# attacker attacker
# .-'-._.-'-._.-'-._ .-'-._.-'-._.-'-._
# |cord1|coord|cord2| OR |cord1|coord|cord2|
# '-._.-'-._.-'-._.-' '-._.-'-._.-'-._.-'
# victim victim
for coord1, coord2 in OPPOSING_NEIGHBOUR_PAIRS[coord]:
if coord1==None or coord2==None:
continue
for opponent in opponents:
if (coord1 in self.players_pieces[opponent] \
and coord2 not in self) \
or (coord2 in self.players_pieces[opponent] \
and coord1 not in self):
self.players_stats[opponent][NUM_THREATS] += addORremoveFactor
self.players_stats[player][NUM_DANGERED] += addORremoveFactor
break
def __repr__(self):
return self.__str__()
def __str__(self):
# Set up the board template:
if not DEBUG:
# Use the normal board template (smaller, not showing coordinates)
template = """# {0}
# .-'-._.-'-._.-'-._.-'-.
# |{16:}|{23:}|{29:}|{34:}|
# .-'-._.-'-._.-'-._.-'-._.-'-.
# |{10:}|{17:}|{24:}|{30:}|{35:}|
# .-'-._.-'-._.-'-._.-'-._.-'-._.-'-.
# |{05:}|{11:}|{18:}|{25:}|{31:}|{36:}|
# .-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-.
# |{01:}|{06:}|{12:}|{19:}|{26:}|{32:}|{37:}|
# '-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'
# |{02:}|{07:}|{13:}|{20:}|{27:}|{33:}|
# '-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'
# |{03:}|{08:}|{14:}|{21:}|{28:}|
# '-._.-'-._.-'-._.-'-._.-'-._.-'
# |{04:}|{09:}|{15:}|{22:}|
# '-._.-'-._.-'-._.-'-._.-'"""
else:
# Use the debug board template (larger, showing coordinates)
template = """# {0}
# ,-' `-._,-' `-._,-' `-._,-' `-.
# | {16:} | {23:} | {29:} | {34:} |
# | 0,-3 | 1,-3 | 2,-3 | 3,-3 |
# ,-' `-._,-' `-._,-' `-._,-' `-._,-' `-.
# | {10:} | {17:} | {24:} | {30:} | {35:} |
# | -1,-2 | 0,-2 | 1,-2 | 2,-2 | 3,-2 |
# ,-' `-._,-' `-._,-' `-._,-' `-._,-' `-._,-' `-.
# | {05:} | {11:} | {18:} | {25:} | {31:} | {36:} |
# | -2,-1 | -1,-1 | 0,-1 | 1,-1 | 2,-1 | 3,-1 |
# ,-' `-._,-' `-._,-' `-._,-' `-._,-' `-._,-' `-._,-' `-.
# | {01:} | {06:} | {12:} | {19:} | {26:} | {32:} | {37:} |
# | -3, 0 | -2, 0 | -1, 0 | 0, 0 | 1, 0 | 2, 0 | 3, 0 |
# `-._,-' `-._,-' `-._,-' `-._,-' `-._,-' `-._,-' `-._,-'
# | {02:} | {07:} | {13:} | {20:} | {27:} | {33:} |
# | -3, 1 | -2, 1 | -1, 1 | 0, 1 | 1, 1 | 2, 1 |
# `-._,-' `-._,-' `-._,-' `-._,-' `-._,-' `-._,-'
# | {03:} | {08:} | {14:} | {21:} | {28:} |
# | -3, 2 | -2, 2 | -1, 2 | 0, 2 | 1, 2 | key:
# `-._,-' `-._,-' `-._,-' `-._,-' `-._,-' ,-' `-.
# | {04:} | {09:} | {15:} | {22:} | | input |
# | -3, 3 | -2, 3 | -1, 3 | 0, 3 | | q, r |
# `-._,-' `-._,-' `-._,-' `-._,-' `-._,-'"""
# prepare the provided board contents as strings, formatted to size.
ran = range(-3, +3+1)
cells = []
for qr in [(q,r) for q in ran for r in ran if -q-r in ran]:
for player, pieces in self.players_pieces.items():
if qr in pieces:
cell = str({ # something 5 characters wide for each colour:
RED: " \033[1m(\033[91mR\033[0m\033[1m)\033[0m ",
GREEN: " \033[1m(\033[92mG\033[0m\033[1m)\033[0m ",
BLUE: " \033[1m(\033[94mB\033[0m\033[1m)\033[0m ",
' ': " "
}[player]).center(5)
break
else:
cell = " " # 5 spaces will fill a cell
cells.append(cell)
# fill in the template to create the board drawing, then print!
board = template.format("STATE", *cells)
return board