Eric Botti commited on
Commit ·
3ea5035
1
Parent(s): ab29f8e
added validation functions, mapped advanced phases
Browse files- src/game.py +88 -40
- src/main.py +5 -1
src/game.py
CHANGED
|
@@ -1,49 +1,67 @@
|
|
| 1 |
-
from game_utils import fetch_prompt, random_animal, random_names
|
| 2 |
from player import Player
|
| 3 |
|
|
|
|
|
|
|
| 4 |
|
| 5 |
class Game:
|
| 6 |
-
def __init__(self
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
self.players = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def add_players(self, players: list[Player]):
|
| 11 |
-
"""Adds players to the game."""
|
| 12 |
-
self.players = players
|
| 13 |
|
| 14 |
def broadcast(self, message):
|
| 15 |
"""Sends a message to all the players, no response required."""
|
| 16 |
-
for
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
def format_responses(self) -> str:
|
| 20 |
"""Formats the responses of the players into a single string."""
|
| 21 |
-
return "\n".join(self.player_responses)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
def start(self):
|
| 24 |
"""Starts the game."""
|
| 25 |
print("Welcome to Chameleon! This is a social deduction game powered by LLMs.")
|
| 26 |
|
| 27 |
-
if not self.players:
|
| 28 |
-
print("Enter your name to begin")
|
| 29 |
-
human_name = input()
|
| 30 |
-
|
| 31 |
-
player_names = random_names(4)
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
for name in player_names:
|
| 35 |
-
self.add_players([
|
| 36 |
-
Player(name, "ai", "herd")
|
| 37 |
-
])
|
| 38 |
-
|
| 39 |
-
self.add_players([
|
| 40 |
-
Player("Player 1", "ai", "herd"),
|
| 41 |
-
Player("Player 2", "ai", "herd"),
|
| 42 |
-
Player(human_name, "human", "chameleon"),
|
| 43 |
-
Player("Player 4", "ai", "herd"),
|
| 44 |
-
Player("Player 5", "ai", "herd")
|
| 45 |
-
])
|
| 46 |
-
|
| 47 |
self.player_responses = []
|
| 48 |
herd_animal = random_animal()
|
| 49 |
|
|
@@ -59,17 +77,47 @@ class Game:
|
|
| 59 |
prompt = prompt_template.format(player_responses=self.format_responses())
|
| 60 |
|
| 61 |
response = player.collect_input(prompt)
|
| 62 |
-
self.player_responses.append(response)
|
| 63 |
|
| 64 |
self.player_votes = []
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from game_utils import fetch_prompt, random_animal, random_names, random_index
|
| 2 |
from player import Player
|
| 3 |
|
| 4 |
+
# Default Values
|
| 5 |
+
NUMBER_OF_PLAYERS = 5
|
| 6 |
|
| 7 |
class Game:
|
| 8 |
+
def __init__(self,
|
| 9 |
+
human_name: str = None,
|
| 10 |
+
number_of_players: int = NUMBER_OF_PLAYERS
|
| 11 |
+
):
|
| 12 |
+
|
| 13 |
+
# Gather Player Names
|
| 14 |
+
if human_name:
|
| 15 |
+
ai_names = random_names(number_of_players - 1)
|
| 16 |
+
self.human_index = random_index(number_of_players)
|
| 17 |
+
else:
|
| 18 |
+
ai_names = random_names(number_of_players)
|
| 19 |
+
|
| 20 |
+
# Choose Chameleon
|
| 21 |
+
self.chameleon_index = random_index(number_of_players)
|
| 22 |
+
|
| 23 |
+
# Add Players
|
| 24 |
self.players = []
|
| 25 |
+
for i in range(0, number_of_players):
|
| 26 |
+
if self.human_index == i:
|
| 27 |
+
name = human_name
|
| 28 |
+
controller = "human"
|
| 29 |
+
else:
|
| 30 |
+
name = ai_names.pop()
|
| 31 |
+
controller = "ai"
|
| 32 |
+
|
| 33 |
+
if self.chameleon_index == i:
|
| 34 |
+
role = "chameleon"
|
| 35 |
+
else:
|
| 36 |
+
role = "herd"
|
| 37 |
+
|
| 38 |
+
self.players.append(Player(name, controller, role))
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
self.player_responses = []
|
| 42 |
+
|
| 43 |
+
print("Game Created")
|
| 44 |
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
def broadcast(self, message):
|
| 47 |
"""Sends a message to all the players, no response required."""
|
| 48 |
+
for player_index in range(0, len(self.players)):
|
| 49 |
+
self.players[player_index].add_message(message)
|
| 50 |
+
if self.human_index == player_index:
|
| 51 |
+
print(message)
|
| 52 |
|
| 53 |
def format_responses(self) -> str:
|
| 54 |
"""Formats the responses of the players into a single string."""
|
| 55 |
+
return "\n".join([f" - {response['sender']}: {response['response']}" for response in self.player_responses])
|
| 56 |
+
|
| 57 |
+
def get_player_names(self) -> list[str]:
|
| 58 |
+
"""Returns the names of the players."""
|
| 59 |
+
return [player.name for player in self.players]
|
| 60 |
|
| 61 |
def start(self):
|
| 62 |
"""Starts the game."""
|
| 63 |
print("Welcome to Chameleon! This is a social deduction game powered by LLMs.")
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
self.player_responses = []
|
| 66 |
herd_animal = random_animal()
|
| 67 |
|
|
|
|
| 77 |
prompt = prompt_template.format(player_responses=self.format_responses())
|
| 78 |
|
| 79 |
response = player.collect_input(prompt)
|
| 80 |
+
self.player_responses.append({"sender": player.name, "response": response})
|
| 81 |
|
| 82 |
self.player_votes = []
|
| 83 |
|
| 84 |
+
# Show All Player Responses
|
| 85 |
+
self.broadcast(self.format_responses())
|
| 86 |
+
|
| 87 |
+
# Chameleon Decides if they want to guess the animal
|
| 88 |
+
# TODO: Add Chameleon Guess Decision Logic
|
| 89 |
+
chameleon_will_guess = False
|
| 90 |
+
|
| 91 |
+
if chameleon_will_guess:
|
| 92 |
+
# Chameleon Guesses Animal
|
| 93 |
+
# TODO: Add Chameleon Guessing Logic
|
| 94 |
+
pass
|
| 95 |
+
else:
|
| 96 |
+
# All Players Vote for Chameleon
|
| 97 |
+
for player in self.players:
|
| 98 |
+
prompt_template = fetch_prompt("vote")
|
| 99 |
+
prompt = prompt_template.format(players=self.get_player_names())
|
| 100 |
+
|
| 101 |
+
response = player.collect_input(prompt)
|
| 102 |
+
self.player_responses.append(response)
|
| 103 |
+
|
| 104 |
+
# Assign Points
|
| 105 |
+
# Chameleon Wins - 3 Points
|
| 106 |
+
# Herd Wins by Failed Chameleon Guess - 1 Point (each)
|
| 107 |
+
# Herd Wins by Correctly Guessing Chameleon - 2 points (each)
|
| 108 |
+
|
| 109 |
+
@staticmethod
|
| 110 |
+
def validate_animal_description(self, description: str) -> bool:
|
| 111 |
+
"""Validates that the description starts with I and is less than 10 words."""
|
| 112 |
+
if not description.startswith("I"):
|
| 113 |
+
return False
|
| 114 |
+
|
| 115 |
+
if len(description.split(" ")) > 10:
|
| 116 |
+
return False
|
| 117 |
+
|
| 118 |
+
return True
|
| 119 |
+
|
| 120 |
+
def validate_vote(self, vote: str) -> bool:
|
| 121 |
+
"""Validates that the vote is for a valid player."""
|
| 122 |
+
player_names = [player.name.lower() for player in self.players]
|
| 123 |
+
return vote.lower() in player_names
|
src/main.py
CHANGED
|
@@ -2,7 +2,11 @@ from game import Game
|
|
| 2 |
from player import Player
|
| 3 |
|
| 4 |
def main():
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
game.start()
|
| 7 |
|
| 8 |
|
|
|
|
| 2 |
from player import Player
|
| 3 |
|
| 4 |
def main():
|
| 5 |
+
print("Please Enter your name:")
|
| 6 |
+
name = input()
|
| 7 |
+
|
| 8 |
+
game = Game(human_name=name)
|
| 9 |
+
|
| 10 |
game.start()
|
| 11 |
|
| 12 |
|