Eric Botti commited on
Commit
878e472
·
1 Parent(s): 0c31321

vote counting + calc winner

Browse files
Files changed (2) hide show
  1. src/game.py +49 -49
  2. src/game_utils.py +13 -0
src/game.py CHANGED
@@ -66,22 +66,6 @@ class Game:
66
  """Returns the names of the players."""
67
  return [player.name for player in self.players]
68
 
69
- @staticmethod
70
- def player_action(prompt: str, player, validator: callable = None):
71
- """Prompts the player to take an action and validates the response."""
72
- max_attempts = 3
73
- response = player.respond_to(prompt)
74
-
75
- if validator:
76
- attempts = 0
77
- while not validator(response):
78
- attempts += 1
79
- if attempts >= max_attempts:
80
- raise ValueError(f"Player {player.name} did not provide a valid response to the following prompt:\n{prompt} Response: {response}")
81
- response = player.respond_to(prompt)
82
-
83
- return response
84
-
85
  async def start(self):
86
  """Starts the game."""
87
  # print("Welcome to Chameleon! This is a social deduction game powered by LLMs.")
@@ -89,47 +73,63 @@ class Game:
89
  self.player_responses = []
90
  herd_animal = random_animal()
91
 
92
- # Collect Player Animal Descriptions
93
- for player in self.players:
94
- if player.role == "chameleon":
95
- prompt_template = fetch_prompt("chameleon_animal")
96
- prompt = prompt_template.format(player_responses=self.format_responses())
97
- else:
98
- prompt_template = fetch_prompt("herd_animal")
99
- prompt = prompt_template.format(animal=herd_animal, player_responses=self.format_responses())
100
 
101
- # Get Player Animal Description
102
- response = await player.respond_to(prompt)
103
- # Parse Animal Description
104
- output = await self.parser.parse(prompt, response, AnimalDescriptionModel)
 
 
 
 
 
 
 
 
 
 
105
 
106
- self.player_responses.append({"sender": player.name, "response": output.description})
107
 
108
- # Show All Player Responses
 
109
 
110
- # Chameleon Decides if they want to guess the animal
111
- chameleon_will_guess = False
 
 
 
 
 
 
 
 
 
112
 
113
- if chameleon_will_guess:
114
- # Chameleon Guesses Animal
115
- # TODO: Add Chameleon Guessing Logic
116
- pass
117
- else:
118
- # All Players Vote for Chameleon
119
- player_votes = []
120
- for player in self.players:
121
- prompt_template = fetch_prompt("vote")
122
- prompt = prompt_template.format(player_responses=self.format_responses())
123
 
124
- # Get Player Vote
125
- response = await player.respond_to(prompt)
126
- # Parse Vote
127
- output = await self.parser.parse(prompt, response, VoteModel)
 
 
 
 
 
128
 
129
- # Add Vote to Player Votes
130
- player_votes.append(output.vote)
 
 
 
 
131
 
132
- print(player_votes)
133
 
134
  # Assign Points
135
  # Chameleon Wins - 3 Points
 
66
  """Returns the names of the players."""
67
  return [player.name for player in self.players]
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  async def start(self):
70
  """Starts the game."""
71
  # print("Welcome to Chameleon! This is a social deduction game powered by LLMs.")
 
73
  self.player_responses = []
74
  herd_animal = random_animal()
75
 
76
+ game_over = False
 
 
 
 
 
 
 
77
 
78
+ while not game_over:
79
+ # Phase I: Collect Player Animal Descriptions
80
+ for player in self.players:
81
+ if player.role == "chameleon":
82
+ prompt_template = fetch_prompt("chameleon_animal")
83
+ prompt = prompt_template.format(player_responses=self.format_responses())
84
+ else:
85
+ prompt_template = fetch_prompt("herd_animal")
86
+ prompt = prompt_template.format(animal=herd_animal, player_responses=self.format_responses())
87
+
88
+ # Get Player Animal Description
89
+ response = await player.respond_to(prompt)
90
+ # Parse Animal Description
91
+ output = await self.parser.parse(prompt, response, AnimalDescriptionModel)
92
 
93
+ self.player_responses.append({"sender": player.name, "response": output.description})
94
 
95
+ # Phase II: Chameleon Decides if they want to guess the animal (secretly)
96
+ chameleon_will_guess = False
97
 
98
+ # Phase III: Chameleon Guesses Animal or All Players Vote for Chameleon
99
+ if chameleon_will_guess:
100
+ # Chameleon Guesses Animal
101
+ # TODO: Add Chameleon Guessing Logic
102
+ pass
103
+ else:
104
+ # All Players Vote for Chameleon
105
+ player_votes = []
106
+ for player in self.players:
107
+ prompt_template = fetch_prompt("vote")
108
+ prompt = prompt_template.format(player_responses=self.format_responses())
109
 
110
+ # Get Player Vote
111
+ response = await player.respond_to(prompt)
112
+ # Parse Vote
113
+ output = await self.parser.parse(prompt, response, VoteModel)
 
 
 
 
 
 
114
 
115
+ # check if a valid player was voted for...
116
+
117
+ # Add Vote to Player Votes
118
+ player_votes.append(output.vote)
119
+
120
+ print(player_votes)
121
+
122
+ # Count Votes
123
+ accused_player = count_chameleon_votes(player_votes)
124
 
125
+ if accused_player:
126
+ game_over = True
127
+ if accused_player == self.players[self.chameleon_index].name:
128
+ winner = "herd"
129
+ else:
130
+ winner = "chameleon"
131
 
132
+ print(f"Game Over! The {winner} wins!")
133
 
134
  # Assign Points
135
  # Chameleon Wins - 3 Points
src/game_utils.py CHANGED
@@ -3,6 +3,7 @@ Utilities for the game including random selections and prompts.
3
  """
4
  import random
5
  import string
 
6
 
7
  ALPHABET = string.ascii_lowercase + string.digits
8
  ID_LENGTH = 8
@@ -30,6 +31,18 @@ def random_index(number_of_players : int) -> int:
30
  return random.randint(0, number_of_players - 1)
31
 
32
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  def fetch_prompt(prompt_name):
34
  return prompts[prompt_name]
35
 
 
3
  """
4
  import random
5
  import string
6
+ from collections import Counter
7
 
8
  ALPHABET = string.ascii_lowercase + string.digits
9
  ID_LENGTH = 8
 
31
  return random.randint(0, number_of_players - 1)
32
 
33
 
34
+ def count_chameleon_votes(player_votes: list[str]) -> str | None:
35
+ """Counts the votes for each player."""
36
+ freq = Counter(player_votes)
37
+ most_voted_player, number_of_votes = freq.most_common()[0]
38
+
39
+ # If one player has more than 50% of the votes, the herd accuses them of being the chameleon
40
+ if number_of_votes / len(player_votes) >= 0.5:
41
+ return most_voted_player
42
+ else:
43
+ return None
44
+
45
+
46
  def fetch_prompt(prompt_name):
47
  return prompts[prompt_name]
48