File size: 7,472 Bytes
8037461
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import streamlit as st
import random
import time

st.markdown("""
    <style>
    body {
        background-color: #000000;
        color: #ffffff;
    }
    .sidebar .sidebar-content {
        background-color: #000000;
        color: #ffffff;
    }
    h1, h2, h3, h4, h5, h6, p, a, li, .stText, .stMarkdown, .stButton {
        color: #ffffff;
    }
    /* Hide the Streamlit header and footer */
    #MainMenu {visibility: hidden;}
    footer {visibility: hidden;}
    </style>
    """, unsafe_allow_html=True)


# Initialize session state if not already done
if 'labels' not in st.session_state:
    st.session_state.labels = ['Speed', 'Power', 'Flight', 'Intelligence', 'Strength', 'Stamina', 'Magic']
if 'stats' not in st.session_state:
    st.session_state.stats = [70, 60, 50, 80, 90, 75, 85]
if 'cpu_choice' not in st.session_state:
    st.session_state.cpu_choice = None
if 'result' not in st.session_state:
    st.session_state.result = ""
if 'player_health' not in st.session_state:
    st.session_state.player_health = 100
if 'cpu_health' not in st.session_state:
    st.session_state.cpu_health = 100
if 'rounds_played' not in st.session_state:
    st.session_state.rounds_played = 0
if 'player_wins' not in st.session_state:
    st.session_state.player_wins = 0
if 'cpu_wins' not in st.session_state:
    st.session_state.cpu_wins = 0
if 'used_labels' not in st.session_state:
    st.session_state.used_labels = []
if 'status' not in st.session_state:
    st.session_state.status = "Fight!"

# Define attribute interactions
interactions = {
    'Speed': ['Power', 'Stamina'],
    'Power': ['Flight', 'Intelligence'],
    'Flight': ['Strength', 'Magic'],
    'Intelligence': ['Speed', 'Magic'],
    'Strength': ['Power', 'Stamina'],
    'Stamina': ['Intelligence', 'Flight'],
    'Magic': ['Strength', 'Speed']
}

# Function to make CPU choice
def cpu_select():
    if st.session_state.labels:
        return random.choice(st.session_state.labels)
    return None

# Function to determine the winner
def determine_winner(player_choice, cpu_choice):
    if cpu_choice in interactions[player_choice]:
        st.session_state.cpu_health -= 20
        st.session_state.player_wins += 1
        return f"Player Wins! πŸŽ‰ {player_choice} {cpu_choice}"
    elif player_choice in interactions[cpu_choice]:
        st.session_state.player_health -= 20
        st.session_state.cpu_wins += 1
        return f"CPU Wins! πŸ€– {player_choice} {cpu_choice}"
    else:
        # Coin toss to resolve tie
        coin_toss = random.choice(['Player', 'CPU'])
        if coin_toss == 'Player':
            st.session_state.cpu_health -= 20
            st.session_state.player_wins += 1
            return "It's a Tie! Coin toss decides: Player Wins! πŸŽ‰"
        else:
            st.session_state.player_health -= 20
            st.session_state.cpu_wins += 1
            return "It's a Tie! Coin toss decides: CPU Wins! πŸ€–"

st.sidebar.image("ai_logo.PNG", width=160)
# Display health bars in the sidebar
st.sidebar.markdown("### Health Bars")
st.sidebar.write(f"Player Health: {'❀️' * (st.session_state.player_health // 20)}")
st.sidebar.write(f"CPU Health: {'πŸ€–' * (st.session_state.cpu_health // 20)}")

# Display rounds played and check for game over
st.sidebar.write(f"Rounds Played: {st.session_state.rounds_played}/5")

