description:str='Halite is a multi-player turn-based strategy game where bots compete on a rectangular grid to capture territory and accumulate strength.\nPlayers control pieces that can move across the map to conquer neutral and enemy territory, with each cell providing production that increases the strength of pieces occupying it.\nThe goal is to control the most territory by the end of the game through strategic expansion, consolidation of forces, and tactical combat decisions.\n\nYou have the choice of writing your Halite bot in one of four programming languages: C, C++, OCaml, or Rust.\nExample implementations can be found under the `airesources/` folder.\nYour submission should be stored in the `submission/` folder. This folder currently contains an example C bot, but feel free to use any of the supported languages.\nPlease make sure your main file is named `main.<ext>`, where `<ext>` is the appropriate file extension for your chosen programming language.\nYou may include additional files as needed, but please ensure:\n1. The `submission/` folder contains only files relevant to your bot.\n2. The `submission/` folder ONLY contains a single bot (no multiple bots in one submission).\n3. Your bot can be compiled. See `runGame.sh` under the corresponding `submission/<language>/` folder to see how we will compile and run your bot.\n'
defget_results(self,agents:list[Player],round_num:int,stats:RoundStats):winners=[]pattern=r"Player\s#(\d+),\s(.*),\scame\sin\srank\s#(\d+)"foridxinrange(self.game_config["sims_per_round"]):log_file=self.log_round(round_num)/HALITE_LOG.format(idx=idx)withopen(log_file)asf:lines=f.readlines()[-len(agents)-1:]forlineinlines:match=re.search(pattern,line)ifmatch:player_idx=int(match.group(1))-1rank=int(match.group(3))ifrank==1:winners.append(agents[player_idx].name)# Count winswin_counts=Counter(winners)# Find all winners with the maximum countmax_wins=max(win_counts.values(),default=0)overall_winners=[nameforname,countinwin_counts.items()ifcount==max_wins]# Update statsstats.winner=RESULT_TIEiflen(overall_winners)>1elseoverall_winners[0]stats.scores=dict(win_counts)forplayer,scoreinwin_counts.items():ifplayer!=RESULT_TIE:stats.player_stats[player].score=score
defvalidate_code(self,agent:Player)->tuple[bool,str|None]:# Check that the `submission/` folder existsexists_output=agent.environment.execute("test -d submission && echo 'exists'")["output"]if"exists"!=exists_output.strip():returnFalse,f"Submission folder `{self.submission}/` does not exist"# Check that there is a *single* file called "main.<ext>" in the submission folder# and that <ext> is one of the supported file typessub_path=Path(agent.environment.config.cwd)/self.submissionls_output=agent.environment.execute("ls",cwd=sub_path)["output"]main_files=[fnameforfnameinls_output.splitlines()iffname.startswith("main.")andPath(fname).suffixinMAP_FILE_TYPE_TO_RUN]supported_exts="|".join(MAP_FILE_TYPE_TO_RUN.keys())iflen(main_files)!=1:return(False,f"Exactly one main.[{supported_exts}] file must be present in submission, found {len(main_files)}",)main_ext=Path(main_files[0]).suffix# Check that the submission compiles if necessaryifmain_extinMAP_FILE_TYPE_TO_COMPILE:compile_cmd=MAP_FILE_TYPE_TO_COMPILE[main_ext].format(path="main",name="main")try:compile_response=agent.environment.execute(compile_cmd,timeout=15,cwd=sub_path)exceptsubprocess.TimeoutExpired:returnFalse,f"Compilation failed (ran {compile_cmd} inside {self.submission}): timed out"ifcompile_response["returncode"]!=0:return(False,f"Compilation failed (ran {compile_cmd} inside {self.submission}): {compile_response['output']}",)# Check that submission runs in competitionexecutable=MAP_FILE_TYPE_TO_RUN[main_ext].format(path=self.submission,name="main")run_cmd=f"./environment/halite {shlex.join([executable,executable])}"try:run_response=agent.environment.execute(run_cmd,timeout=15)exceptsubprocess.TimeoutExpired:returnFalse,f"Submission failed to run (ran {run_cmd}): timed out"ifrun_response["returncode"]!=0:returnFalse,f"Submission failed to run (ran {run_cmd}): {run_response['output']}"# Record command to run executable to hidden fileexecutable_comp=MAP_FILE_TYPE_TO_RUN[main_ext].format(path=f"/{agent.name}/{self.submission}",name="main")agent.environment.execute(f'echo "{executable_comp}" > {HALITE_HIDDEN_EXEC}')returnTrue,None