Sanjayraju30 commited on
Commit
4cfc59d
ยท
verified ยท
1 Parent(s): be4c921

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -14
app.py CHANGED
@@ -5,8 +5,7 @@ import time
5
  choices = ["Rock", "Paper", "Scissors"]
6
  emoji_map = {"Rock": "โœŠ", "Paper": "โœ‹", "Scissors": "โœŒ๏ธ"}
7
 
8
- def play_rps_animated(user_choice):
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
- result = "You win! ๐ŸŽ‰"
 
28
  else:
29
- result = "You lose ๐Ÿ˜ข"
 
30
 
31
- return countdown + f"You chose: {user_choice} {emoji_map[user_choice]}\n" \
32
- f"Bot chose: {bot_choice} {emoji_map[bot_choice]}\n\n" + result
33
 
34
- iface = gr.Interface(
35
- fn=play_rps_animated,
36
- inputs=gr.Radio(choices, label="Choose your move"),
37
- outputs="text",
38
- title="๐ŸŽฎ Rock-Paper-Scissors with Animation",
39
- description="Play against a bot with animated countdown!"
40
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
- iface.launch()
 
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()