Spaces:
Sleeping
Sleeping
File size: 3,435 Bytes
384fe7c 1219364 384fe7c 1219364 fc3af95 384fe7c 1219364 fc3af95 1219364 fc3af95 1219364 fc3af95 1219364 fc3af95 1219364 fc3af95 1219364 fc3af95 1219364 fc3af95 1219364 | 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 | import gradio as gr
import random
# Function to handle the guessing logic
def guess_number(guess, secret_number, attempts):
if secret_number is None:
# Initialize the secret number and reset attempts
secret_number = random.randint(1, 100)
attempts = 0
# Validate input
if not isinstance(guess, (int, float)) or not (1 <= guess <= 100):
result = "β οΈ Please enter an integer between 1 and 100."
return result, secret_number, attempts
attempts += 1 # Increment the number of attempts
if attempts > 8:
# Game over if the number of attempts exceeds 8
result = f"π₯ Game Over! You couldn't guess the number within 8 attempts. The number was {secret_number}. Starting a new game."
secret_number = None # Reset the game
attempts = 0
elif guess == secret_number:
result = f"π Congratulations! You guessed the number {secret_number} in {attempts} attempt(s). Starting a new game."
secret_number = None # Reset the game
attempts = 0
elif guess < secret_number:
result = f"π Too low! Attempt {attempts}/8."
else:
result = f"π Too high! Attempt {attempts}/8."
return result, secret_number, attempts
# Function to restart the game
def restart_game(secret_number, attempts):
secret_number = random.randint(1, 100)
attempts = 0
result = "π Game has been restarted! Try to guess the new number between 1 and 100."
return result, secret_number, attempts
# Function to reveal the answer
def reveal_answer(secret_number, attempts):
if secret_number is None:
# If no game is active, start a new one first
secret_number = random.randint(1, 100)
attempts = 0
result = f"π The secret number was {secret_number}. Starting a new game."
secret_number = None # Reset the game
attempts = 0
return result, secret_number, attempts
with gr.Blocks() as iface:
gr.Markdown("# π΅οΈββοΈ Number Guessing Game")
gr.Markdown("Try to guess the secret number between 1 and 100 in less than 8 attempts.")
guess_input = gr.Number(label="Enter your guess", precision=0, interactive=True)
# Main output display for results
output = gr.Textbox(label="Result", lines=2, interactive=False)
# Button row for Guess, Restart, and Reveal actions
with gr.Row():
submit_button = gr.Button("Guess")
restart_button = gr.Button("π Restart Game")
reveal_button = gr.Button("π Reveal Answer")
# Initialize state for secret number and attempts count
secret_number_state = gr.State()
attempts_state = gr.State()
# Define what happens when the Guess button is clicked
submit_button.click(
fn=guess_number,
inputs=[guess_input, secret_number_state, attempts_state],
outputs=[output, secret_number_state, attempts_state]
)
# Define what happens when the Restart button is clicked
restart_button.click(
fn=restart_game,
inputs=[secret_number_state, attempts_state],
outputs=[output, secret_number_state, attempts_state]
)
# Define what happens when the Reveal Answer button is clicked
reveal_button.click(
fn=reveal_answer,
inputs=[secret_number_state, attempts_state],
outputs=[output, secret_number_state, attempts_state]
)
iface.launch()
|