Skip to content

Commit

Permalink
Ensure werewolf game proceeds smoothly when some players abstain (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskyJx authored Jun 5, 2024
1 parent d2d84e8 commit 580e2f8
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions examples/game_werewolf/werewolf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit 580e2f8

Please sign in to comment.