Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import random | |
| def get_computer_choice(): | |
| """Randomly select rock, paper, or scissors for the computer.""" | |
| return random.choice(["Rock", "Paper", "Scissors"]) | |
| def determine_winner(user_choice, computer_choice): | |
| """Determine the winner of the game.""" | |
| 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" | |
| def main(): | |
| st.set_page_config(page_title="Rock Paper Scissors", page_icon="π", layout="centered") | |
| st.title("π Rock Paper Scissors Game") | |
| # Initialize 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 | |
| st.sidebar.header("Game Settings") | |
| user_choice = st.sidebar.radio("Choose your move:", ["Rock", "Paper", "Scissors"]) | |
| if st.sidebar.button("Play!"): | |
| computer_choice = get_computer_choice() | |
| winner = determine_winner(user_choice, computer_choice) | |
| if winner == "user": | |
| st.session_state.user_score += 1 | |
| result = "You win!" | |
| elif winner == "computer": | |
| st.session_state.computer_score += 1 | |
| result = "Computer wins!" | |
| else: | |
| result = "It's a tie!" | |
| st.write("### Your Choice: ", user_choice) | |
| st.write("### Computer's Choice: ", computer_choice) | |
| st.write("## Result: ", result) | |
| # Display Scores | |
| st.write("---") | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| st.subheader("Your Score") | |
| st.write(st.session_state.user_score) | |
| with col2: | |
| st.subheader("Computer's Score") | |
| st.write(st.session_state.computer_score) | |
| st.sidebar.markdown("---") | |
| st.sidebar.info( | |
| "This is a simple Rock Paper Scissors game with a points system. Make your choice, press Play, and see if you can beat the computer!" | |
| ) | |
| if __name__ == "__main__": | |
| main() | |