Spaces:
Sleeping
Sleeping
| 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!") |