if st.session_state.rounds_played >= 5 or not st.session_state.labels:
    st.sidebar.markdown("### Game Over")
    if st.session_state.player_wins > st.session_state.cpu_wins:
        st.sidebar.write("Final Result: Player Wins! πŸŽ‰")
    elif st.session_state.cpu_wins > st.session_state.player_wins:
        st.sidebar.write("Final Result: CPU Wins! πŸ€–")
    else:
        st.sidebar.write("Final Result: It's a Tie! 🀝")
    
    # Reset game
    if st.sidebar.button("Reset Game"):
        st.session_state.labels = ['Speed', 'Power', 'Flight', 'Intelligence', 'Strength', 'Stamina', 'Magic']
        st.session_state.stats = [70, 60, 50, 80, 90, 75, 85]
        st.session_state.cpu_choice = None
        st.session_state.result = ""
        st.session_state.player_health = 100
        st.session_state.cpu_health = 100
        st.session_state.rounds_played = 0
        st.session_state.player_wins = 0
        st.session_state.cpu_wins = 0
        st.session_state.used_labels = []
        st.session_state.status = "Fight!"
else:
    st.sidebar.write(f"Round: {st.session_state.rounds_played}/5")
    st.sidebar.write(f"CPU move: {st.session_state.cpu_choice}")

# Display the current labels and stats
st.markdown("## AI Fighter - Choose Your Attribute")
st.text(st.session_state.labels)
st.text(st.session_state.stats)

# Player selects a stat
player_choice = st.radio("Select an attribute:", options=st.session_state.labels)

if st.button("Fight! βš”οΈ"):
    if player_choice in st.session_state.used_labels:
        st.warning("You cannot select the same attribute twice. Please choose another one.")
    else:
        st.session_state.used_labels.append(player_choice)
        st.session_state.status = "Fighting... βš”οΈ"
        with st.spinner("Battling...πŸ‘Š"):
            time.sleep(3)  # Wait for 3 seconds
        st.session_state.cpu_choice = cpu_select()
        if st.session_state.cpu_choice is not None:
            st.session_state.result = determine_winner(player_choice, st.session_state.cpu_choice)
            st.session_state.rounds_played += 1
            # Remove the selected attribute and its stat
            index = st.session_state.labels.index(player_choice)
            st.session_state.labels.pop(index)
            st.session_state.stats.pop(index)
            st.session_state.status = "Fight End!"
            
        st.rerun()

# Display fight sequence and status
st.sidebar.markdown("### Status")
st.sidebar.markdown(f"## {st.session_state.status}")
if st.session_state.status == "Fight End!":
    st.sidebar.markdown(f"## {st.session_state.result}")

col1, col2 = st.columns(2)
st.markdown("""
    <style>
    .small-font {
        font-size: 12px;
    }
    </style>
    """, unsafe_allow_html=True)
with col1:
    st.image("ai_logo2.gif", width=280)
    # Display summary of the game logic
    st.markdown("### Summary of the Game Logic")
    st.markdown("""
        - **Player and CPU each select an attribute.**
        - **Random stat is generated.**
        - **Winner is determined based on who is closer to the random stat.**
        - **Tie resolved by a coin toss.**
        - **Loser’s health is reduced.**
        - **Game ends after 5 rounds or when no attributes are left.**
        - **Final result is displayed, and the game can be reset.**
        """)
    st.image("legend1.PNG", width=300)
with col2:
    st.image("ai_logo2a.gif", width=280)   

# Display attributes and their interactions
    st.markdown("### Attributes and their Interactions:")
    st.markdown("""
        - **Speed ⚑ beats Power πŸ’ͺ and Stamina πŸƒ.**
        - **Power πŸ’ͺ beats Flight ✈️ and Intelligence 🧠.**
        - **Flight ✈️ beats Strength πŸ”¨ and Magic ✨.**
        - **Intelligence 🧠 beats Speed ⚑ and Magic ✨.**
        - **Strength πŸ”¨ beats Power πŸ’ͺ and Stamina πŸƒ.**
        - **Stamina πŸƒ beats Intelligence 🧠 and Flight ✈️.**
        - **Magic ✨ beats Strength πŸ”¨ and Speed ⚑.**
        - **Otherwise tie and coin toss win/forfeit **
        """)
    st.image("legend.PNG", width=320) 
st.info("**Built by dw v2 **")