Eric Botti commited on
Commit
6605c56
·
1 Parent(s): f975b0f

added more animals

Browse files
Files changed (2) hide show
  1. src/game_chameleon.py +43 -23
  2. src/game_utils_chameleon.py +0 -28
src/game_chameleon.py CHANGED
@@ -1,17 +1,15 @@
1
- from typing import Optional, Type
2
 
3
  from game_utils import random_index
4
- from game_utils_chameleon import *
5
  from output_formats import *
6
  from player import ChameleonPlayer, Player
7
  from prompts import fetch_prompt, format_prompt
8
- from message import Message, MessageType
9
- from agent_interfaces import HumanAgentCLI, OpenAIAgentInterface, HumanAgentInterface
10
  from game import Game
11
 
12
  # Default Values
13
  NUMBER_OF_PLAYERS = 6
14
- WINNING_SCORE = 3
15
 
16
 
17
  class ChameleonGame(Game):
@@ -21,6 +19,8 @@ class ChameleonGame(Game):
21
 
22
  winning_score = WINNING_SCORE
23
  """The Number of points required to win the game."""
 
 
24
 
25
  def __init__(self, *args, **kwargs):
26
 
@@ -42,7 +42,7 @@ class ChameleonGame(Game):
42
  """Record of the votes of each herd member for the chameleon for each round."""
43
 
44
  @property
45
- def chameleon(self) -> Player:
46
  """Returns the current chameleon."""
47
  return self.player_from_id(self.chameleon_ids[-1])
48
 
@@ -71,17 +71,6 @@ class ChameleonGame(Game):
71
  """Returns the current round number."""
72
  return len(self.herd_animals)
73
 
74
- def format_animal_descriptions(self, exclude: Player = None) -> str:
75
- """Formats the animal description responses of the players into a single string."""
76
- formatted_responses = ""
77
- for response in self.round_animal_descriptions:
78
- # Used to exclude the player who is currently responding, so they don't vote for themselves like a fool
79
- if response["player_id"] != exclude.id:
80
- player = self.player_from_id(response["player_id"])
81
- formatted_responses += f" - {player.name}: {response['description']}\n"
82
-
83
- return formatted_responses
84
-
85
  def run_game(self):
86
  """Starts the game."""
87
 
@@ -103,6 +92,9 @@ class ChameleonGame(Game):
103
  if max(points) >= self.winning_score:
104
  self.game_state = "game_end"
105
  self.winner_id = self.players[points.index(max(points))].id
 
 
 
106
  else:
107
  self.game_state = "setup_round"
108
  # Go back to start
@@ -111,9 +103,6 @@ class ChameleonGame(Game):
111
  random.shuffle(self.players)
112
  self.run_game()
113
 
114
- if self.game_state == "game_end":
115
- self.game_message(f"The game is over {self.winner_id} has won!")
116
-
117
  def run_round(self):
118
  """Starts the round."""
119
 
@@ -152,7 +141,7 @@ class ChameleonGame(Game):
152
  def setup_round(self):
153
  """Sets up the round. This includes assigning roles and gathering player names."""
154
  # Choose Animal
155
- herd_animal = random_animal()
156
  self.herd_animals.append(herd_animal)
157
  self.debug_message(f"The secret animal is {herd_animal}.")
158
 
@@ -249,7 +238,7 @@ class ChameleonGame(Game):
249
  voted_for = self.player_from_id(vote["voted_for_id"])
250
  self.game_message(f"{voter.name} voted for {voted_for.name}")
251
 
252
- accused_player_id = count_chameleon_votes(self.herd_vote_tally)
253
 
254
  self.game_message(f"The round is over. Calculating results...")
