Eric Botti commited on
Commit
f975b0f
·
1 Parent(s): 9b424c4

merged streamlit and cli games

Browse files
Files changed (3) hide show
  1. src/app.py +1 -67
  2. src/game_chameleon.py +63 -36
  3. src/main.py +1 -3
src/app.py CHANGED
@@ -37,72 +37,6 @@ class StreamlitInterface(HumanAgentInterface):
37
  return response
38
 
39
 
40
- class StreamlitChameleonGame(ChameleonGame):
41
- """A Streamlit version of the Game class that uses a state machine to manage the game state."""
42
-
43
- def run_game(self):
44
- """Starts the game."""
45
-
46
- # Check if the game has not been won
47
- if self.game_state != "game_end":
48
- if self.game_state == "game_start":
49
- self.game_message(fetch_prompt("game_rules"))
50
- self.game_state = "setup_round"
51
- if self.game_state == "setup_round":
52
- self.setup_round()
53
- self.game_state = "animal_description"
54
- if self.game_state in ["animal_description", "chameleon_guess", "herd_vote"]:
55
- self.run_round()
56
- if self.game_state == "resolve_round":
57
- self.resolve_round()
58
-
59
- points = [player.points for player in self.players]
60
-
61
- if max(points) >= self.winning_score:
62
- self.game_state = "game_end"
63
- self.winner_id = self.players[points.index(max(points))].id
64
- else:
65
- self.game_state = "setup_round"
66
- self.run_game()
67
-
68
- if self.game_state == "game_end":
69
- self.game_message(f"The game is over {self.winner_id} has won!")
70
-
71
-
72
- def run_round(self):
73
- """Starts the round."""
74
-
75
- # Phase I: Collect Player Animal Descriptions
76
- if self.game_state == "animal_description":
77
- for current_player in self.players:
78
- if current_player.id not in [animal_description['player_id'] for animal_description in self.round_animal_descriptions]:
79
-
80
- response = self.player_turn_animal_description(current_player)
81
-
82
- if not response:
83
- break
84
-
85
- if len(self.round_animal_descriptions) == len(self.players):
86
- self.game_state = "chameleon_guess"
87
-
88
- # Phase II: Chameleon Guesses the Animal
89
- if self.game_state == "chameleon_guess":
90
- self.player_turn_chameleon_guess(self.chameleon)
91
-
92
- # Phase III: The Herd Votes for who they think the Chameleon is
93
- if self.game_state == "herd_vote":
94
- for current_player in self.players:
95
- if current_player.role == "herd" and current_player.id not in [vote['voter_id'] for vote in self.herd_vote_tally]:
96
-
97
- response = self.player_turn_herd_vote(current_player)
98
-
99
- if not response:
100
- break
101
-
102
- if len(self.herd_vote_tally) == len(self.players) - 1:
103
- self.game_state = "resolve_round"
104
-
105
-
106
  # Streamlit App
107
 
108
  margin_size = 1
@@ -130,7 +64,7 @@ with center:
130
 
131
  if user_input:
132
  if "game" not in st.session_state:
133
- st.session_state.game = StreamlitChameleonGame.from_human_name(user_input, StreamlitInterface)
134
  else:
135
  session_state.user_input = user_input
136
 
 
37
  return response
38
 
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  # Streamlit App
41
 
42
  margin_size = 1
 
64
 
65
  if user_input:
66
  if "game" not in st.session_state:
67
+ st.session_state.game = ChameleonGame.from_human_name(user_input, StreamlitInterface)
68
  else:
69
  session_state.user_input = user_input
70
 
src/game_chameleon.py CHANGED
@@ -41,10 +41,8 @@ class ChameleonGame(Game):
41
  self.herd_vote_tallies: List[List[dict]] = []
42
  """Record of the votes of each herd member for the chameleon for each round."""
43
 
44
-
45
-
46
  @property
47
- def chameleon(self) -> ChameleonPlayer:
48
  """Returns the current chameleon."""
49
  return self.player_from_id(self.chameleon_ids[-1])
50
 
@@ -84,23 +82,72 @@ class ChameleonGame(Game):
84
 
85
  return formatted_responses
86
 
