amberroohee commited on
Commit
b96c0dc
Β·
verified Β·
1 Parent(s): 9b9d24e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -0
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import random
3
+
4
+ st.set_page_config(page_title="Rock Paper Scissors", page_icon="✊")
5
+
6
+ # -----------------------
7
+ # Game Setup
8
+ # -----------------------
9
+ choices = ["Rock", "Paper", "Scissors"]
10
+ emojis = {"Rock": "✊", "Paper": "βœ‹", "Scissors": "✌️"}
11
+
12
+ # Initialize session state
13
+ if "player_score" not in st.session_state:
14
+ st.session_state.player_score = 0
15
+ st.session_state.computer_score = 0
16
+ st.session_state.history = []
17
+
18
+ # -----------------------
19
+ # Functions
20
+ # -----------------------
21
+ def get_winner(player, computer):
22
+ if player == computer:
23
+ return "Draw"
24
+ elif (
25
+ (player == "Rock" and computer == "Scissors") or
26
+ (player == "Paper" and computer == "Rock") or
27
+ (player == "Scissors" and computer == "Paper")
28
+ ):
29
+ return "Player"
30
+ else:
31
+ return "Computer"
32
+
33
+ def reset_game():
34
+ st.session_state.player_score = 0
35
+ st.session_state.computer_score = 0
36
+ st.session_state.history = []
37
+
38
+ # -----------------------
39
+ # UI
40
+ # -----------------------
41
+ st.title("✊ Rock Paper Scissors Game")
42
+ st.write("Play against the computer!")
43
+
44
+ col1, col2, col3 = st.columns(3)
45
+
46
+ with col1:
47
+ if st.button("✊ Rock"):
48
+ player_choice = "Rock"
49
+ with col2:
50
+ if st.button("βœ‹ Paper"):
51
+ player_choice = "Paper"
52
+ with col3:
53
+ if st.button("✌️ Scissors"):
54
+ player_choice = "Scissors"
55
+
56
+ # -----------------------
57
+ # Game Logic
58
+ # -----------------------
59
+ if "player_choice" not in locals():
60
+ player_choice = None
61
+
62
+ if player_choice:
63
+ computer_choice = random.choice(choices)
64
+ winner = get_winner(player_choice, computer_choice)
65
+
66
+ if winner == "Player":
67
+ st.session_state.player_score += 1
68
+ elif winner == "Computer":
69
+ st.session_state.computer_score += 1
70
+
71
+ st.session_state.history.append(
72
+ f"You: {emojis[player_choice]} | Computer: {emojis[computer_choice]} β†’ {winner}"
73
+ )
74
+
75
+ st.subheader("Result")
76
+ st.write(f"You chose {emojis[player_choice]} **{player_choice}**")
77
+ st.write(f"Computer chose {emojis[computer_choice]} **{computer_choice}**")
78
+ st.success(f"Winner: {winner}")
79
+
80
+ # -----------------------
81
+ # Scoreboard
82
+ # -----------------------
83
+ st.divider()
84
+ st.subheader("Scoreboard")
85
+ st.write(f"Player: {st.session_state.player_score}")
86
+ st.write(f"Computer: {st.session_state.computer_score}")
87
+
88
+ # -----------------------
89
+ # History
90
+ # -----------------------
91
+ st.divider()
92
+ st.subheader("Game History")
93
+ for item in reversed(st.session_state.history[-10:]):
94
+ st.write(item)
95
+
96
+ # -----------------------
97
+ # Reset
98
+ # -----------------------
99
+ if st.button("Reset Game"):
100
+ reset_game()
101
+ st.rerun()