Eric Botti commited on
Commit ·
ae85ba5
1
Parent(s): 057fb29
started game abstraction
Browse files- src/app.py +3 -3
- src/game.py +17 -10
- src/main.py +3 -3
src/app.py
CHANGED
|
@@ -3,7 +3,7 @@ from typing import Type
|
|
| 3 |
import streamlit as st
|
| 4 |
from streamlit import session_state
|
| 5 |
|
| 6 |
-
from game import
|
| 7 |
from agent_interfaces import HumanAgentInterface
|
| 8 |
from message import Message
|
| 9 |
from prompts import fetch_prompt, format_prompt
|
|
@@ -36,7 +36,7 @@ class StreamlitInterface(HumanAgentInterface):
|
|
| 36 |
return session_state.user_input
|
| 37 |
|
| 38 |
|
| 39 |
-
class
|
| 40 |
"""A Streamlit version of the Game class that uses a state machine to manage the game state."""
|
| 41 |
|
| 42 |
def run_game(self):
|
|
@@ -141,7 +141,7 @@ with center:
|
|
| 141 |
|
| 142 |
if user_input:
|
| 143 |
if "game" not in st.session_state:
|
| 144 |
-
st.session_state.game =
|
| 145 |
session_state.user_input = user_input
|
| 146 |
st.session_state.game.run_game()
|
| 147 |
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
from streamlit import session_state
|
| 5 |
|
| 6 |
+
from game import ChameleonGame
|
| 7 |
from agent_interfaces import HumanAgentInterface
|
| 8 |
from message import Message
|
| 9 |
from prompts import fetch_prompt, format_prompt
|
|
|
|
| 36 |
return session_state.user_input
|
| 37 |
|
| 38 |
|
| 39 |
+
class StreamlitChameleonGame(ChameleonGame):
|
| 40 |
"""A Streamlit version of the Game class that uses a state machine to manage the game state."""
|
| 41 |
|
| 42 |
def run_game(self):
|
|
|
|
| 141 |
|
| 142 |
if user_input:
|
| 143 |
if "game" not in st.session_state:
|
| 144 |
+
st.session_state.game = StreamlitChameleonGame(human_name=user_input, verbose=True, human_interface=StreamlitInterface)
|
| 145 |
session_state.user_input = user_input
|
| 146 |
st.session_state.game.run_game()
|
| 147 |
|
src/game.py
CHANGED
|
@@ -16,7 +16,23 @@ NUMBER_OF_PLAYERS = 6
|
|
| 16 |
WINNING_SCORE = 3
|
| 17 |
|
| 18 |
|
|
|
|
| 19 |
class Game:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
"""The main game class, handles the game logic and player interactions."""
|
| 21 |
|
| 22 |
winning_score = WINNING_SCORE
|
|
@@ -30,15 +46,8 @@ class Game:
|
|
| 30 |
verbose: bool = False,
|
| 31 |
debug: bool = False
|
| 32 |
):
|
|
|
|
| 33 |
# Instance Variables
|
| 34 |
-
self.game_id = game_id()
|
| 35 |
-
"""The unique id of the game."""
|
| 36 |
-
self.start_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 37 |
-
"""The time the game was started."""
|
| 38 |
-
self.verbose = verbose
|
| 39 |
-
"""If True, the game will display verbose messages to the player."""
|
| 40 |
-
self.debug = debug
|
| 41 |
-
"""If True, the game will display debug messages to the player."""
|
| 42 |
self.chameleon_ids: List[str] = []
|
| 43 |
"""Record of which player was the chameleon for each round."""
|
| 44 |
self.herd_animals: List[str] = []
|
|
@@ -49,8 +58,6 @@ class Game:
|
|
| 49 |
"""Record of what animal the chameleon guessed for each round."""
|
| 50 |
self.herd_vote_tallies: List[List[dict]] = []
|
| 51 |
"""Record of the votes of each herd member for the chameleon for each round."""
|
| 52 |
-
self.winner_id: str | None = None
|
| 53 |
-
"""The id of the player who has won the game."""
|
| 54 |
|
| 55 |
# Gather Player Names
|
| 56 |
if human_name:
|
|
|
|
| 16 |
WINNING_SCORE = 3
|
| 17 |
|
| 18 |
|
| 19 |
+
# Abstracting the Game Class is a WIP so that future games can be added
|
| 20 |
class Game:
|
| 21 |
+
"""Base class for all games."""
|
| 22 |
+
def __init__(self, verbose: bool = False, debug: bool = False):
|
| 23 |
+
self.game_id = game_id()
|
| 24 |
+
"""The unique id of the game."""
|
| 25 |
+
self.start_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 26 |
+
"""The time the game was started."""
|
| 27 |
+
self.verbose = verbose
|
| 28 |
+
"""If True, the game will display verbose messages to the player."""
|
| 29 |
+
self.debug = debug
|
| 30 |
+
"""If True, the game will display debug messages to the player."""
|
| 31 |
+
self.winner_id: str | None = None
|
| 32 |
+
"""The id of the player who has won the game."""
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class ChameleonGame(Game):
|
| 36 |
"""The main game class, handles the game logic and player interactions."""
|
| 37 |
|
| 38 |
winning_score = WINNING_SCORE
|
|
|
|
| 46 |
verbose: bool = False,
|
| 47 |
debug: bool = False
|
| 48 |
):
|
| 49 |
+
super().__init__(verbose, debug)
|
| 50 |
# Instance Variables
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
self.chameleon_ids: List[str] = []
|
| 52 |
"""Record of which player was the chameleon for each round."""
|
| 53 |
self.herd_animals: List[str] = []
|
|
|
|
| 58 |
"""Record of what animal the chameleon guessed for each round."""
|
| 59 |
self.herd_vote_tallies: List[List[dict]] = []
|
| 60 |
"""Record of the votes of each herd member for the chameleon for each round."""
|
|
|
|
|
|
|
| 61 |
|
| 62 |
# Gather Player Names
|
| 63 |
if human_name:
|
src/main.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from game import
|
| 2 |
from player import Player
|
| 3 |
import asyncio
|
| 4 |
from player import Player
|
|
@@ -8,9 +8,9 @@ def main():
|
|
| 8 |
name = input()
|
| 9 |
|
| 10 |
if name:
|
| 11 |
-
game =
|
| 12 |
else:
|
| 13 |
-
game =
|
| 14 |
|
| 15 |
asyncio.run(game.run_game())
|
| 16 |
|
|
|
|
| 1 |
+
from game import ChameleonGame
|
| 2 |
from player import Player
|
| 3 |
import asyncio
|
| 4 |
from player import Player
|
|
|
|
| 8 |
name = input()
|
| 9 |
|
| 10 |
if name:
|
| 11 |
+
game = ChameleonGame(human_name=name, verbose=True)
|
| 12 |
else:
|
| 13 |
+
game = ChameleonGame(verbose=True)
|
| 14 |
|
| 15 |
asyncio.run(game.run_game())
|
| 16 |
|