Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure the game proceeds smoothly when some players declare abstentio… #272

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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