-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.py
46 lines (40 loc) · 2.12 KB
/
agent.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
import random
class AgentPlayer:
possible_positions_mrx = []
# play gets called by the controller once per turn
def play(self, gamestate):
if gamestate['figure_id'] == 1:
self.calculate_possible_positions_mrx(gamestate['position_of_mrx'], gamestate['mrx_last_used_ticket'],
gamestate['board'])
return self.play_greedy(gamestate['routes'], gamestate['board'], gamestate['figure_position'])
# Play to a random station
def play_random(self, routes):
return random.sample(routes, 1)[0]
def play_greedy(self, routes, board, my_position):
if not routes:
return None
if not AgentPlayer.possible_positions_mrx:
return self.play_random(routes)
min_average_distance_to_mrx = 10
for index, route in enumerate(routes):
sum_of_distances_to_mrx = 0
for position_of_mrx in AgentPlayer.possible_positions_mrx:
current_distance_to_mrx = board.distance(my_position, position_of_mrx)
sum_of_distances_to_mrx += current_distance_to_mrx
current_avgerage_distance_to_mrx = sum_of_distances_to_mrx / len(AgentPlayer.possible_positions_mrx)
if current_avgerage_distance_to_mrx < min_average_distance_to_mrx:
min_average_distance_to_mrx = current_avgerage_distance_to_mrx
route_to_go_to = index
return routes[route_to_go_to]
def calculate_possible_positions_mrx(self, position_of_mrx, mrx_last_used_ticket, board):
if position_of_mrx == 0:
if AgentPlayer.possible_positions_mrx:
new_positions = []
for p in AgentPlayer.possible_positions_mrx:
for q in board.routes_from(p):
if q['ticket'] == mrx_last_used_ticket and not q['station'] in new_positions:
new_positions.append(q['station'])
new_positions.sort()
AgentPlayer.possible_positions_mrx = new_positions
else:
AgentPlayer.possible_positions_mrx = [position_of_mrx]