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):scores,wins=defaultdict(int),defaultdict(int)foridxinrange(len(agents)):shift=agents[idx:]+agents[:idx]# Shift agents by idx to match simulation orderwithopen(self.log_round(round_num)/COREWAR_LOG.format(idx=idx))asf:result_output=f.read()# Get the last n lines which contain the scores (closer to original)lines=result_output.strip().split("\n")relevant_lines=lines[-len(shift)*2:]iflen(lines)>=len(shift)*2elselinesrelevant_lines=[lforlinrelevant_linesiflen(l.strip())>0]# Go through each line; score position is correlated with agent indexfori,lineinenumerate(relevant_lines):match=re.search(r".*\sby\s.*\sscores\s(\d+)",line)ifmatch:scores[shift[i].name]+=int(match.group(1))# Last line corresponds to absolute number of winslast=relevant_lines[-1][len("Results:"):].strip()fori,winenumerate(last.split()[:-1]):# NOTE: Omitting ties (last entry)wins[shift[i].name]+=int(w)iflen(wins)!=len(agents):# Should not happenself.logger.error(f"Have {len(wins)} wins but {len(agents)} agents")# Bookkeepingstats.scores={a.name:wins[a.name]forainagents}forainagents:stats.player_stats[a.name].score=wins[a.name]# Determine overall winner by highest wins, then highest scoremax_wins=max(wins.values(),default=0)potential_winners=[nameforname,winwins.items()ifw==max_wins]iflen(potential_winners)==1:stats.winner=potential_winners[0]else:# Tie-break by scoremax_score=-1winner=RESULT_TIEfornameinpotential_winners:ifscores[name]>max_score:max_score=scores[name]winner=nameelifscores[name]==max_score:winner=RESULT_TIEstats.winner=winner
Source code in codeclash/arenas/corewar/corewar.py
97 98 99100101102103104105
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