@@ -55,7 +55,7 @@ def _team_side(self, agent_index: int = -1, position: pacai.core.board.Position
5555 if (position is None ):
5656 raise ValueError ("Could not find position." )
5757
58- if (position .row < (self .board .width / 2 )):
58+ if (position .col < (self .board .width / 2 )):
5959 return - 1
6060
6161 return 1
@@ -66,6 +66,13 @@ def _team_agent_indexes(self, team_modifier: int) -> list[int]:
6666 return [agent_index for agent_index in self .get_agent_indexes () if (self ._team_modifier (agent_index ) == team_modifier )]
6767
6868 def is_scared (self , agent_index : int = - 1 ) -> bool :
69+ if (agent_index == - 1 ):
70+ agent_index = self .agent_index
71+
72+ # Dead agents have nothing to fear
73+ if (self .get_agent_position (agent_index = agent_index ) is None ):
74+ return False
75+
6976 # Agents cannot be scared when on the opponent's side of the board.
7077 if (self ._team_side (agent_index = agent_index ) != self ._team_modifier (agent_index = agent_index )):
7178 return False
@@ -86,7 +93,8 @@ def get_legal_actions(self, position: pacai.core.board.Position | None = None) -
8693 if (position is None ):
8794 return [pacai .core .action .STOP ]
8895
89- return super ().get_legal_actions (position )
96+ # Call directly into pacai.core.gamestate.GameState because Pac-Man has special actions for ghosts.
97+ return pacai .core .gamestate .GameState .get_legal_actions (self , position )
9098
9199 def food_count (self , team_modifier : int = 0 , agent_index : int = - 1 ) -> int :
92100 """
@@ -228,6 +236,8 @@ def process_turn(self, # pylint: disable=too-many-statements
228236 new_position = self .board .get_agent_initial_position (self .agent_index )
229237 if (new_position is None ):
230238 raise ValueError (f"Cannot find initial position for agent { self .agent_index } ." )
239+
240+ self .board .place_marker (agent_marker , new_position )
231241 else :
232242 new_position = old_position .apply_action (action )
233243
@@ -236,22 +246,31 @@ def process_turn(self, # pylint: disable=too-many-statements
236246 if (old_position != new_position ):
237247 interaction_markers = self .board .get (new_position )
238248
239- # Since we are moving, pickup the agent from their current location.
249+ # Since we are moving, pickup the agent from their current location and move them to their new location .
240250 if (old_position is not None ):
241251 self .board .remove_marker (agent_marker , old_position )
252+ self .board .place_marker (agent_marker , new_position )
242253
243254 died = False
244255
245256 # Process actions for all the markers we are moving onto.
246257 for interaction_marker in interaction_markers :
247258 if (interaction_marker == pacai .pacman .board .MARKER_PELLET ):
259+ # Ignore our own food.
260+ if (team_modifier == self ._team_side (position = new_position )):
261+ continue
262+
248263 # Eat a food pellet.
249264 self .board .remove_marker (interaction_marker , new_position )
250265 self .score += team_modifier * FOOD_POINTS
251266
252267 if (self .food_count (team_modifier = team_modifier ) == 0 ):
253268 self .game_over = True
254269 elif (interaction_marker == pacai .pacman .board .MARKER_CAPSULE ):
270+ # Ignore our own capsules.
271+ if (team_modifier == self ._team_side (position = new_position )):
272+ continue
273+
255274 # Eat a power capsule, scare all enemy ghosts.
256275 self .board .remove_marker (interaction_marker , new_position )
257276
@@ -267,21 +286,21 @@ def process_turn(self, # pylint: disable=too-many-statements
267286 continue
268287
269288 # Check if anyone is scared.
270- self_scared = self .is_scared (agent_index )
289+ self_scared = self .is_scared (self . agent_index )
271290 other_scared = self .is_scared (other_agent_index )
272291
273292 # Check who is a ghost (agent's on their own side are a ghost).
274- # We know if we are a ghost if the other agent is a ghost or scared.
293+ self_ghost = ( team_modifier == self . _team_side ( agent_index = self . agent_index ))
275294 other_ghost = (other_team_modifier == self ._team_side (agent_index = other_agent_index ))
276295
277296 # Check who was eaten (and remove them), what team did the eating, and the points that should be awarded.
278297 eating_team_modifier = 0
279298 points = 0
280299
281- if (self_scared or other_ghost ):
300+ if (( self_scared and self_ghost ) or (( not other_scared ) and other_ghost ) ):
282301 # We got eaten, but our marker is already off the board.
283302 died = True
284- self ._kill_agent (agent_index )
303+ self ._kill_agent (self . agent_index )
285304
286305 eating_team_modifier = other_team_modifier
287306
@@ -303,9 +322,9 @@ def process_turn(self, # pylint: disable=too-many-statements
303322
304323 self .score += (eating_team_modifier * points )
305324
306- # Move the agent to the new location if it did not die .
307- if (not died ):
308- self .board .place_marker (agent_marker , new_position )
325+ # The current agent has died, remove their marker .
326+ if (died ):
327+ self .board .remove_marker (agent_marker , new_position )
309328
310329 # Decrement the scared timer.
311330 if (self .agent_index in self .scared_timers ):
0 commit comments