Skip to content

Commit

Permalink
save and resume game state at will
Browse files Browse the repository at this point in the history
  • Loading branch information
Luncenok committed Oct 22, 2024
1 parent 54e5ba4 commit 65e695e
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 195 deletions.
38 changes: 4 additions & 34 deletions scratchpad/game_engine_refractoring_plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
**Phase 2: Continue Game Functionality**

1. **Modify `enter_main_game_loop`:**
- Add a parameter `continue_from_state` to `enter_main_game_loop`.
- If `continue_from_state` is provided, call `load_state` to load the game state from the specified file.
- If `continue_from_state` is not provided, start a new game as usual.
- Remove the `continue_from_state` parameter as it's always assumed the game will load from a state.
- Directly call `load_state` at the beginning of the function to load the game state from the predefined file.

2. **Update Game Loop:**
- Modify the game loop to use the loaded `round_number` and `current_player_index` if a state was loaded.
- Ensure that the game loop continues from the correct round and player's turn.
- Use the loaded `round_number` and `current_player_index` to control the game loop.
- Instead of starting from `round_number = 0`, use the loaded `round_number` as the starting point.
- Use `current_player_index` to determine which player should act next.
- After each player's turn, increment `current_player_index` and wrap around to 0 if it reaches the end of the player list.

Expand All @@ -36,35 +35,6 @@
- Provide a mechanism for users to select a saved state and continue playing from that point.
- Display the current round number and the player whose turn it is in the GUI.

**Example Code Snippets:**

```python
# src/game/game_engine.py

class GameEngine(BaseModel):
# ... other attributes ...
round_number: int = 0
current_player_index: int = 0

def save_state(self, filename: str) -> None:
with open(filename, "w") as f:
json.dump(self.dict(), f)

def load_state(self, filename: str) -> None:
with open(filename, "r") as f:
data = json.load(f)
self.__dict__.update(data)

def enter_main_game_loop(self, continue_from_state: Optional[str] = None) -> None:
if continue_from_state:
self.load_state(continue_from_state)
# ... rest of the game loop ...
self.round_number += 1
current_player = self.state.players[self.current_player_index]
# ... get actions from current_player ...
self.current_player_index = (self.current_player_index + 1) % len(self.state.players)
# ... rest of the game loop ...
```

**Additional Considerations:**

Expand Down
6 changes: 4 additions & 2 deletions src/demo_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def main():
model_name = "gpt-4o-mini" # Or any other suitable model name

player_names = ["Wateusz", "Waciej", "Warek", "Wojtek", "Wafał", "Wymek"]
players = [FakeAIPlayer(name=player_names[i], llm_model_name=model_name) for i in range(3)]
players = [FakeAIPlayer(name=player_names[i], llm_model_name="fake") for i in range(6)]
game_engine.load_players(players, impostor_count=1)
game_engine.init_game()

Expand All @@ -36,8 +36,10 @@ def main():
# json_game_state.players[0].state.tasks[0] = "DONE"
# gui_handler.update_gui(json_game_state)

game_engine.enter_main_game_loop()
while not game_engine.perform_step():
gui_handler.update_gui(game_engine.state)
gui_handler.update_gui(game_engine.state)
st.text("Game has ended")
st.json(game_engine.to_dict(), expanded=False) # Display final game state
st.text("\n".join(game_engine.state.playthrough)) # Display final game log

Expand Down
1 change: 1 addition & 0 deletions src/game/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
NUM_CHATS = 5
MIN_NAME = 2
IMPOSTOR_COOLDOWN = 1
STATE_FILE = "game_state.json"
PLAYER_COLORS = [
"red",
"blue",
Expand Down
Loading

0 comments on commit 65e695e

Please sign in to comment.