Eric Botti
commited on
Commit
·
5de0b8a
1
Parent(s):
7c01a62
adding game elements
Browse files- src/game.py +67 -0
- src/player.py +32 -0
- src/prompts.py +55 -0
src/game.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from prompts import fetch_prompt
|
2 |
+
from player import Player
|
3 |
+
|
4 |
+
|
5 |
+
class Game:
|
6 |
+
def __init__(self):
|
7 |
+
print("Game Created")
|
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 player in self.players:
|
17 |
+
player.add_message(message)
|
18 |
+
|
19 |
+
def format_responses(self, responses: list[str]) -> str:
|
20 |
+
"""Formats the responses of the players into a single string."""
|
21 |
+
return "\n".join(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 |
+
self.add_players([
|
32 |
+
Player("Player 1", "ai", "herd"),
|
33 |
+
Player("Player 2", "ai", "herd"),
|
34 |
+
Player("human_name", "human", "chameleon"),
|
35 |
+
Player("Player 4", "ai", "herd"),
|
36 |
+
Player("Player 5", "ai", "herd")
|
37 |
+
])
|
38 |
+
|
39 |
+
self.player_responses = []
|
40 |
+
herd_animal = "Giraffe"
|
41 |
+
|
42 |
+
# Collect Player Animal Descriptions
|
43 |
+
for player in self.players:
|
44 |
+
match player.role:
|
45 |
+
case "herd":
|
46 |
+
prompt_template = fetch_prompt("herd_animal")
|
47 |
+
prompt = prompt_template.format(animal=herd_animal, player_responses=self.player_responses)
|
48 |
+
|
49 |
+
case "chameleon":
|
50 |
+
prompt_template = fetch_prompt("chameleon_animal")
|
51 |
+
prompt = prompt_template.format(player_responses="")
|
52 |
+
|
53 |
+
response = player.collect_input(prompt)
|
54 |
+
self.player_responses.append(response)
|
55 |
+
|
56 |
+
self.player_votes = []
|
57 |
+
|
58 |
+
# Collect Player Votes
|
59 |
+
for player in self.players:
|
60 |
+
prompt_template = fetch_prompt("vote")
|
61 |
+
prompt = prompt_template.format(animal=herd_animal, player_responses=self.player_responses)
|
62 |
+
|
63 |
+
response = player.collect_input(prompt)
|
64 |
+
self.player_responses.append(response)
|
65 |
+
|
66 |
+
|
67 |
+
|
src/player.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from prompts import fetch_prompt
|
2 |
+
|
3 |
+
class Player:
|
4 |
+
def __init__(self, name: str, controller: str, role: str):
|
5 |
+
self.name = name
|
6 |
+
self.controller = controller
|
7 |
+
self.role = role
|
8 |
+
self.messages = []
|
9 |
+
|
10 |
+
def collect_input(self, prompt: str) -> str:
|
11 |
+
"""Store the input and output in the messages list. Return the output."""
|
12 |
+
self.messages.append(prompt)
|
13 |
+
output = self.respond(prompt)
|
14 |
+
self.messages.append(output)
|
15 |
+
return output
|
16 |
+
|
17 |
+
def respond(self, prompt: str) -> str:
|
18 |
+
if self.controller == "human":
|
19 |
+
print(prompt)
|
20 |
+
return input()
|
21 |
+
|
22 |
+
elif self.controller == "ai":
|
23 |
+
return "I am an AI and I am responding to the prompt."
|
24 |
+
|
25 |
+
def add_message(self, message: str):
|
26 |
+
"""Add a message to the messages list. No response required."""
|
27 |
+
self.messages.append(message)
|
28 |
+
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
|
src/prompts.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Prompts used for the game
|
3 |
+
"""
|
4 |
+
|
5 |
+
|
6 |
+
def fetch_prompt(prompt_name):
|
7 |
+
return prompts[prompt_name]
|
8 |
+
|
9 |
+
|
10 |
+
game_rules = '''\
|
11 |
+
GAME RULES: You are playing a social deduction game where every player pretends the be the same animal.
|
12 |
+
During the round players go around the room and make an "I"-statement as if they were the animal.
|
13 |
+
All players know what animal they are pretending to be, except one who is known as the Chameleon. The Chameleon and must blend in by providing details about the animal using context from other players.
|
14 |
+
The other players must be careful not to give away too much information with their responses so that Chameleon cannot guess the animal. After all players have spoken, they vote on who they think the Chameleon is.
|
15 |
+
WIN CONDITION:
|
16 |
+
- If the other players vote for the Chameleon, they win the game.
|
17 |
+
- If they vote for the wrong player then the Chameleon wins the game.
|
18 |
+
|
19 |
+
'''
|
20 |
+
|
21 |
+
herd_animal = """\
|
22 |
+
You are a {animal}. In 10 words or less give a description of yourself starting with "I". The description should not give away too much information about the {animal} as you do not want the Chameleon to be able to guess what animal you are. Do not repeat responses from other players.
|
23 |
+
Previously Mentioned Descriptions:
|
24 |
+
{player_responses}
|
25 |
+
Your Response:
|
26 |
+
"""
|
27 |
+
|
28 |
+
chameleon_animal = """\
|
29 |
+
You are the Chameleon. Initially, you don't know what animal the other players are, your goal is to deduce . Using the context that they have provided, In 10 words or less give a description of yourself starting with "I". If no one else has said anything try to say something generic that could be true of any animals.
|
30 |
+
Previously Mentioned Descriptions:
|
31 |
+
{player_responses}
|
32 |
+
Your Response:
|
33 |
+
"""
|
34 |
+
|
35 |
+
|
36 |
+
vote_prompt = """\
|
37 |
+
The players have spoken, now it is time to vote. Choose from the players below who you think the Chameleon is.
|
38 |
+
{players}
|
39 |
+
"""
|
40 |
+
|
41 |
+
herd_vote = """\
|
42 |
+
Who do you think the Chameleon is?
|
43 |
+
"""
|
44 |
+
|
45 |
+
chameleon_prompt = """\
|
46 |
+
You are the Chameleon, vote for someone else to remove suspicion from yourself.
|
47 |
+
"""
|
48 |
+
|
49 |
+
|
50 |
+
prompts = {
|
51 |
+
"herd_animal": game_rules + herd_animal,
|
52 |
+
"chameleon_animal": game_rules + chameleon_animal,
|
53 |
+
"vote_prompt": vote_prompt
|
54 |
+
}
|
55 |
+
|