255
  self.game_message(
@@ -283,4 +272,35 @@ class ChameleonGame(Game):
283
 
284
  # Print Scores
285
  player_points = "\n".join([f"{player.name}: {player.points}" for player in self.players])
286
- self.game_message(f"Current Game Score:\n{player_points}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from collections import Counter
2
 
3
  from game_utils import random_index
 
4
  from output_formats import *
5
  from player import ChameleonPlayer, Player
6
  from prompts import fetch_prompt, format_prompt
7
+
 
8
  from game import Game
9
 
10
  # Default Values
11
  NUMBER_OF_PLAYERS = 6
12
+ WINNING_SCORE = 6
13
 
14
 
15
  class ChameleonGame(Game):
 
19
 
20
  winning_score = WINNING_SCORE
21
  """The Number of points required to win the game."""
22
+ available_animals = ["Dog", "Cat", "Mouse", "Hamster", "Monkey", "Rabbit", "Fox", "Bear", "Panda", "Koala", "Tiger", "Lion", "Cow", "Pig", "Frog", "Owl", "Duck", "Chicken", "Butterfly", "Turtle", "Snake", "Octopus", "Squid", "Hedgehog", "Elephant", "Rhinoceros", "Zebra", "Crocodile", "Whale", "Dolphin", "Camel", "Giraffe", "Deer", "Gorilla", "Goat", "Llama", "Horse", "Unicorn", "Flamingo", "Skunk", "Shark"]
23
+ """The list of animals that can be chosen as the secret animal."""
24
 
25
  def __init__(self, *args, **kwargs):
26
 
 
42
  """Record of the votes of each herd member for the chameleon for each round."""
43
 
44
  @property
45
+ def chameleon(self) -> ChameleonPlayer:
46
  """Returns the current chameleon."""
47
  return self.player_from_id(self.chameleon_ids[-1])
48
 
 
71
  """Returns the current round number."""
72
  return len(self.herd_animals)
73
 
 
 
 
 
 
 
 
 
 
 
 
74
  def run_game(self):
75
  """Starts the game."""
76
 
 
92
  if max(points) >= self.winning_score:
93
  self.game_state = "game_end"
94
  self.winner_id = self.players[points.index(max(points))].id
95
+ winner = self.player_from_id(self.winner_id)
96
+ self.game_message(f"The game is over {winner.name} has won!")
97
+
98
  else:
99
  self.game_state = "setup_round"
100
  # Go back to start
 
103
  random.shuffle(self.players)
104
  self.run_game()
105
 
 
 
 
106
  def run_round(self):
107
  """Starts the round."""
108
 
 
141
  def setup_round(self):
142
  """Sets up the round. This includes assigning roles and gathering player names."""
143
  # Choose Animal
144
+ herd_animal = self.random_animal()
145
  self.herd_animals.append(herd_animal)
146
  self.debug_message(f"The secret animal is {herd_animal}.")
147
 
 
238
  voted_for = self.player_from_id(vote["voted_for_id"])
239
  self.game_message(f"{voter.name} voted for {voted_for.name}")
240
 
241
+ accused_player_id = self.count_chameleon_votes(self.herd_vote_tally)
242
 
243
  self.game_message(f"The round is over. Calculating results...")
244
  self.game_message(
 
272
 
273
  # Print Scores
274
  player_points = "\n".join([f"{player.name}: {player.points}" for player in self.players])
275
+ self.game_message(f"Current Game Score:\n{player_points}")
276
+
277
+ def random_animal(self) -> str:
278
+ """Returns a random animal from the list of available animals, and removes it from the list."""
279
+ animal = random.choice(self.available_animals)
280
+ self.available_animals.remove(animal)
281
+ return animal
282
+
283
+ @staticmethod
284
+ def count_chameleon_votes(player_votes: list[dict]) -> str | None:
285
+ """Counts the votes for each player."""
286
+ votes = [vote['voted_for_id'] for vote in player_votes]
287
+
288
+ freq = Counter(votes)
289
+ most_voted_player, number_of_votes = freq.most_common()[0]
290
+
291
+ # If one player has more than 50% of the votes, the herd accuses them of being the chameleon
292
+ if number_of_votes / len(player_votes) >= 0.5:
293
+ return most_voted_player
294
+ else:
295
+ return None
296
+
297
+ def format_animal_descriptions(self, exclude: Player = None) -> str:
298
+ """Formats the animal description responses of the players into a single string."""
299
+ formatted_responses = ""
300
+ for response in self.round_animal_descriptions:
301
+ # Used to exclude the player who is currently responding, so they don't vote for themselves like a fool
302
+ if response["player_id"] != exclude.id:
303
+ player = self.player_from_id(response["player_id"])
304
+ formatted_responses += f" - {player.name}: {response['description']}\n"
305
+
306
+ return formatted_responses
src/game_utils_chameleon.py DELETED
@@ -1,28 +0,0 @@
1
- """
2
- Utilities for the game including random selections and prompts.
3
- """
4
- import random
5
- import string
6
- import json
7
- from collections import Counter
8
-
9
-
10
- def random_animal():
11
- return random.choice(available_animals)
12
-
13
-
14
- available_animals = ["Giraffe", "Elephant", "Lion", "Zebra", "Monkey", "Gorilla"]
15
-
16
-
17
- def count_chameleon_votes(player_votes: list[dict]) -> str | None:
18
- """Counts the votes for each player."""
19
- votes = [vote['voted_for_id'] for vote in player_votes]
20
-
21
- freq = Counter(votes)
22
- most_voted_player, number_of_votes = freq.most_common()[0]
23
-
24
- # If one player has more than 50% of the votes, the herd accuses them of being the chameleon
25
- if number_of_votes / len(player_votes) >= 0.5:
26
- return most_voted_player
27
- else:
28
- return None