Spaces:
Runtime error
Runtime error
import streamlit as st | |
import random | |
import pandas as pd | |
import os | |
# Cascadia Game Components | |
habitat_tiles = ['π²', 'ποΈ', 'π', 'π΅', 'π'] | |
wildlife_tokens = ['π»', 'π¦ ', 'π', 'π¦', 'πΏοΈ'] | |
players = ['Player 1', 'Player 2'] | |
save_file = 'cascadia_game_state.csv' | |
# Function to load game state from CSV | |
def load_game_state(): | |
if os.path.exists(save_file): | |
df = pd.read_csv(save_file) | |
game_state = { | |
'habitat_stack': df['habitat_stack'].dropna().tolist(), | |
'wildlife_stack': df['wildlife_stack'].dropna().tolist(), | |
'players': {} | |
} | |
for player in players: | |
game_state['players'][player] = { | |
'habitat': df[player + '_habitat'].dropna().tolist(), | |
'wildlife': df[player + '_wildlife'].dropna().tolist(), | |
'nature_tokens': int(df[player + '_nature_tokens'][0]) | |
} | |
return game_state | |
else: | |
return None | |
# Function to save game state to CSV | |
def save_game_state(game_state): | |
data = { | |
'habitat_stack': pd.Series(game_state['habitat_stack']), | |
'wildlife_stack': pd.Series(game_state['wildlife_stack']) | |
} | |
for player in players: | |
player_state = game_state['players'][player] | |
data[player + '_habitat'] = pd.Series(player_state['habitat']) | |
data[player + '_wildlife'] = pd.Series(player_state['wildlife']) | |
data[player + '_nature_tokens'] = pd.Series([player_state['nature_tokens']]) | |
df = pd.DataFrame(data) | |
df.to_csv(save_file, index=False) | |
# Initialize or load game state | |
game_state = load_game_state() | |
if game_state is None: | |
game_state = { | |
'habitat_stack': random.sample(habitat_tiles * 10, 50), | |
'wildlife_stack': random.sample(wildlife_tokens * 10, 50), | |
'players': {player: {'habitat': [], 'wildlife': [], 'nature_tokens': 3} for player in players} | |
} | |
save_game_state(game_state) | |
# Streamlit Interface | |
st.title("π² Cascadia Lite π²") | |
# Function to draw habitat and wildlife | |
def draw_habitat_and_wildlife(player, game_state): | |
habitat = game_state['habitat_stack'].pop() | |
wildlife = game_state['wildlife_stack'].pop() | |
game_state['players'][player]['habitat'].append(habitat) | |
game_state['players'][player]['wildlife'].append(wildlife) | |
save_game_state(game_state) | |
# Display players' areas and handle actions | |
col1, col2 = st.columns(2) | |
for index, player in enumerate(players): | |
with (col1 if index == 0 else col2): | |
st.write(f"## {player}'s Play Area") | |
player_data = pd.DataFrame({ | |
'Habitat Tiles': game_state['players'][player]['habitat'], | |
'Wildlife Tokens': game_state['players'][player]['wildlife'] | |
}) | |
st.dataframe(player_data) | |
# Drafting Phase | |
if st.button(f"{player}: Draw Habitat and Wildlife"): | |
draw_habitat_and_wildlife(player, game_state) | |
game_state = load_game_state() | |
# Tile and Wildlife Placement | |
placement_options = ['Place Habitat', 'Place Wildlife', 'Skip'] | |
placement_choice = st.selectbox(f"{player}: Choose an action", placement_options, key=f'placement_{player}') | |
if placement_choice != 'Skip': | |
st.write(f"{player} chose to {placement_choice}") | |
# Nature Tokens | |
nature_tokens = game_state['players'][player]['nature_tokens'] | |
if st.button(f"{player}: Use a Nature Token ({nature_tokens} left)"): | |
if nature_tokens > 0: | |
# Logic to use a nature token | |
st.write(f"{player} used a Nature Token!") | |
game_state['players'][player]['nature_tokens'] -= 1 | |
save_game_state(game_state) | |
else: | |
st.warning("No Nature Tokens left!") | |
# Reset Button | |
if st.button("Reset Game"): | |
os.remove(save_file) # Delete the save file | |
game_state = { | |
'habitat_stack': random.sample(habitat_tiles * 10, 50), | |
'wildlife_stack': random.sample(wildlife_tokens * 10, 50), | |
'players': {player: {'habitat': [], 'wildlife': [], 'nature_tokens': 3} for player in players} | |
} | |
save_game_state(game_state) | |
st.experimental_rerun() | |
# Game Controls and Instructions | |
st.write("## Game Controls") | |
st.write("Use the buttons and select boxes to play the game!") |