amberroohee's picture
Update app.py
b2ee00a verified
import streamlit as st
import random
st.set_page_config(page_title="Rock Paper Scissors", page_icon="✊")
# -----------------------
# Game Setup
# -----------------------
choices = ["Rock", "Paper", "Scissors"]
emojis = {"Rock": "✊", "Paper": "βœ‹", "Scissors": "✌️"}
# Initialize session state
if "player_score" not in st.session_state:
st.session_state.player_score = 0
st.session_state.computer_score = 0
st.session_state.history = []
# -----------------------
# Functions
# -----------------------
def get_winner(player, computer):
if player == computer:
return "Draw"
elif (
(player == "Rock" and computer == "Scissors") or
(player == "Paper" and computer == "Rock") or
(player == "Scissors" and computer == "Paper")
):
return "Player"
else:
return "Computer"
def reset_game():
st.session_state.player_score = 0
st.session_state.computer_score = 0
st.session_state.history = []
# -----------------------
# UI
# -----------------------
st.title("✊ Rock Paper Scissors Game")
st.write("Play against the computer!")
col1, col2, col3 = st.columns(3)
with col1:
if st.button("✊ Rock"):
player_choice = "Rock"
with col2:
if st.button("βœ‹ Paper"):
player_choice = "Paper"
with col3:
if st.button("✌️ Scissors"):
player_choice = "Scissors"
# -----------------------
# Game Logic
# -----------------------
if "player_choice" not in locals():
player_choice = None
if player_choice:
computer_choice = random.choice(choices)
winner = get_winner(player_choice, computer_choice)
if winner == "Player":
st.session_state.player_score += 1
elif winner == "Computer":
st.session_state.computer_score += 1
st.session_state.history.append(
f"You: {emojis[player_choice]} | Computer: {emojis[computer_choice]} β†’ {winner}"
)
st.subheader("Result")
st.write(f"You chose {emojis[player_choice]} **{player_choice}**")
st.write(f"Computer chose {emojis[computer_choice]} **{computer_choice}**")
st.success(f"Winner: {winner}")
# -----------------------
# Scoreboard
# -----------------------
st.divider()
st.subheader("Scoreboard")
st.write(f"Player: {st.session_state.player_score}")
st.write(f"Computer: {st.session_state.computer_score}")
# -----------------------
# History
# -----------------------
st.divider()
st.subheader("Game History")
for item in reversed(st.session_state.history[-10:]):
st.write(item)
# -----------------------
# Reset
# -----------------------
if st.button("Reset Game"):
reset_game()
st.rerun()