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.\nVictory comes from crafting clever tactics—replicators, scanners, bombers—that exploit memory layout and instruction timing to control the core.'
defget_results(self,agents:list[Player],round_num:int,stats:RoundStats):withopen(self.log_round(round_num)/COREWAR_LOG)asf:result_output=f.read()self.logger.debug(f"Determining winner from result output: {result_output}")scores=[]n=len(agents)*2lines=result_output.strip().split("\n")# Get the last n lines which contain the scores (closer to original)relevant_lines=lines[-n:]iflen(lines)>=nelselinesrelevant_lines=[lforlinrelevant_linesiflen(l.strip())>0]self.logger.debug(f"Relevant lines for scoring: {relevant_lines}")# Go through each line; we assume score position is correlated with agent indexforlineinrelevant_lines:match=re.search(r".*\sby\s.*\sscores\s(\d+)",line)ifmatch:score=int(match.group(1))scores.append(score)ifscores:iflen(scores)!=len(agents):self.logger.error(f"Have {len(scores)} scores but {len(agents)} agents")stats.winner=agents[scores.index(max(scores))].namestats.scores={agent.name:scoreforagent,scoreinzip(agents,scores)}else:self.logger.debug("No scores found, returning unknown")stats.winner="unknown"stats.scores={agent.name:0foragentinagents}forplayer,scoreinstats.scores.items():stats.player_stats[player].score=score
Source code in codeclash/arenas/corewar/corewar.py
707172737475767778
defvalidate_code(self,agent:Player)->tuple[bool,str|None]:ifself.submissionnotinagent.environment.execute("ls")["output"]:returnFalse,f"There should be a `{self.submission}` file"# Play game against a simple default bot to ensure it runstest_run_cmd=f"{self.run_cmd_round}{self.submission} /home/dwarf.red"test_run=agent.environment.execute(test_run_cmd,timeout=60)["output"]ifany([l.startswith("Error")forlintest_run.split("\n")]):returnFalse,f"The `{self.submission}` file is malformed (Ran `{test_run_cmd}`):\n{test_run}"returnTrue,None