Skip to content

Commit 148e737

Browse files
committed
Better validation for CoreWar
1 parent dec5d11 commit 148e737

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

codeclash/games/corewar/corewar.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
from codeclash.games.game import CodeGame, RoundStats
66

77
COREWAR_LOG = "sim.log"
8-
COREWAR_FILE = "warriors/warrior.red"
98

109

1110
class CoreWarGame(CodeGame):
1211
name: str = "CoreWar"
1312
description: str = """CoreWar is a programming battle where you write "warriors" in an assembly-like language called Redcode to compete within a virtual machine (MARS), aiming to eliminate your rivals by making their code self-terminate.
1413
Victory comes from crafting clever tactics—replicators, scanners, bombers—that exploit memory layout and instruction timing to control the core."""
15-
submission: str = COREWAR_FILE
14+
submission: str = "warrior.red"
1615

1716
def __init__(self, config, **kwargs):
1817
super().__init__(config, **kwargs)
@@ -25,7 +24,7 @@ def __init__(self, config, **kwargs):
2524
self.run_cmd_round += f" -{arg} {val}"
2625

2726
def execute_round(self, agents: list[Player]):
28-
args = [f"/{agent.name}/{COREWAR_FILE}" for agent in agents]
27+
args = [f"/{agent.name}/{self.submission}" for agent in agents]
2928
cmd = (
3029
f"{self.run_cmd_round} {shlex.join(args)} "
3130
f"-r {self.game_config['sims_per_round']} "
@@ -69,6 +68,11 @@ def get_results(self, agents: list[Player], round_num: int, stats: RoundStats):
6968
stats.player_stats[player].score = score
7069

7170
def validate_code(self, agent: Player) -> tuple[bool, str | None]:
72-
if "warrior.red" not in agent.environment.execute("ls warriors")["output"]:
73-
return False, f"There should be a `{COREWAR_FILE}` file"
71+
if self.submission not in agent.environment.execute("ls")["output"]:
72+
return False, f"There should be a `{self.submission}` file"
73+
# Play game against a simple default bot to ensure it runs
74+
test_run_cmd = f"{self.run_cmd_round} {self.submission} /home/dwarf.red"
75+
test_run = agent.environment.execute(test_run_cmd)["output"]
76+
if any([l.startswith("Error:") for l in test_run.split("\n")]):
77+
return False, f"The `{self.submission}` file is malformed (Ran `{test_run_cmd}`):\n{test_run}"
7478
return True, None

docker/CoreWar.Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ RUN git clone https://${GITHUB_TOKEN}@github.com/emagedoc/CoreWar.git /workspace
1414
WORKDIR /workspace
1515

1616
RUN cd src/ && make CFLAGS="-O -DEXT94 -DPERMUTATE -DRWLIMIT" LIB=""
17+
18+
# Copy dwarf example to home directory for validation purposes
19+
RUN cp /workspace/doc/examples/dwarf.red /home/dwarf.red

0 commit comments

Comments
 (0)