Skip to content

Commit 73181fa

Browse files
authored
Merge pull request #21 from emagedoc/john/battlecode
Fixes #3: Adding BattleCode as a game
2 parents fe1886d + 14de2af commit 73181fa

5 files changed

Lines changed: 73 additions & 4 deletions

File tree

codeclash/games/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from codeclash.games.abstract import CodeGame
2+
from codeclash.games.battlecode.main import BattleCodeGame
23
from codeclash.games.battlesnake.main import BattleSnakeGame
34
from codeclash.games.corewar.main import CoreWarGame
45
from codeclash.games.robocode.main import RoboCodeGame
@@ -8,10 +9,13 @@
89
# might consider postponing imports to avoid loading things we don't need
910
def get_game(config: dict) -> CodeGame:
1011
game = {
11-
BattleSnakeGame.name: BattleSnakeGame,
12-
CoreWarGame.name: CoreWarGame,
13-
RoboCodeGame.name: RoboCodeGame,
14-
RobotRumbleGame.name: RobotRumbleGame,
12+
x.name: x for x in [
13+
BattleCodeGame,
14+
BattleSnakeGame,
15+
CoreWarGame,
16+
RoboCodeGame,
17+
RobotRumbleGame
18+
]
1519
}.get(config["game"]["name"])
1620
if game is None:
1721
raise ValueError(f"Unknown game: {config['game']['name']}")

codeclash/games/abstract.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ def run_round(self, agents: list[Any]):
140140
self._pre_round_setup(agents)
141141
self.execute_round(agents)
142142
self.determine_winner(agents)
143+
last_winner = self.scoreboard[-1][1]
144+
print(f"Round {self.round} winner: {last_winner}")
143145
self._post_round_setup(agents)
144146

145147
@property

codeclash/games/battlecode/main.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import re
2+
from typing import Any
3+
4+
from codeclash.constants import DIR_WORK, RESULT_TIE
5+
from codeclash.games.abstract import CodeGame
6+
7+
8+
class BattleCodeGame(CodeGame):
9+
name: str = "BattleCode"
10+
11+
def __init__(self, config):
12+
super().__init__(config)
13+
assert len(config["players"]) == 2, "BattleCode is a two-player game"
14+
self.run_cmd_round: str = "python run.py run"
15+
for arg, val in config.get("args", {}).items():
16+
if isinstance(val, bool):
17+
if val:
18+
self.run_cmd_round += f" --{arg}"
19+
else:
20+
self.run_cmd_round += f" --{arg} {val}"
21+
22+
def determine_winner(self, agents: list[Any]):
23+
response = self.environment.execute(f"tail -3 {self.round_log_path} | head -1")
24+
winner = re.search(r"\s\((.*)\)\swins\s\(", response["output"]).group(1)
25+
winner = {"A": agents[0].name, "B": agents[1].name}.get(winner, RESULT_TIE)
26+
self.scoreboard.append((self.round, winner))
27+
28+
def execute_round(self, agents: list[Any]):
29+
for agent in agents:
30+
src, dest = f"/{agent.name}/src/mysubmission/", str(
31+
DIR_WORK / "src" / agent.name
32+
)
33+
self.environment.execute(f"cp -r {src} {dest}")
34+
args = [
35+
f"--p{idx+1}-dir src --p{idx+1} {agent.name}"
36+
for idx, agent in enumerate(agents)
37+
]
38+
cmd = f"{self.run_cmd_round} {' '.join(args)} > {self.round_log_path}"
39+
print(f"Running command: {cmd}")
40+
response = self.environment.execute(cmd)
41+
assert response["returncode"] == 0, response

configs/battlecode.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
game:
2+
name: BattleCode
3+
rounds: 2
4+
args:
5+
maps: quack
6+
players:
7+
- agent: dummy
8+
name: p1
9+
- agent: dummy
10+
name: p2

docker/BattleCode.Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM python:3.12-slim-bookworm
2+
3+
ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1
4+
RUN apt-get update && apt-get install -y --no-install-recommends \
5+
build-essential git curl && \
6+
rm -rf /var/lib/apt/lists/*
7+
8+
RUN git clone https://github.com/emagedoc/BattleCode.git /testbed
9+
10+
WORKDIR /testbed
11+
12+
RUN python run.py update

0 commit comments

Comments
 (0)