Skip to content

Commit 9beb0fb

Browse files
committed
Fix Battlesnake to handle ties
1 parent a4d71b9 commit 9beb0fb

1 file changed

Lines changed: 7 additions & 11 deletions

File tree

codeclash/games/battlesnake/main.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,16 @@ def __init__(self, config, *, tournament_id: str, local_output_dir: Path):
2424
self.run_cmd_round += f" --{arg} {val}"
2525

2626
def get_stats(self, result_outputs: list[str], agents: list[Player]) -> RoundStats:
27-
winners = []
27+
scores = {}
2828
for ro in result_outputs:
2929
lines = ro.strip().split("\n")
30-
last_line = lines[-1] if lines else "" # Get the last line which contains the game result
31-
winner = json.loads(last_line)["winnerName"]
32-
winners.append(winner)
30+
results = json.loads(lines[-1]) if lines else {} # Get the last line which contains the game result
31+
winner = RESULT_TIE if results["isDraw"] else results["winnerName"]
32+
scores[winner] = scores.get(winner, 0) + 1
3333

34-
win_counts = {agent.name: winners.count(agent.name) for agent in agents}
35-
max_wins = max(win_counts.values())
36-
winners = [name for name, wins in win_counts.items() if wins == max_wins]
37-
return RoundStats(
38-
winner=RESULT_TIE if len(winners) > 1 else winners[0],
39-
scores=win_counts,
40-
)
34+
winner = max(scores, key=scores.get)
35+
winner = RESULT_TIE if list(scores.values()).count(scores[winner]) > 1 else winner
36+
return RoundStats(winner=winner, scores=scores)
4137

4238
def execute_round(self, agents: list[Player]) -> RoundData:
4339
cmd = []

0 commit comments

Comments
 (0)