File size: 3,162 Bytes
fce2dad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Import necessary libraries
import streamlit as st
import random
import time

# Initialize game state and player names
def initialize_game():
    with open("game_state.txt", "w") as f:
        f.write("Player 1:100,Player 2:100,Player 1")
    with open("player1_actions.txt", "w") as f:
        f.write("")
    with open("player2_actions.txt", "w") as f:
        f.write("")

# Read game state
def read_game_state():
    with open("game_state.txt", "r") as f:
        state = f.readline().split(',')
        p1_name, p1_hp = state[0].split(':')
        p2_name, p2_hp = state[1].split(':')
        turn = state[2]
    return p1_name, int(p1_hp), p2_name, int(p2_hp), turn

# Write player action
def write_action(player, action):
    if player == "Player 1":
        with open("player1_actions.txt", "w") as f:
            f.write(action)
    else:
        with open("player2_actions.txt", "w") as f:
            f.write(action)

# Update game state based on actions
def update_game_state():
    p1_name, p1_hp, p2_name, p2_hp, turn = read_game_state()
    
    # Read Player 1's action
    with open("player1_actions.txt", "r") as f:
        p1_action = f.readline().strip()
    
    # Read Player 2's action
    with open("player2_actions.txt", "r") as f:
        p2_action = f.readline().strip()
    
    # Update game based on actions
    if turn == "Player 1" and p1_action:
        if p1_action == "Attack":
            damage = random.randint(5, 15)
            p2_hp -= damage
        elif p1_action == "Heal":
            heal = random.randint(5, 10)
            p1_hp += heal
        turn = "Player 2"
    elif turn == "Player 2" and p2_action:
        if p2_action == "Attack":
            damage = random.randint(5, 15)
            p1_hp -= damage
        elif p2_action == "Heal":
            heal = random.randint(5, 10)
            p2_hp += heal
        turn = "Player 1"
    
    # Write updated game state
    with open("game_state.txt", "w") as f:
        f.write(f"{p1_name}:{p1_hp},{p2_name}:{p2_hp},{turn}")

# Streamlit Interface
def app():
    st.title("Text Battle!")
    
    # Initialize game if not already initialized
    if not st.session_state.get("initialized"):
        initialize_game()
        st.session_state.initialized = True
    
    p1_name, p1_hp, p2_name, p2_hp, turn = read_game_state()
    
    st.write(f"{p1_name} HP: {p1_hp}")
    st.write(f"{p2_name} HP: {p2_hp}")
    
    if p1_hp <= 0:
        st.write(f"{p2_name} Wins!")
        st.stop()
    elif p2_hp <= 0:
        st.write(f"{p1_name} Wins!")
        st.stop()
    
    # Player actions
    if turn == "Player 1":
        action = st.selectbox("Player 1, choose your action:", ["", "Attack", "Heal", "Pass"])
        if st.button("Submit"):
            write_action("Player 1", action)
            update_game_state()
    else:
        action = st.selectbox("Player 2, choose your action:", ["", "Attack", "Heal", "Pass"])
        if st.button("Submit"):
            write_action("Player 2", action)
            update_game_state()
    
    # Refresh game state every 3 seconds
    time.sleep(3)
    st.experimental_rerun()

# Run the Streamlit app
app()