Spaces:
Build error
Build error
Download app.py from hareshchander/rockpaperscissors: direct link, hf CLI and curl.
- Browser
- Download file 2.8 kB
-
https://huggingface.co/spaces/hareshchander/rockpaperscissors/resolve/main/app.py
- Command line
-
hf download hf://spaces/hareshchander/rockpaperscissors/app.py
-
curl -L -o app.py https://huggingface.co/spaces/hareshchander/rockpaperscissors/resolve/main/app.py
2.8 kB
| import streamlit as st | |
| import random | |
| # --- Custom CSS for Colorful Background --- | |
| st.markdown( | |
| """ | |
| <style> | |
| body { | |
| background: linear-gradient(135deg, #FFDEE9 0%, #B5FFFC 100%); | |
| color: #000000; | |
| } | |
| .stApp { | |
| background: linear-gradient(135deg, #FFDEE9 0%, #B5FFFC 100%); | |
| } | |
| .stButton>button { | |
| background-color: #FF7F50; | |
| color: white; | |
| font-size: 16px; | |
| border-radius: 10px; | |
| } | |
| .stButton>button:hover { | |
| background-color: #FF4500; | |
| color: white; | |
| } | |
| </style> | |
| """, | |
| unsafe_allow_html=True | |
| ) | |
| # --- Title and Instructions --- | |
| st.title("๐ชจโ๏ธ๐ Rock-Paper-Scissors Game with Points") | |
| st.write("Choose Rock, Paper, or Scissors and play against the computer! Each win earns you a point.") | |
| # --- Options for the Game --- | |
| choices = ["๐ชจ Rock", "๐ Paper", "โ๏ธ Scissors"] | |
| # --- Initialize or Retrieve 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 | |
| # --- Display Current Scores --- | |
| st.markdown(f"### **Your Score:** ๐ฏ {st.session_state.user_score}") | |
| st.markdown(f"### **Computer Score:** ๐ค {st.session_state.computer_score}") | |
| # --- User's Choice --- | |
| user_choice = st.selectbox("Make your choice:", choices) | |
| # --- Button to Submit the Choice --- | |
| if st.button("๐ฎ Play"): | |
| # Extract the actual choice (remove the emoji) | |
| user_choice_text = user_choice.split()[1] | |
| # Computer's random choice | |
| computer_choice = random.choice(choices) | |
| computer_choice_text = computer_choice.split()[1] | |
| # Display both choices | |
| st.write(f"**You chose:** {user_choice}") | |
| st.write(f"**Computer chose:** {computer_choice}") | |
| # Determine the winner and update scores | |
| if user_choice_text == computer_choice_text: | |
| st.info("๐ค It's a tie!") | |
| elif ( | |
| (user_choice_text == "Rock" and computer_choice_text == "Scissors") or | |
| (user_choice_text == "Paper" and computer_choice_text == "Rock") or | |
| (user_choice_text == "Scissors" and computer_choice_text == "Paper") | |
| ): | |
| st.success("๐ You win!") | |
| st.session_state.user_score += 1 | |
| else: | |
| st.error("๐ข You lose!") | |
| st.session_state.computer_score += 1 | |
| # --- Reset Button to Restart the Game --- | |
| if st.button("๐ Reset Scores"): | |
| st.session_state.user_score = 0 | |
| st.session_state.computer_score = 0 | |
| st.success("โ Scores have been reset!") | |
| # --- Add Image for Fun --- | |
| st.image("https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Rock-paper-scissors.svg/1920px-Rock-paper-scissors.svg.png", caption="Rock-Paper-Scissors", width=400) | |