-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathivar_defensive_agent.py
134 lines (107 loc) · 4.21 KB
/
ivar_defensive_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
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
from captureAgents import CaptureAgent
from util import nearestPoint
class defensiveAgent(CaptureAgent):
"""
A Dummy agent to serve as an example of the necessary agent structure.
You should look at baselineTeam.py for more details about how to
create an agent as this is the bare minimum.
"""
def registerInitialState(self, gameState):
CaptureAgent.registerInitialState(self, gameState)
def is_over_half(self, position, gameState):
"""
Checks if the agent is over the half of the board
@param position: the position of the agent
@param gameState: the current game state
@return: True if the agent is over the half of the board, False otherwise
"""
food = self.getFood(gameState)
if self.red:
if position[0] <= len(food[0])-1:
return False
else:
return True
else:
if position[0] >= len(food[0]):
return False
else:
return True
def chooseAction(self, gameState):
#python capture.py -r baselineTeam -b myTeam
#python capture.py -r myTeam -b baselineTeam
self.distancer.getMazeDistances()
legal_actions = gameState.getLegalActions(self.index)
food_locations = self.find_food(gameState)
opponents_location = self.find_opponents(gameState)
our_location = self.find_self(gameState)
action = self.best_action(our_location=our_location, food_locations=food_locations, opponents_location=opponents_location, legal_actions=legal_actions, gameState=gameState)
return action
def find_food(self, gameState):
"""
Finds the location of all our food pallets in the game
@param gameState: the current game state
@return: a list of all the food pallets in the game
"""
food_locations = []
food = self.getFoodYouAreDefending(gameState)
x = 0
for row in food:
y = 0
for cel in row:
if cel:
food_locations.append((x, y))
y += 1
x += 1
return food_locations
def find_opponents(self, gameState):
"""
Finds the location of all the opponents in the game
@param gameState: the current game state
@return: a list of all the opponents in the game
"""
opponents = []
opponent_index = self.getOpponents(gameState)
for index in opponent_index:
opponents.append(gameState.getAgentPosition(index))
return opponents
def find_self(self, gameState):
"""
Finds the location of our agent in the game
@param gameState: the current game state
@return: the location of our agent
"""
return gameState.getAgentPosition(self.index)
def difference_after_movement(self, pos1, action):
if action == "North":
return (pos1[0], pos1[1] + 1)
elif action == "South":
return (pos1[0], pos1[1] - 1)
elif action == "East":
return (pos1[0] + 1, pos1[1])
elif action == "West":
return (pos1[0] - 1, pos1[1])
else:
return pos1
def closest_opponent_to_food(self, food_locations, opponents_location):
min_distance = float('inf')
closest_opponent = None
closes_food = None
for food_loc in food_locations:
for opp_loc in opponents_location:
dist = self.getMazeDistance(food_loc, opp_loc)
if dist < min_distance:
min_distance = dist
closest_opponent = opp_loc
closest_food = food_loc
return closest_opponent, closest_food
def best_action(self, our_location, food_locations, opponents_location, legal_actions, gameState):
current_best_action = "Stop"
best_action_value = float('inf')
for action in legal_actions:
next_location = self.difference_after_movement(our_location, action)
closest_opponent, their_closest_food = self.closest_opponent_to_food(food_locations, opponents_location)
action_value = self.getMazeDistance(next_location, closest_opponent) - 0.2 * self.getMazeDistance(next_location, their_closest_food)
if action_value < best_action_value and self.is_over_half(next_location, gameState) == False:
best_action_value = action_value
current_best_action = action
return current_best_action