87
- async def run_game(self):
88
- """Sets up the game. This includes assigning roles and gathering player names."""
89
- self.game_message(fetch_prompt("game_rules"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
- self.setup_round()
 
 
 
 
92
 
93
- self.run_round()
94
 
95
- self.resolve_round()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
- # # Log Game Info
98
- # game_log = {
99
- # "game_id": self.game_id,
100
- # "start_time": self.start_time,
101
- # "number_of_players": len(self.players),
102
- # "human_player": self.players[self.human_index].id if self.human_index else "None",
103
- # }
104
 
105
  def setup_round(self):
106
  """Sets up the round. This includes assigning roles and gathering player names."""
@@ -130,26 +177,6 @@ class ChameleonGame(Game):
130
 
131
  self.game_message(f"Each player will now take turns describing themselves:")
132
 
133
- def run_round(self):
134
- """Starts the round."""
135
- # Phase I: Collect Player Animal Descriptions
136
- for current_player in self.players:
137
- self.game_message(fetch_prompt("player_describe_animal"), current_player)
138
- self.player_turn_animal_description(current_player)
139
-
140
- # Phase II: Chameleon Guesses the Animal
141
- self.game_message("All players have spoken. The Chameleon will now guess the secret animal...")
142
- player_responses = self.format_animal_descriptions(exclude=self.chameleon)
143
- self.game_message(format_prompt("chameleon_guess_animal", player_responses=player_responses), self.chameleon)
144
- self.player_turn_chameleon_guess(self.chameleon)
145
-
146
- # Phase III: The Herd Votes for who they think the Chameleon is
147
- for current_player in self.players:
148
- if current_player.role == "herd":
149
- player_responses = self.format_animal_descriptions(exclude=current_player)
150
- self.game_message(format_prompt("vote", player_responses=player_responses), current_player)
151
- self.player_turn_herd_vote(current_player)
152
-
153
  def player_turn_animal_description(self, player: Player):
154
  """Handles a player's turn to describe themselves."""
155
  if not self.awaiting_input:
 
41
  self.herd_vote_tallies: List[List[dict]] = []
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
 
 
82
 
83
  return formatted_responses
84
 
85
+ def run_game(self):
86
+ """Starts the game."""
87
+
88
+ # Check if the game has not been won
89
+ if self.game_state != "game_end":
90
+ if self.game_state == "game_start":
91
+ self.game_message(fetch_prompt("game_rules"))
92
+ self.game_state = "setup_round"
93
+ if self.game_state == "setup_round":
94
+ self.setup_round()
95
+ self.game_state = "animal_description"
96
+ if self.game_state in ["animal_description", "chameleon_guess", "herd_vote"]:
97
+ self.run_round()
98
+ if self.game_state == "resolve_round":
99
+ self.resolve_round()
100
+
101
+ points = [player.points for player in self.players]
102
+
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
109
+ self.game_message(f"No player has won yet, the game will end when a player reaches {self.winning_score} points.")
110
+ self.game_message(f"Starting a new round...")
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
 
120
+ # Phase I: Collect Player Animal Descriptions
121
+ if self.game_state == "animal_description":
122
+ for current_player in self.players:
123
+ if current_player.id not in [animal_description['player_id'] for animal_description in
124
+ self.round_animal_descriptions]:
125
 
126
+ response = self.player_turn_animal_description(current_player)
127
 
128
+ if not response:
129
+ break
130
+
131
+ if len(self.round_animal_descriptions) == len(self.players):
132
+ self.game_state = "chameleon_guess"
133
+
134
+ # Phase II: Chameleon Guesses the Animal
135
+ if self.game_state == "chameleon_guess":
136
+ self.player_turn_chameleon_guess(self.chameleon)
137
+
138
+ # Phase III: The Herd Votes for who they think the Chameleon is
139
+ if self.game_state == "herd_vote":
140
+ for current_player in self.players:
141
+ if current_player.role == "herd" and current_player.id not in [vote['voter_id'] for vote in
142
+ self.herd_vote_tally]:
143
 
144
+ response = self.player_turn_herd_vote(current_player)
145
+
146
+ if not response:
147
+ break
148
+
149
+ if len(self.herd_vote_tally) == len(self.players) - 1:
150
+ self.game_state = "resolve_round"
151
 
152
  def setup_round(self):
153
  """Sets up the round. This includes assigning roles and gathering player names."""
 
177
 
178
  self.game_message(f"Each player will now take turns describing themselves:")
179
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
  def player_turn_animal_description(self, player: Player):
181
  """Handles a player's turn to describe themselves."""
182
  if not self.awaiting_input:
src/main.py CHANGED
@@ -1,7 +1,5 @@
1
  from game_chameleon import ChameleonGame
2
  from player import Player
3
- import asyncio
4
- from player import Player
5
 
6
  def main():
7
  print("Please Enter your name, or leave blank to run an AI only game")
@@ -9,7 +7,7 @@ def main():
9
 
10
  game = ChameleonGame.from_human_name(name)
11
 
12
- asyncio.run(game.run_game())
13
 
14
 
15
  if __name__ == "__main__":
 
1
  from game_chameleon import ChameleonGame
2
  from player import Player
 
 
3
 
4
  def main():
5
  print("Please Enter your name, or leave blank to run an AI only game")
 
7
 
8
  game = ChameleonGame.from_human_name(name)
9
 
10
+ game.run_game()
11
 
12
 
13
  if __name__ == "__main__":