foodle3.0 / app.py
dindizz's picture
Update app.py
691474b verified
import gradio as gr
import random
# List of 5-letter food dishes, including Indian dishes
FOOD_DISHES = [
"pizza", "ramen", "tacos", "pasta", "sushi",
"momos", "bread", "donut", "honey", "noods",
"salad", "guava", "curry", "pecan", "jelly",
"apple", "melon", "toast", "olive", "wraps",
# Indian dishes
"bhaji", "pulao", "vadas", "rasam", "sabzi",
"lassi", "chaat", "kulfi", "rotis", "biryani"
]
# Game state variables
answer = ""
attempts_left = 6
guesses = []
game_over = False
def initialize_game():
"""Resets the game to its initial state."""
global answer, attempts_left, guesses, game_over
answer = random.choice(FOOD_DISHES).lower()
attempts_left = 6
guesses = []
game_over = False
print(f"Answer set to: {answer}") # Debug print for answer confirmation
return generate_grid_html(), "New game started! Try to guess the dish.", ""
def generate_grid_html():
"""Generates the HTML for the Wordle-style grid."""
grid_html = '<div class="grid">'
for row in range(6):
for col in range(5):
if row < len(guesses):
letter, color = guesses[row][col]
grid_html += f'<div class="cell" style="background-color: {color};">{letter}</div>'
else:
grid_html += '<div class="cell"></div>'
grid_html += "</div>"
return grid_html
def foodle_guess(guess):
global attempts_left, guesses, game_over
if game_over:
return generate_grid_html(), "Game is over! Please restart to play again.", ""
guess = guess.strip().lower()
if len(guess) != 5:
return generate_grid_html(), "Your guess must be exactly 5 letters long!", ""
if len(answer) != 5:
return generate_grid_html(), "Error: The answer is not set correctly. Please restart the game.", ""
feedback = []
answer_used = [False] * 5
for i in range(5):
if guess[i] == answer[i]:
feedback.append((guess[i].upper(), "green"))
answer_used[i] = True
else:
feedback.append((guess[i].upper(), ""))
for i in range(5):
if feedback[i][1] == "":
for j in range(5):
if guess[i] == answer[j] and not answer_used[j]:
feedback[i] = (guess[i].upper(), "yellow")
answer_used[j] = True
break
guesses.append(feedback)
attempts_left -= 1
if guess == answer:
game_over = True
return generate_grid_html(), f"πŸŽ‰ Correct! The dish is '{answer.upper()}'!", ""
if attempts_left == 0:
game_over = True
return generate_grid_html(), f"Game Over! The dish was '{answer.upper()}'.", ""
return generate_grid_html(), f"Attempts left: {attempts_left}", ""
def reveal_answer():
global game_over
game_over = True
return generate_grid_html(), f"The correct answer was '{answer.upper()}'. Game over!", ""
def get_hint():
"""Provide a hint by revealing the first letter of the answer."""
if not game_over:
return f"Hint: The first letter is '{answer[0].upper()}'."
return "Game is over! No hints available."
# Gradio Interface Setup
with gr.Blocks(css="""
.grid {
display: grid;
grid-template-columns: repeat(5, 60px);
grid-gap: 10px;
justify-content: center;
margin-top: 20px;
}
.cell {
width: 60px;
height: 60px;
border: 2px solid white;
color: white;
text-align: center;
line-height: 60px;
font-size: 2rem;
text-transform: uppercase;
}
""") as interface:
gr.Markdown("# 🍲 **Foodle: Guess the Food Dish!**")
gr.Markdown("#### Test your culinary knowledge and guess the 5-letter dish!")
grid_display = gr.HTML(generate_grid_html())
guess_input = gr.Textbox(label="Enter your guess", placeholder="Type your 5-letter guess")
submit_button = gr.Button("Submit")
hint_button = gr.Button("Get Hint")
reveal_button = gr.Button("Reveal Answer")
restart_button = gr.Button("Restart Game")
status = gr.Textbox(label="Game Status", interactive=False)
submit_button.click(fn=foodle_guess, inputs=[guess_input], outputs=[grid_display, status, guess_input])
hint_button.click(fn=get_hint, inputs=[], outputs=[status])
reveal_button.click(fn=reveal_answer, inputs=[], outputs=[grid_display, status])
restart_button.click(fn=initialize_game, inputs=[], outputs=[grid_display, status, guess_input])
with gr.Row():
gr.Markdown(
"""
<hr>
<p>Built by <a href="https://www.linkedin.com/in/venkataraghavansrinivasan/"
target="_blank" style="color: #4caf50; text-decoration: none;">
S. Venkataraghavan</a></p>
"""
)
# Launch the Interface
interface.launch()