Skip to content

Commit cc74114

Browse files
Working on the capture semantics.
1 parent a29094c commit cc74114

14 files changed

+69
-87
lines changed

pacai/capture/bin.py

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,11 @@
77
import typing
88

99
import pacai.capture.game
10-
import pacai.core.agentinfo
11-
import pacai.core.board
1210
import pacai.util.bin
13-
import pacai.util.alias
1411

15-
DEFAULT_BOARD: str = 'classic-medium'
12+
DEFAULT_BOARD: str = 'capture-medium'
1613
DEFAULT_SPRITE_SHEET: str = 'pacman'
1714

18-
def set_cli_args(parser: argparse.ArgumentParser, **kwargs) -> argparse.ArgumentParser:
19-
"""
20-
Set Capture-specific CLI arguments.
21-
This is a sibling to init_from_args(), as the arguments set here can be interpreted there.
22-
"""
23-
24-
# TEST
25-
parser.add_argument('--pacman', dest = 'pacman', metavar = 'AGENT_TYPE',
26-
action = 'store', type = str, default = pacai.util.alias.AGENT_USER_INPUT.short,
27-
help = ('Select the agent type that PacMan will use (default: %(default)s).'
28-
+ f' Builtin agents: {pacai.util.alias.AGENT_SHORT_NAMES}.'))
29-
30-
return parser
31-
32-
def init_from_args(args: argparse.Namespace) -> tuple[dict[int, pacai.core.agentinfo.AgentInfo], list[int], dict[str, typing.Any]]:
33-
"""
34-
Setup agents based on Capture rules.
35-
"""
36-
37-
base_agent_infos: dict[int, pacai.core.agentinfo.AgentInfo] = {}
38-
39-
# TEST
40-
41-
# Create base arguments for all possible agents.
42-
for i in range(pacai.core.board.MAX_AGENTS):
43-
if (i == 0):
44-
base_agent_infos[i] = pacai.core.agentinfo.AgentInfo(name = args.pacman)
45-
else:
46-
base_agent_infos[i] = pacai.core.agentinfo.AgentInfo(name = args.ghosts)
47-
48-
return base_agent_infos, [], {}
49-
5015
def get_additional_ui_options(args: argparse.Namespace) -> dict[str, typing.Any]:
5116
""" Get additional options for the UI. """
5217

@@ -61,9 +26,7 @@ def main() -> int:
6126
description = "Play a game of Capture.",
6227
default_board = DEFAULT_BOARD,
6328
game_class = pacai.capture.game.Game,
64-
custom_set_cli_args = set_cli_args,
6529
get_additional_ui_options = get_additional_ui_options,
66-
custom_init_from_args = init_from_args,
6730
)
6831

6932
if (__name__ == '__main__'):

pacai/capture/gamestate.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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):

pacai/resources/boards/capture-alley.board

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
}
44
---
55
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6-
%1% .%.%.% %.% %.%%% % %
7-
%3% %.% % %%% % % % % % %
6+
%0% .%.%.% %.% %.%%% % %
7+
%2% %.% % %%% % % % % % %
88
% % % % %%% %%% %% %%%%% %.% % %
99
% % % % % %.% .%.% %%% % %.% % %
1010
% % % % % % % %%%%% %.% %%% %%% % %
1111
% % % %.% % % % % %.% % % %
1212
% % %%% %%% %.% %%%%% % % % % % % %
1313
% % %.% % %%% %.%. %.% % % % % %
1414
% % %.% %%%%% %% %%% %%% % % % %
15-
% % % % % % %%% % %.% %4%
16-
% % %%%.% %.% %.%.%. %2%
15+
% % % % % % %%% % %.% %3%
16+
% % %%%.% %.% %.%.%. %1%
1717
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

pacai/resources/boards/capture-blox.board

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
}
44
---
55
%%%%%%%%%%%%%%%%%%
6-
%1%%%%%%%%%%%%%%2%
6+
%0%%%%%%%%%%%%%%1%
77
% %..... .....% %
88
% %..... .....% %
99
% %.% %%%%%% %.% %
@@ -20,5 +20,5 @@
2020
% %..... .....% %
2121
% %.%% % % %%.% %
2222
% % %% % % %% % %
23-
%3%.... ....%4%
24-
%%%%%%%%%%%%%%%%%%
23+
%2%.... ....%3%
24+
%%%%%%%%%%%%%%%%%%

