Spaces:
Runtime error
Runtime error
File size: 4,901 Bytes
0432453 bcb150d 0432453 bcb150d 0432453 bcb150d 0432453 cc65c2d 0432453 cc65c2d b745733 0432453 b745733 c4f372c b745733 cc65c2d b745733 0432453 cc65c2d b745733 0432453 b745733 0432453 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
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, 'score': 0} 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)
# Calculate and update score
score = calculate_score(habitat, wildlife)
game_state['players'][player]['score'] += score
save_game_state(game_state)
# Function to change wildlife
def change_wildlife(player, game_state):
if game_state['players'][player]['wildlife']:
game_state['players'][player]['wildlife'][-1] = random.choice(wildlife_tokens)
save_game_state(game_state)
else:
st.warning(f"{player}, you have no wildlife to change!")
# 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)
# Display current score
st.write(f"Current Score: {game_state['players'][player]['score']}")
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!") |