# Import necessary libraries import streamlit as st import random import time from datetime import datetime # Constants HISTORY_LENGTH = 5 # Number of previous actions to display # Initialize game state def initialize_game(player_name): timestamp = datetime.now().strftime("%Y%m%d%H%M%S") with open(f"{timestamp}_{player_name}_state.txt", "w") as f: f.write(f"{player_name}:100,None,Player 1") with open(f"{timestamp}_{player_name}_actions.txt", "w") as f: f.write("") # Read game state def read_game_state(player_name, timestamp): with open(f"{timestamp}_{player_name}_state.txt", "r") as f: state = f.readline().split(',') player_name, hp, last_action, turn = state return player_name, int(hp), last_action, turn # Write player action def write_action(player_name, action, timestamp): with open(f"{timestamp}_{player_name}_actions.txt", "a") as f: f.write(f"{action}\n") # Update game state based on actions def update_game_state(player_name, timestamp): _, hp, _, turn = read_game_state(player_name, timestamp) # Read Player's action with open(f"{timestamp}_{player_name}_actions.txt", "r") as f: actions = f.readlines() last_action = actions[-1].strip() if actions else None # Update game based on actions if turn == player_name and last_action: if last_action == "Attack": damage = random.randint(5, 15) hp -= damage elif last_action == "Heal": heal = random.randint(5, 10) hp += heal # Write updated game state with open(f"{timestamp}_{player_name}_state.txt", "w") as f: f.write(f"{player_name}:{hp},{last_action},Player 1") # Get the last few actions def get_recent_actions(player_name, timestamp): with open(f"{timestamp}_{player_name}_actions.txt", "r") as f: actions = f.readlines() return actions[-HISTORY_LENGTH:] # Streamlit Interface def app(): st.title("Text Battle!") if "player_name" not in st.session_state: st.session_state.player_name = st.text_input("Enter your name:") if st.session_state.player_name: st.session_state.timestamp = datetime.now().strftime("%Y%m%d%H%M%S") initialize_game(st.session_state.player_name) if "player_name" in st.session_state: player_name, hp, last_action, turn = read_game_state(st.session_state.player_name, st.session_state.timestamp) st.write(f"{player_name} HP: {hp}") st.write(f"Last Action: {last_action}") if hp <= 0: st.write(f"{player_name} has been defeated!") del st.session_state.player_name st.stop() # Display recent actions st.write("Recent Actions:") for action in get_recent_actions(st.session_state.player_name, st.session_state.timestamp): st.write(action.strip()) # Player actions if turn == st.session_state.player_name: action = st.selectbox(f"{st.session_state.player_name}, choose your action:", ["", "Attack", "Heal", "Pass"]) if st.button("Submit"): write_action(st.session_state.player_name, action, st.session_state.timestamp) update_game_state(st.session_state.player_name, st.session_state.timestamp) # Display timer and refresh game state every 10 seconds st.write("Refreshing in 10 seconds...") time.sleep(10) st.experimental_rerun() # Run the Streamlit app app()