Spaces:
Running
Running
import gradio as gr | |
import random | |
import time | |
choices = ["Rock", "Paper", "Scissors"] | |
emoji_map = {"Rock": "โ", "Paper": "โ", "Scissors": "โ๏ธ"} | |
def play_rps_best_of_5(user_choice, score): | |
countdown = "Rock... โ\n" | |
time.sleep(0.5) | |
countdown += "Paper... โ\n" | |
time.sleep(0.5) | |
countdown += "Scissors... โ๏ธ\n" | |
time.sleep(0.5) | |
countdown += "Shoot!\n\n" | |
bot_choice = random.choice(choices) | |
if user_choice == bot_choice: | |
score["draws"] += 1 | |
result = "It's a draw!" | |
elif ( | |
(user_choice == "Rock" and bot_choice == "Scissors") or | |
(user_choice == "Paper" and bot_choice == "Rock") or | |
(user_choice == "Scissors" and bot_choice == "Paper") | |
): | |
score["wins"] += 1 | |
result = "You win this round! ๐" | |
else: | |
score["losses"] += 1 | |
result = "You lose this round ๐ข" | |
status = f"Score: ๐ข {score['wins']} - ๐ด {score['losses']} - โช {score['draws']}\n" | |
# Check for Best of 5 winner | |
if score["wins"] == 3: | |
final_result = "\n๐ You won the game! Click 'Reset' to play again." | |
elif score["losses"] == 3: | |
final_result = "\n๐ You lost the game. Click 'Reset' to try again." | |
else: | |
final_result = "\nKeep playing..." | |
full_output = countdown + \ | |
f"You chose: {user_choice} {emoji_map[user_choice]}\n" \ | |
f"Bot chose: {bot_choice} {emoji_map[bot_choice]}\n\n" + \ | |
result + "\n\n" + status + final_result | |
return full_output, score | |
def reset_game(): | |
return "Game reset! Start playing again.", {"wins": 0, "losses": 0, "draws": 0} | |
with gr.Blocks() as demo: | |
gr.Markdown("## ๐ฎ Rock-Paper-Scissors (Best of 5 + Animation)\nCan you beat the bot 3 times before it beats you?") | |
score_state = gr.State(value={"wins": 0, "losses": 0, "draws": 0}) | |
with gr.Row(): | |
user_input = gr.Radio(choices, label="Your Move") | |
output_text = gr.Textbox(label="Game Log", lines=10) | |
with gr.Row(): | |
play_btn = gr.Button("Play") | |
reset_btn = gr.Button("Reset") | |
play_btn.click(play_rps_best_of_5, inputs=[user_input, score_state], outputs=[output_text, score_state]) | |
reset_btn.click(fn=reset_game, inputs=[], outputs=[output_text, score_state]) | |
demo.launch() | |