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