Eric Botti
commited on
Commit
·
dd5c856
1
Parent(s):
878e472
added chameleon guessing logic
Browse files- src/game.py +27 -10
- src/game_utils.py +12 -0
- src/models.py +17 -0
src/game.py
CHANGED
@@ -8,16 +8,16 @@ from parser import ParserKani
|
|
8 |
# Default Values
|
9 |
NUMBER_OF_PLAYERS = 5
|
10 |
|
11 |
-
class Game:
|
12 |
|
|
|
13 |
log_dir = os.path.join(os.pardir, "experiments")
|
14 |
player_log_file = "{game_id}-{role}-{player_num}.jsonl"
|
15 |
parser_log_file = "{game_id}-parser.jsonl"
|
16 |
|
17 |
def __init__(self,
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
|
22 |
# Game ID
|
23 |
self.game_id = game_id()
|
@@ -47,7 +47,8 @@ class Game:
|
|
47 |
else:
|
48 |
role = "herd"
|
49 |
|
50 |
-
log_path = os.path.join(self.log_dir,
|
|
|
51 |
|
52 |
self.players.append(Player(name, controller, role, log_filepath=log_path))
|
53 |
|
@@ -93,13 +94,31 @@ class Game:
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
# Phase III: Chameleon Guesses Animal or All Players Vote for Chameleon
|
99 |
if chameleon_will_guess:
|
100 |
# Chameleon Guesses Animal
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
else:
|
104 |
# All Players Vote for Chameleon
|
105 |
player_votes = []
|
@@ -137,5 +156,3 @@ class Game:
|
|
137 |
# Herd Wins by Correctly Guessing Chameleon - 2 points (each)
|
138 |
|
139 |
# Log Game Info
|
140 |
-
|
141 |
-
|
|
|
8 |
# Default Values
|
9 |
NUMBER_OF_PLAYERS = 5
|
10 |
|
|
|
11 |
|
12 |
+
class Game:
|
13 |
log_dir = os.path.join(os.pardir, "experiments")
|
14 |
player_log_file = "{game_id}-{role}-{player_num}.jsonl"
|
15 |
parser_log_file = "{game_id}-parser.jsonl"
|
16 |
|
17 |
def __init__(self,
|
18 |
+
human_name: str = None,
|
19 |
+
number_of_players: int = NUMBER_OF_PLAYERS
|
20 |
+
):
|
21 |
|
22 |
# Game ID
|
23 |
self.game_id = game_id()
|
|
|
47 |
else:
|
48 |
role = "herd"
|
49 |
|
50 |
+
log_path = os.path.join(self.log_dir,
|
51 |
+
self.player_log_file.format(game_id=self.game_id, role=role, player_num=i))
|
52 |
|
53 |
self.players.append(Player(name, controller, role, log_filepath=log_path))
|
54 |
|
|
|
94 |
self.player_responses.append({"sender": player.name, "response": output.description})
|
95 |
|
96 |
# Phase II: Chameleon Decides if they want to guess the animal (secretly)
|
97 |
+
prompt = fetch_prompt("chameleon_guess_decision")
|
98 |
+
|
99 |
+
response = await self.players[self.chameleon_index].respond_to(prompt)
|
100 |
+
output = await self.parser.parse(prompt, response, ChameleonGuessDecisionModel)
|
101 |
+
|
102 |
+
if output.decision == "guess":
|
103 |
+
chameleon_will_guess = True
|
104 |
+
else:
|
105 |
+
chameleon_will_guess = False
|
106 |
|
107 |
# Phase III: Chameleon Guesses Animal or All Players Vote for Chameleon
|
108 |
if chameleon_will_guess:
|
109 |
# Chameleon Guesses Animal
|
110 |
+
prompt = fetch_prompt("chameleon_guess_animal")
|
111 |
+
|
112 |
+
response = await self.players[self.chameleon_index].respond_to(prompt)
|
113 |
+
output = await self.parser.parse(prompt, response, ChameleonGuessAnimalModel)
|
114 |
+
|
115 |
+
if output.animal == herd_animal:
|
116 |
+
game_over = True
|
117 |
+
winner = "chameleon"
|
118 |
+
else:
|
119 |
+
game_over = True
|
120 |
+
winner = "herd"
|
121 |
+
|
122 |
else:
|
123 |
# All Players Vote for Chameleon
|
124 |
player_votes = []
|
|
|
156 |
# Herd Wins by Correctly Guessing Chameleon - 2 points (each)
|
157 |
|
158 |
# Log Game Info
|
|
|
|
src/game_utils.py
CHANGED
@@ -73,6 +73,16 @@ Previously Mentioned Descriptions:
|
|
73 |
{player_responses}
|
74 |
"""
|
75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
_vote_prompt = """\
|
77 |
Below are the responses from all players. Now it is time to vote. Choose from the players below who you think the Chameleon is.
|
78 |
{player_responses}
|
@@ -81,6 +91,8 @@ Below are the responses from all players. Now it is time to vote. Choose from th
|
|
81 |
prompts = {
|
82 |
"herd_animal": _game_rules + _herd_animal,
|
83 |
"chameleon_animal": _game_rules + _chameleon_animal,
|
|
|
|
|
84 |
"vote": _vote_prompt
|
85 |
}
|
86 |
|
|
|
73 |
{player_responses}
|
74 |
"""
|
75 |
|
76 |
+
_chameleon_guess_decision = """\
|
77 |
+
You now have the opportunity to guess what animal the other players are pretending to be.
|
78 |
+
If you guess correctly you will WIN, if you guess incorrectly you will LOSE.
|
79 |
+
If you believe you know what animal the other players are pretending to be make choose to GUESS, otherwise choose to PASS.
|
80 |
+
"""
|
81 |
+
|
82 |
+
_chameleon_guess_animal = """\
|
83 |
+
What animal do you think the other players are pretending to be?
|
84 |
+
"""
|
85 |
+
|
86 |
_vote_prompt = """\
|
87 |
Below are the responses from all players. Now it is time to vote. Choose from the players below who you think the Chameleon is.
|
88 |
{player_responses}
|
|
|
91 |
prompts = {
|
92 |
"herd_animal": _game_rules + _herd_animal,
|
93 |
"chameleon_animal": _game_rules + _chameleon_animal,
|
94 |
+
"chameleon_guess_decision": _chameleon_guess_decision,
|
95 |
+
"chameleon_guess_animal": _chameleon_guess_animal,
|
96 |
"vote": _vote_prompt
|
97 |
}
|
98 |
|
src/models.py
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
from pydantic import BaseModel, field_validator
|
2 |
|
3 |
MAX_DESCRIPTION_LEN = 10
|
@@ -31,6 +33,21 @@ class AnimalGuessModel(BaseModel):
|
|
31 |
animal_name: str
|
32 |
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
class VoteModel(BaseModel):
|
35 |
vote: str
|
36 |
|
|
|
1 |
+
from typing import Annotated
|
2 |
+
|
3 |
from pydantic import BaseModel, field_validator
|
4 |
|
5 |
MAX_DESCRIPTION_LEN = 10
|
|
|
33 |
animal_name: str
|
34 |
|
35 |
|
36 |
+
class ChameleonGuessDecisionModel(BaseModel):
|
37 |
+
decision: Annotated[str, "Must be one of: ['guess', 'pass']"]
|
38 |
+
|
39 |
+
@field_validator('decision')
|
40 |
+
@classmethod
|
41 |
+
def check_decision(cls, v) -> str:
|
42 |
+
if v.lower() not in ['guess', 'pass']:
|
43 |
+
raise ValueError("Decision must be one of: ['guess', 'pass']")
|
44 |
+
return v
|
45 |
+
|
46 |
+
|
47 |
+
class ChameleonGuessAnimalModel(BaseModel):
|
48 |
+
animal: str
|
49 |
+
|
50 |
+
|
51 |
class VoteModel(BaseModel):
|
52 |
vote: str
|
53 |
|