1+ import re
12from typing import Any
23
4+ from codeclash .constants import RESULT_TIE
35from codeclash .games .abstract import CodeGame
46
57
@@ -10,13 +12,25 @@ def __init__(self, config):
1012 super ().__init__ (config )
1113 assert len (config ["players" ]) == 2 , "BattleCode is a two-player game"
1214 self .run_cmd_round : str = "python run.py run"
13-
15+ for arg , val in config .get ("args" , {}).items ():
16+ if isinstance (val , bool ):
17+ if val :
18+ self .run_cmd_round += f" --{ arg } "
19+ else :
20+ self .run_cmd_round += f" --{ arg } { val } "
21+
1422 def determine_winner (self , agents : list [Any ]):
15- response = self .environment .execute (f"tail -2 { self .round_log_path } " )
16- self .scoreboard .append ((self .round , "BLAH" )) # TODO
17-
23+ response = self .environment .execute (f"tail -3 { self .round_log_path } | head -1" )
24+ winner = re .search (r"\s\((.*)\)\swins\s\(" , response ["output" ]).group (1 )
25+ winner = {"A" : agents [0 ].name , "B" : agents [1 ].name }.get (winner , RESULT_TIE )
26+ self .scoreboard .append ((self .round , winner ))
27+
1828 def execute_round (self , agents : list [Any ]):
19- args = [f"/{ agent .name } /src/" for agent in agents ]
20- cmd = f"{ self .run_cmd_round } " # TODO
29+ args = [
30+ f" --p{ idx + 1 } -dir /{ agent .name } /src/ --p{ idx + 1 } { agent .name } "
31+ for idx , agent in enumerate (agents )
32+ ]
33+ cmd = f"{ self .run_cmd_round } { ' ' .join (args )} > { self .round_log_path } "
34+ print (f"Running command: { cmd } " )
2135 response = self .environment .execute (cmd )
22- assert response ["returncode" ] == 0 , response
36+ assert response ["returncode" ] == 0 , response
0 commit comments