Spaces:
Runtime error
Runtime error
import streamlit as st | |
import random | |
import csv | |
import os | |
# Define the player card attributes | |
player_cards = { | |
"Player 1": { | |
"sketch": "๐ฉ", | |
"character": "Nurse", | |
"player_board": "๐ฅ", | |
"action_dice": "๐ฒ", | |
"health_tokens": "โค๏ธ", | |
"coin": "๐ฐ", | |
"battle_tokens": "โ๏ธ" | |
}, | |
"Player 2": { | |
"sketch": "๐จ", | |
"character": "Doctor", | |
"player_board": "๐ฅ", | |
"action_dice": "๐ฒ", | |
"health_tokens": "โค๏ธ", | |
"coin": "๐ฐ", | |
"battle_tokens": "โ๏ธ" | |
} | |
} | |
# Define the health problems | |
health_problems = ["Flu", "COVID-19", "Diabetes", "Heart Disease", "Cancer"] | |
# Define the game rules | |
attack_range = (1, 20) | |
defense_range = (1, 10) | |
# Create a function to play a single round of the game | |
def play_round(player_card, health_problem): | |
st.write(f"{player_card['sketch']} {player_card['character']} attacks {health_problem} with {player_card['action_dice']}...") | |
attack_score = random.randint(*attack_range) | |
defense_score = random.randint(*defense_range) | |
health_ferocity = random.randint(*attack_range) | |
health_resistance = random.randint(*defense_range) | |
if attack_score > health_resistance: | |
st.write(f"{player_card['sketch']} {player_card['character']} deals {attack_score - health_resistance} damage to {health_problem}!") | |
else: | |
st.write(f"{player_card['sketch']} {player_card['character']} misses the attack!") | |
if health_ferocity > defense_score: | |
st.write(f"{health_problem} deals {health_ferocity - defense_score} damage to {player_card['sketch']} {player_card['character']}!") | |
else: | |
st.write(f"{health_problem} fails to attack!") | |
# Create a function to play multiple rounds of the game | |
def play_game(num_games): | |
# Initialize the game state | |
player_scores = {player: 0 for player in player_cards} | |
health_problem_scores = {problem: 0 for problem in health_problems} | |
for i in range(num_games): | |
# Randomly select a player and health problem | |
player = random.choice(list(player_cards.keys())) | |
health_problem = random.choice(health_problems) | |
# Play the round | |
play_round(player_cards[player], health_problem) | |
# Update the scores | |
player_scores[player] += 1 | |
health_problem_scores[health_problem] += 1 | |
# Save the game state to a CSV file | |
with open("game_state.csv", "a", newline="") as f: | |
writer = csv.writer(f) | |
if os.stat("game_state.csv").st_size == 0: | |
writer.writerow(["Player", "Sketch", "Character", "Player Board", "Action Dice", "Health Tokens", "Coin", "Battle Tokens", "Score"]) | |
for player, attributes in player_cards.items(): | |
row = [player, attributes["sketch"], attributes["character"], attributes["player_board"], attributes["action_dice"], attributes["health_tokens"], attributes["coin"], attributes["battle_tokens"], player_scores[player]] | |
writer.writerow(row) | |
for problem in health_problems: | |
row = [problem, health_problem_scores[problem]] | |
writer.writerow(row) | |
# Display the game results | |
st.write("# Game Results") | |
for player, score in player_scores.items(): | |
st.write(f"{player}: {score} wins") | |
for problem, score in health_problem_scores.items(): | |
st.write(f"{problem}: {score} defeats") | |
# Display a button to download the game state CSV file | |
if os.path.exists("game_state.csv"): | |
st.write("# Download Game State") | |
files = [f for f in os.listdir(".") if os.path.isfile(f) and f.endswith(".csv")] | |
if "game_state.csv" in files: | |
files.remove("game_state.csv") | |
if len(files) > 0: | |
file_to_delete = st.selectbox("Select a file to delete", files) | |
if st.button("Delete File"): | |
os.remove(file_to_delete) | |
if st.button("Download Game State"): | |
with open("game_state.csv", "r") as f: | |
csv_data = f.read() | |
st.download_button("game_state.csv", csv_data, file_name="game_state.csv", mime="text/csv") | |
st.write("*Note: Downloaded files are saved in your browser's default download location*") | |
# Define the Streamlit app | |
def app(): | |
st.set_page_config(page_title="Health Care Game", page_icon="๐ฅ", layout="wide") | |
st.title("Health Care Game") | |
st.sidebar.write("# Game Settings") | |
num_games = st.sidebar.slider("Number of games to play", 1, 100, 10) | |
st.sidebar.write("# Player Cards") | |
for player, attributes in player_cards.items(): | |
st.sidebar.write(f"## {player}") | |
st.sidebar.write(f"Sketch: {attributes['sketch']}") | |
st.sidebar.write(f"Character: {attributes['character']}") | |
st.sidebar.write(f"Player Board: {attributes['player_board']}") | |
st.sidebar.write(f"Action Dice: {attributes['action_dice']}") | |
st.sidebar.write(f"Health Tokens: {attributes['health_tokens']}") | |
st.sidebar.write(f"Coin: {attributes['coin']}") | |
st.sidebar.write(f"Battle Tokens: {attributes['battle_tokens']}") | |
st.sidebar.write("# Health Problems") | |
for problem in health_problems: | |
st.sidebar.write(f"- {problem}") | |
# Start the game when the user clicks the "Play Game" button | |
if st.button("Play Game"): | |
play_game(num_games) | |
if __name__ == "__main__": | |
app() | |