Spaces:
Sleeping
Sleeping
File size: 2,326 Bytes
17534de a099b2f be4c921 a099b2f be4c921 4cfc59d be4c921 a099b2f 4cfc59d a099b2f 4cfc59d 6a59296 4cfc59d 17534de 4cfc59d 6a59296 4cfc59d 17534de 4cfc59d |
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 |
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()
|