File size: 2,648 Bytes
313ea7e
0913af9
313ea7e
0913af9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
98
99
100
101
102
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()