Skip to content

Commit cfed8f7

Browse files
committed
Determine winner works
1 parent dca3698 commit cfed8f7

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

codeclash/games/battlecode/main.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import re
12
from typing import Any
23

4+
from codeclash.constants import RESULT_TIE
35
from codeclash.games.abstract import CodeGame
46

57

@@ -10,13 +12,25 @@ def __init__(self, config):
1012
super().__init__(config)
1113
assert len(config["players"]) == 2, "BattleCode is a two-player game"
1214
self.run_cmd_round: str = "python run.py run"
13-
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+
1422
def determine_winner(self, agents: list[Any]):
15-
response = self.environment.execute(f"tail -2 {self.round_log_path}")
16-
self.scoreboard.append((self.round, "BLAH")) # TODO
17-
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+
1828
def execute_round(self, agents: list[Any]):
19-
args = [f"/{agent.name}/src/" for agent in agents]
20-
cmd = f"{self.run_cmd_round}" # TODO
29+
args = [
30+
f" --p{idx+1}-dir /{agent.name}/src/ --p{idx+1} {agent.name}"
31+
for idx, agent in enumerate(agents)
32+
]
33+
cmd = f"{self.run_cmd_round} {' '.join(args)} > {self.round_log_path}"
34+
print(f"Running command: {cmd}")
2135
response = self.environment.execute(cmd)
22-
assert response["returncode"] == 0, response
36+
assert response["returncode"] == 0, response

configs/battlecode.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
game:
22
name: BattleCode
33
rounds: 2
4+
args:
5+
maps: quack
46
players:
57
- agent: dummy
68
name: p1

0 commit comments

Comments
 (0)