File size: 4,811 Bytes
bcbc290 691474b bcbc290 |
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
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()
|