File size: 2,220 Bytes
b5721f5
 
 
 
 
 
 
 
 
 
9d1e930
b5721f5
 
 
 
 
9d1e930
b5721f5
9d1e930
b5721f5
 
 
 
 
9d1e930
 
 
 
 
 
b5721f5
 
 
 
 
9d1e930
 
 
 
 
 
 
 
 
 
b5721f5
 
 
 
 
9d1e930
 
 
 
 
 
 
 
 
 
b5721f5
 
9d1e930
b5721f5
 
 
 
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
import streamlit as st
import random

def get_computer_choice():
    """Randomly select rock, paper, or scissors for the computer."""
    return random.choice(["Rock", "Paper", "Scissors"])

def determine_winner(user_choice, computer_choice):
    """Determine the winner of the game."""
    if user_choice == computer_choice:
        return "tie"
    elif (
        (user_choice == "Rock" and computer_choice == "Scissors") or
        (user_choice == "Paper" and computer_choice == "Rock") or
        (user_choice == "Scissors" and computer_choice == "Paper")
    ):
        return "user"
    else:
        return "computer"

def main():
    st.set_page_config(page_title="Rock Paper Scissors", page_icon="๐Ÿ–Š", layout="centered")
    st.title("๐Ÿ† Rock Paper Scissors Game")

    # 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

    st.sidebar.header("Game Settings")
    user_choice = st.sidebar.radio("Choose your move:", ["Rock", "Paper", "Scissors"])

    if st.sidebar.button("Play!"):
        computer_choice = get_computer_choice()
        winner = determine_winner(user_choice, computer_choice)

        if winner == "user":
            st.session_state.user_score += 1
            result = "You win!"
        elif winner == "computer":
            st.session_state.computer_score += 1
            result = "Computer wins!"
        else:
            result = "It's a tie!"

        st.write("### Your Choice: ", user_choice)
        st.write("### Computer's Choice: ", computer_choice)
        st.write("## Result: ", result)

    # Display Scores
    st.write("---")
    col1, col2 = st.columns(2)
    with col1:
        st.subheader("Your Score")
        st.write(st.session_state.user_score)
    with col2:
        st.subheader("Computer's Score")
        st.write(st.session_state.computer_score)

    st.sidebar.markdown("---")
    st.sidebar.info(
        "This is a simple Rock Paper Scissors game with a points system. Make your choice, press Play, and see if you can beat the computer!"
    )

if __name__ == "__main__":
    main()