pacai/resources/boards/capture-crowded.board

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
}
44
---
55
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6-
% 3%%2 %
6+
% 2%%1 %
77
% %%%%%%%%%%%%%% %%%%%%%%%%%%%%%%% %
88
% %......... % ..............% %
99
% %........ %.%%%%%%%%.%%% %%.%%% %
1010
% %%%%.%%%%% %... .%.....% %....% %
11-
% ....%5%%%.%%%. .......% %.... %
12-
% ....% %....... .%%%.%%%6%.... %
11+
% ....%4%%%.%%%. .......% %.... %
12+
% ....% %....... .%%%.%%%5%.... %
1313
% %....% %.....%. ...% %%%%%.%%%% %
1414
% %%% %% %%%.%%%%%%%%.% ........% %
1515
% %.............. % .........% %
1616
% %%%%%%%%%%%%%%%%% %%%%%%%%%%%%%% %
17-
% 1%%4 %
17+
% 0%%3 %
1818
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

pacai/resources/boards/capture-default.board

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
}
44
---
55
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6-
% %. %.%.% % %.%.%4%
7-
% % %% %% % %%% %.%2%
6+
% %. %.%.% % %.%.%3%
7+
% % %% %% % %%% %.%1%
88
% % %. % %%% %%%% .%..% % % %
99
% % %% % ..% % % %%%%% % % %
1010
% % %%%%% %%% %%%.% o % % %
@@ -15,6 +15,6 @@
1515
% % % o %.%%% %%% %%%%% % %
1616
% % % %%%%% % % %.. % %% % %
1717
% % % %..%. %%%% %%% % .% % %
18-
%1%.% %%% % %% %% % %
19-
%3%.%.% % %.%.% .% %
20-
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
18+
%0%.% %%% % %% %% % %
19+
%2%.%.% % %.%.% .% %
20+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

pacai/resources/boards/capture-distant.board

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
}
44
---
55
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6-
% % %. %.%.% % %.%.% %4%
7-
% % % % %% %% % %%% %.% % %2%
6+
% % %. %.%.% % %.%.% %3%
7+
% % % % %% %% % %%% %.% % %1%
88
% % % % %. % %%% %%%% .%..% % % % % %
99
% % % % %% % ..% % % %%%%% % % % % %
1010
% % % % %%%%% %%% %%%.% o % % % % %
@@ -15,6 +15,6 @@
1515
% % % % % o %.%%% %%% %%%%% % % % %
1616
% % % % % %%%%% % % %.. % %% % % % %
1717
% % % % % %..%. %%%% %%% % .% % % % %
18-
%1% % %.% %%% % %% %% % % % %
19-
%3% %.%.% % %.%.% .% % %
18+
%0% % %.% %%% % %% %% % % % %
19+
%2% %.%.% % %.%.% .% % %
2020
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

pacai/resources/boards/capture-fast.board

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
}
44
---
55
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6-
% %.%%.% % %.%.%4%
7-
% % % % %%%% %%% %.%2%
6+
% %.%%.% % %.%.%3%
7+
% % % % %%%% %%% %.%1%
88
% % % % %%% % .%..% % %
99
% % % % ..% %.%%%% %%%%%% % % %
1010
% % % %%%%%% %%%%.% %.. % % % %
1111
% % %..%. % %%% % % % %
12-
%1%.% %%% %%%% % % % %
13-
%3%.%.% % %.%%.% %
12+
%0%.% %%% %%%% % % % %
13+
%2%.%.% % %.%%.% %
1414
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

pacai/resources/boards/capture-jumbo.board

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
}
44
---
55
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6-
%1%o %%%%%% o%2%
6+
%0%o %%%%%% o%1%
77
% % %%%%%%%%%%%%%%% %%%%%% %%%%%%%%%%%%%%% % %
88
% % %.................%%.................% % %
99
% % %.%%%%%%%%%%%%% %%%%%% %%%%%%%%%%%%%.% % %
@@ -21,7 +21,7 @@
2121
% %.%...% ...%...% % % %...%... %...%.% %
2222
% %...%.. %.%.%.%.%...%%...%.%.%.%.% ..%...% %
2323
% %.%...% %.%.%...%.%.%%.%.%...%.%.% %...%.% %
24-
%3%...%.% %.%.% % %...%%...% % %.%.% %.%...%4%
24+
%2%...%.% %.%.% % %...%%...% % %.%.% %.%...%3%
2525
% %.%...% %... %.%.%%.%.% ...% %...%.% %
2626
% %...%.. %%%%% % %...%%...% % %%%%% ..%...% %
2727
% %.....% %....%.%.%%.%.%....% %.....% %

pacai/resources/boards/capture-medium.board

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
%.%%.%.%%%%%%.%.%%.%
1414
%...... ......%
1515
% %%%%%%%%%%%%%%%% %
16-
% 1%%2 %
16+
% 0%%1 %
1717
%%%%%% %%%%%% %%%%%%
18-
%%%%%% 3%%4 %%%%%%
18+
%%%%%% 2%%3 %%%%%%
1919
%%%%%%%%%%%%%%%%%%%%

0 commit comments

Comments
 (0)