Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import random
|
3 |
+
|
4 |
+
# Game logic for Rock-Paper-Scissors
|
5 |
+
def determine_winner(user_choice, computer_choice):
|
6 |
+
# The logic to determine the winner
|
7 |
+
if user_choice == computer_choice:
|
8 |
+
return "It's a tie!", 0
|
9 |
+
elif (user_choice == "Rock" and computer_choice == "Scissors") or \
|
10 |
+
(user_choice == "Scissors" and computer_choice == "Paper") or \
|
11 |
+
(user_choice == "Paper" and computer_choice == "Rock"):
|
12 |
+
return "You Win!", 1
|
13 |
+
else:
|
14 |
+
return "Computer Wins!", -1
|
15 |
+
|
16 |
+
# Initialize or reset the game
|
17 |
+
def reset_game():
|
18 |
+
return 0, 0 # user_points, computer_points
|
19 |
+
|
20 |
+
# Set up Streamlit page config
|
21 |
+
st.set_page_config(page_title="Rock Paper Scissors", page_icon="✊✋✌️", layout="centered")
|
22 |
+
|
23 |
+
# Game title and description
|
24 |
+
st.title("Rock-Paper-Scissors Game")
|
25 |
+
st.markdown("### Play Rock-Paper-Scissors against the computer!")
|
26 |
+
st.markdown("""
|
27 |
+
- **Rock** beats **Scissors**.
|
28 |
+
- **Scissors** beats **Paper**.
|
29 |
+
- **Paper** beats **Rock**.
|
30 |
+
""")
|
31 |
+
|
32 |
+
# Sidebar - Reset Game
|
33 |
+
if st.sidebar.button("Reset Game"):
|
34 |
+
user_points, computer_points = reset_game()
|
35 |
+
|
36 |
+
# Sidebar - Current Score
|
37 |
+
st.sidebar.markdown("### Current Score")
|
38 |
+
user_points = st.sidebar.number_input("Your Points", min_value=0, step=1, value=user_points, key="user_points", disabled=True)
|
39 |
+
computer_points = st.sidebar.number_input("Computer's Points", min_value=0, step=1, value=computer_points, key="computer_points", disabled=True)
|
40 |
+
|
41 |
+
# User's choice (button selection)
|
42 |
+
col1, col2, col3 = st.columns(3)
|
43 |
+
|
44 |
+
with col1:
|
45 |
+
if st.button("Rock"):
|
46 |
+
user_choice = "Rock"
|
47 |
+
with col2:
|
48 |
+
if st.button("Paper"):
|
49 |
+
user_choice = "Paper"
|
50 |
+
with col3:
|
51 |
+
if st.button("Scissors"):
|
52 |
+
user_choice = "Scissors"
|
53 |
+
|
54 |
+
# If a user choice is made
|
55 |
+
if 'user_choice' in locals():
|
56 |
+
# Computer's random choice
|
57 |
+
computer_choice = random.choice(["Rock", "Paper", "Scissors"])
|
58 |
+
|
59 |
+
# Show user's and computer's choice
|
60 |
+
st.write(f"You chose: {user_choice}")
|
61 |
+
st.write(f"Computer chose: {computer_choice}")
|
62 |
+
|
63 |
+
# Determine the winner
|
64 |
+
result, points_change = determine_winner(user_choice, computer_choice)
|
65 |
+
|
66 |
+
# Update points
|
67 |
+
if points_change == 1:
|
68 |
+
user_points += 1
|
69 |
+
elif points_change == -1:
|
70 |
+
computer_points += 1
|
71 |
+
|
72 |
+
# Show result
|
73 |
+
st.write(f"Result: {result}")
|
74 |
+
|
75 |
+
# Show current score
|
76 |
+
st.sidebar.write(f"Your Points: {user_points} - Computer's Points: {computer_points}")
|
77 |
+
|
78 |
+
# Footer (optional)
|
79 |
+
st.markdown("""
|
80 |
+
---
|
81 |
+
<p style="font-size:14px; text-align:center;">Made with ❤️ using Streamlit</p>
|
82 |
+
""", unsafe_allow_html=True)
|