rehanafzal's picture
Update app.py
0af8bf3 verified
# rock_paper_scissors.py
import random
import streamlit as st
# Initialize session state for points
if "user_points" not in st.session_state:
st.session_state.user_points = 0
if "computer_points" not in st.session_state:
st.session_state.computer_points = 0
if "rounds_played" not in st.session_state:
st.session_state.rounds_played = 0
# Game choices
choices = ["Rock", "Paper", "Scissors"]
# Define the logic for the game
def get_winner(user_choice, computer_choice):
if user_choice == computer_choice:
return "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"):
return "user"
else:
return "computer"
# Streamlit app UI
st.title("Rock, Paper, Scissors Game with Points!")
st.write("๐ŸŽฎ Play against the computer and try to score the most points!")
# User choice
user_choice = st.selectbox("Your choice:", choices)
if st.button("Play"):
# Computer choice
computer_choice = random.choice(choices)
# Determine the winner
winner = get_winner(user_choice, computer_choice)
# Update points
if winner == "user":
st.session_state.user_points += 1
result = "You win this round! ๐ŸŽ‰"
elif winner == "computer":
st.session_state.computer_points += 1
result = "Computer wins this round! ๐Ÿค–"
else:
result = "It's a tie! ๐Ÿ˜"
# Update rounds played
st.session_state.rounds_played += 1
# Display results
st.write(f"**Your choice:** {user_choice}")
st.write(f"**Computer's choice:** {computer_choice}")
st.write(f"**Result:** {result}")
# Display points
st.write(f"**Your Points:** {st.session_state.user_points}")
st.write(f"**Computer's Points:** {st.session_state.computer_points}")
st.write(f"**Rounds Played:** {st.session_state.rounds_played}")
# Reset button
if st.button("Reset Game"):
st.session_state.user_points = 0
st.session_state.computer_points = 0
st.session_state.rounds_played = 0
st.write("Game reset! ๐ŸŽฎ Start playing again!")