CowBull / app.py
JAYASWAROOP's picture
Update app.py
30b53de verified
raw
history blame
1.4 kB
import random as r
import gradio as gr
def generate_number():
while True:
num = [r.randint(1, 9)] + [r.randint(0, 9) for _ in range(3)]
if len(set(num)) == 4:
return num
def play_game(user_guess):
global secret_number
guessed_number = [int(i) for i in user_guess]
if len(set(user_guess)) != 4 or user_guess[0] == '0':
return "Invalid input. Please enter a 4-digit number with non-repeating digits and the first digit not being 0."
cow = sum(g == s for g, s in zip(guessed_number, secret_number))
bull = sum(g in secret_number and g != s for g, s in zip(guessed_number, secret_number))
if cow == 4:
return "Congratulations! You won the game."
else:
return f"Bulls: {bull}\nCows: {cow}\nEnter your next guess"
# Generate the secret number
secret_number = generate_number()
def game_response(user_input,identifier=None):
global secret_number
if secret_number is None:
secret_number = generate_number()
return "Welcome to Bulls and Cows game! Try to guess the secret 4-digit number."
response = play_game(user_input)
if "Congratulations!" in response:
secret_number = None # Reset secret number for the next game
return response
# Interface setup
demo = gr.ChatInterface(game_response, examples=["1234", "5678", "9012"])
if __name__ == "__main__":
demo.launch()