Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,8 +5,7 @@ import time
|
|
5 |
choices = ["Rock", "Paper", "Scissors"]
|
6 |
emoji_map = {"Rock": "โ", "Paper": "โ", "Scissors": "โ๏ธ"}
|
7 |
|
8 |
-
def
|
9 |
-
# Simulate countdown
|
10 |
countdown = "Rock... โ\n"
|
11 |
time.sleep(0.5)
|
12 |
countdown += "Paper... โ\n"
|
@@ -18,25 +17,52 @@ def play_rps_animated(user_choice):
|
|
18 |
bot_choice = random.choice(choices)
|
19 |
|
20 |
if user_choice == bot_choice:
|
|
|
21 |
result = "It's a draw!"
|
22 |
elif (
|
23 |
(user_choice == "Rock" and bot_choice == "Scissors") or
|
24 |
(user_choice == "Paper" and bot_choice == "Rock") or
|
25 |
(user_choice == "Scissors" and bot_choice == "Paper")
|
26 |
):
|
27 |
-
|
|
|
28 |
else:
|
29 |
-
|
|
|
30 |
|
31 |
-
|
32 |
-
f"Bot chose: {bot_choice} {emoji_map[bot_choice]}\n\n" + result
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
|
|
|
5 |
choices = ["Rock", "Paper", "Scissors"]
|
6 |
emoji_map = {"Rock": "โ", "Paper": "โ", "Scissors": "โ๏ธ"}
|
7 |
|
8 |
+
def play_rps_best_of_5(user_choice, score):
|
|
|
9 |
countdown = "Rock... โ\n"
|
10 |
time.sleep(0.5)
|
11 |
countdown += "Paper... โ\n"
|
|
|
17 |
bot_choice = random.choice(choices)
|
18 |
|
19 |
if user_choice == bot_choice:
|
20 |
+
score["draws"] += 1
|
21 |
result = "It's a draw!"
|
22 |
elif (
|
23 |
(user_choice == "Rock" and bot_choice == "Scissors") or
|
24 |
(user_choice == "Paper" and bot_choice == "Rock") or
|
25 |
(user_choice == "Scissors" and bot_choice == "Paper")
|
26 |
):
|
27 |
+
score["wins"] += 1
|
28 |
+
result = "You win this round! ๐"
|
29 |
else:
|
30 |
+
score["losses"] += 1
|
31 |
+
result = "You lose this round ๐ข"
|
32 |
|
33 |
+
status = f"Score: ๐ข {score['wins']} - ๐ด {score['losses']} - โช {score['draws']}\n"
|
|
|
34 |
|
35 |
+
# Check for Best of 5 winner
|
36 |
+
if score["wins"] == 3:
|
37 |
+
final_result = "\n๐ You won the game! Click 'Reset' to play again."
|
38 |
+
elif score["losses"] == 3:
|
39 |
+
final_result = "\n๐ You lost the game. Click 'Reset' to try again."
|
40 |
+
else:
|
41 |
+
final_result = "\nKeep playing..."
|
42 |
+
|
43 |
+
full_output = countdown + \
|
44 |
+
f"You chose: {user_choice} {emoji_map[user_choice]}\n" \
|
45 |
+
f"Bot chose: {bot_choice} {emoji_map[bot_choice]}\n\n" + \
|
46 |
+
result + "\n\n" + status + final_result
|
47 |
+
|
48 |
+
return full_output, score
|
49 |
+
|
50 |
+
def reset_game():
|
51 |
+
return "Game reset! Start playing again.", {"wins": 0, "losses": 0, "draws": 0}
|
52 |
+
|
53 |
+
with gr.Blocks() as demo:
|
54 |
+
gr.Markdown("## ๐ฎ Rock-Paper-Scissors (Best of 5 + Animation)\nCan you beat the bot 3 times before it beats you?")
|
55 |
+
|
56 |
+
score_state = gr.State(value={"wins": 0, "losses": 0, "draws": 0})
|
57 |
+
with gr.Row():
|
58 |
+
user_input = gr.Radio(choices, label="Your Move")
|
59 |
+
output_text = gr.Textbox(label="Game Log", lines=10)
|
60 |
+
|
61 |
+
with gr.Row():
|
62 |
+
play_btn = gr.Button("Play")
|
63 |
+
reset_btn = gr.Button("Reset")
|
64 |
+
|
65 |
+
play_btn.click(play_rps_best_of_5, inputs=[user_input, score_state], outputs=[output_text, score_state])
|
66 |
+
reset_btn.click(fn=reset_game, inputs=[], outputs=[output_text, score_state])
|
67 |
|
68 |
+
demo.launch()
|