Spaces:
Runtime error
Runtime error
File size: 1,403 Bytes
e6f7cce ee481e3 e6f7cce 9036e85 e6f7cce 9036e85 e6f7cce 9036e85 e6f7cce f58dbe1 e6f7cce 9036e85 e6f7cce 9036e85 e6f7cce 30b53de 9036e85 ee481e3 9036e85 e6f7cce 9036e85 efecc26 ba80bce 9036e85 4d5276a |
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 |
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() |