|
| 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 |
0 commit comments