Spaces:
Sleeping
Sleeping
import streamlit as st | |
import random | |
# Set up the app title and description | |
st.set_page_config(page_title="Rock Paper Scissors", page_icon="βοΈ", layout="centered") | |
st.title("Rock Paper Scissors Game βποΈβοΈ") | |
st.markdown(""" | |
Play a game of **Rock-Paper-Scissors** against the computer! | |
Choose your move below, and see if you can outscore the machine. | |
""") | |
# Initialize session state variables 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 | |
# Choices for the game | |
choices = ["Rock", "Paper", "Scissors"] | |
# Mapping choices to emojis | |
emoji_map = {"Rock": "β", "Paper": "ποΈ", "Scissors": "βοΈ"} | |
# Function to determine the winner and update scores | |
def determine_winner(user_choice, computer_choice): | |
if user_choice == computer_choice: | |
return "It's a tie! π€" | |
elif ( | |
(user_choice == "Rock" and computer_choice == "Scissors") or | |
(user_choice == "Paper" and computer_choice == "Rock") or | |
(user_choice == "Scissors" and computer_choice == "Paper") | |
): | |
st.session_state['user_score'] += 1 | |
return "You win! π" | |
else: | |
st.session_state['computer_score'] += 1 | |
return "You lose! π’" | |
# Display current scores | |
st.sidebar.header("Scores") | |
st.sidebar.write(f"**Your Score:** {st.session_state['user_score']}") | |
st.sidebar.write(f"**Computer's Score:** {st.session_state['computer_score']}") | |
# User's choice | |
user_choice = st.radio("Choose your move:", choices) | |
# Generate computer's choice and play the game | |
if st.button("Play"): | |
computer_choice = random.choice(choices) | |
# Display choices | |
st.write(f"**You chose:** {emoji_map[user_choice]} {user_choice}") | |
st.write(f"**Computer chose:** {emoji_map[computer_choice]} {computer_choice}") | |
# Determine the winner | |
result = determine_winner(user_choice, computer_choice) | |
st.subheader(result) | |
# Reset Scores Button | |
if st.sidebar.button("Reset Scores"): | |
st.session_state['user_score'] = 0 | |
st.session_state['computer_score'] = 0 | |
st.sidebar.success("Scores have been reset!") | |