diff --git a/examples/game_werewolf/werewolf_utils.py b/examples/game_werewolf/werewolf_utils.py index d0a91ce71..9b8b1e22f 100644 --- a/examples/game_werewolf/werewolf_utils.py +++ b/examples/game_werewolf/werewolf_utils.py @@ -39,14 +39,22 @@ def update_alive_players( def majority_vote(votes: list) -> Any: """majority_vote function""" - unit, counts = np.unique(votes, return_counts=True) + votes_valid = [item for item in votes if item != "Abstain"] + # Count the votes excluding abstentions. + unit, counts = np.unique(votes_valid, return_counts=True) return unit[np.argmax(counts)] def extract_name_and_id(name: str) -> tuple[str, int]: """extract player name and id from a string""" - name = re.search(r"\b[Pp]layer\d+\b", name).group(0) - idx = int(re.search(r"[Pp]layer(\d+)", name).group(1)) - 1 + try: + name = re.search(r"\b[Pp]layer\d+\b", name).group(0) + idx = int(re.search(r"[Pp]layer(\d+)", name).group(1)) - 1 + except AttributeError: + # In case Player remains silent or speaks to abstain. + logger.warning(f"vote: invalid name {name}, set to Abstain") + name = "Abstain" + idx = -1 return name, idx