Abdullah-Basar's picture
Update app.py
beb29de verified
import streamlit as st
import random
# Title
st.title("🎮 Rock-Paper-Scissors Game 🎮")
# Sidebar for Instructions
st.sidebar.title("How to Play")
st.sidebar.write("""
1. Choose your move: Rock, Paper, or Scissors.
2. Click the "Play" button to see the result.
3. Earn points by beating the computer!
- Rock beats Scissors.
- Scissors beats Paper.
- Paper beats Rock.
""")
# Initialize session state for scores
if "user_score" not in st.session_state:
st.session_state.user_score = 0
if "computer_score" not in st.session_state:
st.session_state.computer_score = 0
# User input
st.subheader("Choose your move:")
choices = ["Rock", "Paper", "Scissors"]
user_choice = st.selectbox("Select your move:", choices)
# Play the game
if st.button("Play"):
computer_choice = random.choice(choices)
# Determine the winner
if user_choice == computer_choice:
result = "It's a Tie! 🤝"
elif (user_choice == "Rock" and computer_choice == "Scissors") or \
(user_choice == "Scissors" and computer_choice == "Paper") or \
(user_choice == "Paper" and computer_choice == "Rock"):
result = "You Win! 🎉"
st.session_state.user_score += 1
else:
result = "You Lose! 😢"
st.session_state.computer_score += 1
# Display results
st.write(f"**Your Choice:** {user_choice}")
st.write(f"**Computer's Choice:** {computer_choice}")
st.subheader(result)
# Display Scores
st.write("---")
st.subheader("Scores")
col1, col2 = st.columns(2)
with col1:
st.write(f"**Your Score:** {st.session_state.user_score}")
with col2:
st.write(f"**Computer's Score:** {st.session_state.computer_score}")
# Reset scores
if st.button("Reset Scores"):
st.session_state.user_score = 0
st.session_state.computer_score = 0
st.success("Scores have been reset!")
# Footer
st.write("---")
st.markdown("💻 **Developed by Abdullah**")