File size: 1,687 Bytes
dd541c3
 
 
 
 
61fa7e6
dd541c3
 
 
 
61fa7e6
 
 
 
 
 
 
 
dd541c3
 
 
 
 
 
 
 
 
 
 
61fa7e6
dd541c3
 
 
 
 
 
61fa7e6
dd541c3
 
61fa7e6
dd541c3
 
 
 
 
 
 
 
 
 
61fa7e6
 
 
 
 
 
 
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
import streamlit as st
import random

# Title and description
st.title("Rock, Paper, Scissors Game")
st.write("Play a simple game of Rock, Paper, Scissors against the computer! Track your score as you play.")

# Options
choices = ["Rock", "Paper", "Scissors"]

# Initialize session state for scores
if 'user_score' not in st.session_state:
    st.session_state.user_score = 0
if 'computer_score' not in st.session_state:
    st.session_state.computer_score = 0
if 'draws' not in st.session_state:
    st.session_state.draws = 0

# User's choice
user_choice = st.selectbox("Choose your option:", choices)

# Button to play
def play_game():
    # Computer's choice
    computer_choice = random.choice(choices)

    # Determine the winner
    if user_choice == computer_choice:
        result = "It's a draw!"
        st.session_state.draws += 1
    elif (
        (user_choice == "Rock" and computer_choice == "Scissors") or
        (user_choice == "Paper" and computer_choice == "Rock") or
        (user_choice == "Scissors" and computer_choice == "Paper")
    ):
        result = "You win!"
        st.session_state.user_score += 1
    else:
        result = "Computer wins!"
        st.session_state.computer_score += 1

    return computer_choice, result

if st.button("Play"):
    computer_choice, result = play_game()

    # Display results
    st.write(f"You chose: {user_choice}")
    st.write(f"Computer chose: {computer_choice}")
    st.success(result)

    # Display scores
    st.write("### Scores:")
    st.write(f"Your Score: {st.session_state.user_score}")
    st.write(f"Computer Score: {st.session_state.computer_score}")
    st.write(f"Draws: {st.session_state.draws}")