Spaces:
Runtime error
Runtime error
File size: 3,112 Bytes
b1d7433 67e5d1d 29c9f6c 67e5d1d 29c9f6c 67e5d1d b1d7433 29c9f6c b1d7433 0256134 b1d7433 08d4f36 b1d7433 08d4f36 b1d7433 e32478e 08d4f36 e32478e 29c9f6c 43bc504 d1dd783 29c9f6c 43bc504 29c9f6c e32478e 0256134 e32478e a7503d8 b1d7433 0256134 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 |
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"]
count = 0
playerWin = 0
computerWin = 0
# 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.title("Rock, Paper, Scissors")
st.write("Choose your move:")
# Create three horizontal buttons
col1, col2, col3 = st.columns(3)
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 '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 scissor game. For rock will say "Rock", for paper will say "Paper" and for scissor will say "Scissor". 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:
count = 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(player_move)
with col2:
st.write("Computer's move:")
st.subheader(computer_move)
# Display result
result = determine_winner(player_move, computer_move)
st.write("Result:")
st.subheader(result)
if count == 3:
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
|