Rock_Paper_Scissor-Game / src /streamlit_app.py
HMZaheer's picture
Update src/streamlit_app.py
ed0a87f verified
Raw
History Blame Contribute Delete
2.45 kB
import streamlit as st
import random
# -----------------------------
# Page Configuration
# -----------------------------
st.set_page_config(
page_title="Rock Paper Scissors Game",
page_icon="๐ŸŽฎ",
layout="centered"
)
# -----------------------------
# 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
if "draws" not in st.session_state:
st.session_state.draws = 0
# -----------------------------
# Title
# -----------------------------
st.title("๐ŸŽฎ Rock Paper Scissors Game")
st.write("Play against the computer!")
# -----------------------------
# User Choice
# -----------------------------
choices = ["Rock", "Paper", "Scissors"]
user_choice = st.selectbox(
"Choose your move:",
choices
)
# -----------------------------
# Play Button
# -----------------------------
if st.button("Play"):
computer_choice = random.choice(choices)
st.subheader("Results")
st.write(f"๐Ÿง‘ Your Choice: **{user_choice}**")
st.write(f"๐Ÿ’ป Computer Choice: **{computer_choice}**")
# -----------------------------
# Game Logic
# -----------------------------
if user_choice == computer_choice:
result = "It's a Draw!"
st.session_state.draws += 1
elif (
(user_choice == "Rock" and computer_choice == "Scissors")
or
(user_choice == "Paper" and computer_choice == "Rock")
or
(user_choice == "Scissors" and computer_choice == "Paper")
):
result = "๐ŸŽ‰ You Win!"
st.session_state.user_score += 1
else:
result = "๐Ÿ˜ข Computer Wins!"
st.session_state.computer_score += 1
st.success(result)
# -----------------------------
# Score Board
# -----------------------------
st.markdown("---")
st.subheader("๐Ÿ† Score Board")
col1, col2, col3 = st.columns(3)
with col1:
st.metric("๐Ÿง‘ You", st.session_state.user_score)
with col2:
st.metric("๐Ÿ’ป Computer", st.session_state.computer_score)
with col3:
st.metric("๐Ÿค Draws", st.session_state.draws)
# -----------------------------
# Reset Button
# -----------------------------
if st.button("Reset Scores"):
st.session_state.user_score = 0
st.session_state.computer_score = 0
st.session_state.draws = 0
st.success("Scores Reset Successfully!")