import streamlit as st
import random
import time
st.markdown("""
""", 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("""
""", 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 **")