-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
97 lines (69 loc) · 2.7 KB
/
app.py
File metadata and controls
97 lines (69 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import streamlit as st
import random
st.set_page_config(page_title="Rock Paper Scissors", layout="centered")
# ---------------- GAME LOGIC ----------------
def decide_winner(player, computer):
if player == computer:
return "draw"
if (
(player == "Rock" and computer == "Scissors") or
(player == "Paper" and computer == "Rock") or
(player == "Scissors" and computer == "Paper")
):
return "player"
return "computer"
def reset_game():
st.session_state.player_score = 0
st.session_state.computer_score = 0
st.session_state.last_player_move = None
st.session_state.last_computer_move = None
st.session_state.result = None
# ---------------- SESSION STATE ----------------
if "player_score" not in st.session_state:
st.session_state.player_score = 0
if "computer_score" not in st.session_state:
st.session_state.computer_score = 0
if "last_player_move" not in st.session_state:
st.session_state.last_player_move = None
if "last_computer_move" not in st.session_state:
st.session_state.last_computer_move = None
if "result" not in st.session_state:
st.session_state.result = None
# ---------------- UI ----------------
st.title("✊✋✌️ Rock Paper Scissors")
# ---- Scoreboard ----
col1, col2 = st.columns(2)
with col1:
st.metric("You", st.session_state.player_score)
with col2:
st.metric("Computer", st.session_state.computer_score)
st.divider()
st.subheader("Make your move")
choices = ["Rock", "Paper", "Scissors"]
cols = st.columns(3)
for i, choice in enumerate(choices):
with cols[i]:
if st.button(choice):
computer_choice = random.choice(choices)
st.session_state.last_player_move = choice
st.session_state.last_computer_move = computer_choice
outcome = decide_winner(choice, computer_choice)
st.session_state.result = outcome
if outcome == "player":
st.session_state.player_score += 1
elif outcome == "computer":
st.session_state.computer_score += 1
# ---- Show Moves ----
if st.session_state.last_player_move:
st.subheader("Last Round")
st.write(f"**You played:** {st.session_state.last_player_move}")
st.write(f"**Computer played:** {st.session_state.last_computer_move}")
if st.session_state.result == "draw":
st.info("🤝 It's a Draw!")
elif st.session_state.result == "player":
st.success("🎉 You Win This Round!")
else:
st.error("💻 Computer Wins This Round!")
st.divider()
# ---- Restart ----
st.button("Restart Game", on_click=reset_game)