File size: 3,566 Bytes
b1d7433
 
67e5d1d
29c9f6c
67e5d1d
29c9f6c
67e5d1d
b1d7433
29c9f6c
 
b1d7433
 
7465ff6
b1d7433
 
 
 
 
 
 
 
08d4f36
b1d7433
 
08d4f36
b1d7433
 
 
c162cab
 
 
 
2adcb25
 
 
c162cab
 
 
b1d7433
 
 
 
e32478e
 
 
68ffc25
e32478e
68ffc25
e32478e
 
68ffc25
e32478e
 
68ffc25
e32478e
 
 
7465ff6
 
08d4f36
 
 
 
 
e32478e
 
 
 
 
 
 
29c9f6c
 
 
43bc504
4f7c546
29c9f6c
 
43bc504
 
29c9f6c
 
 
e32478e
 
7465ff6
e32478e
a7503d8
 
b1d7433
 
 
 
 
68ffc25
b1d7433
 
68ffc25
b1d7433
 
 
 
 
0256134
7465ff6
 
08d4f36
0256134
 
 
08d4f36
 
0256134
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
103
104
105
106
107
108
109
110
111
112
113
114
import streamlit as st
import random
import google.generativeai as genai
import os

GOOGLE_API_KEY = os.getenv("Gemini")
genai.configure(api_key=GOOGLE_API_KEY)

model = genai.GenerativeModel('gemini-pro')

# Define the moves
moves = ["Rock", "Paper", "Scissors"]


# Function to determine the winner
def determine_winner(player_move, computer_move):
    if player_move == computer_move:
        return "It's a tie!"
    elif (player_move == "Rock" and computer_move == "Scissors") or \
         (player_move == "Paper" and computer_move == "Rock") or \
         (player_move == "Scissors" and computer_move == "Paper"):
        st.session_state.playerWin+=1
        return "You win!"
    else:
        st.session_state.computerWin+=1
        return "You lose!"

# Streamlit app

st.markdown("""
    <style>
        button {
            height: 4rem;
            width: 4rem;
            font-size: 40px !important;
        }
    </style>
""", unsafe_allow_html=True)
st.title("Rock, Paper, Scissors")

st.write("Choose your move:")

# Create three horizontal buttons
col1, col2, col3 = st.columns(3)

moveEmojis = {"Rock": "✊", "Paper":"✋", "Scissors": "✌️"}
with col1:
    if st.button("Rock ✊"):
        player_move = "Rock"
with col2:
    if st.button("Paper ✋"):
        player_move = "Paper"
with col3:
    if st.button("Scissors✌️"):
        player_move = "Scissors"

# Ensure player_move is defined
if 'count' not in st.session_state:
    st.session_state.count = 0
if 'playerWin' not in st.session_state:
    st.session_state.playerWin = 0
if 'computerWin' not in st.session_state:
    st.session_state.computerWin = 0
       
if 'player_move' not in st.session_state:
    st.session_state.player_move = None

# Assign player move to session state if a move is made
if 'player_move' in locals():
    st.session_state.player_move = player_move

if "chat" not in st.session_state:
    st.session_state.chat = model.start_chat(history = [
        {
            "role": 'user',
            "parts": 'You are playing a random rock paper scissors game. For rock will say "Rock", for paper will say "Paper" and for scissors will say "Scissors".  Pick a random word out of three and reply in single word once I do my move.'
        },
        {
            "role": 'model',
            "parts": 'Alright, let us play a random Rock-Paper-Scissors game. Make your first move!'
        }
    ])
    
# If a move is selected, randomly choose a move for the computer and determine the result
if st.session_state.player_move:
    st.session_state.count = st.session_state.count+1
    player_move = st.session_state.player_move
    response = st.session_state.chat.send_message(player_move)
    computer_move = response.text
    
    # Display player and computer moves
    col1, col2 = st.columns(2)
    with col1:
        st.write("Your move:")
        st.subheader("{}: {}".format(player_move, moveEmojis[player_move]))
    with col2:
        st.write("Computer's move:")
        st.subheader("{}: {}".format(computer_move, moveEmojis[computer_move]))
    
    # Display result
    result = determine_winner(player_move, computer_move)
    st.write("Result:")
    st.subheader(result)

    if st.session_state.count == 3:
        st.session_state.count = 0
        if st.session_state.computerWin > st.session_state.playerWin:
            st.write("Best of Three result: Sorry You lose!")
        else:
            st.write("Best of Three result: Yayy you win !")
        st.session_state.computerWin = 0
        st.session_state.playerWin = 0