dindizz commited on
Commit
57627fe
·
verified ·
1 Parent(s): 7c19a41

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -0
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import necessary libraries
2
+ import gradio as gr
3
+ import random
4
+
5
+ # List of 5-letter raga names for RagaDle
6
+ raga_names = [
7
+ "thodi", "durga", "atana", "manji", "behag", "yaman", "bhair", "begad"
8
+ ]
9
+
10
+ # Global state variables
11
+ answer = random.choice(raga_names).lower()
12
+ current_guess = ""
13
+ attempts = 0
14
+ grid_content = [[{"letter": "", "color": ""} for _ in range(5)] for _ in range(6)]
15
+
16
+ def generate_grid_html():
17
+ """Generate HTML to render the current state of the grid."""
18
+ html_content = ""
19
+ for row in grid_content:
20
+ row_html = "".join(
21
+ f"<div class='grid-cell {entry['color']}'>{entry['letter'].upper()}</div>"
22
+ for entry in row
23
+ )
24
+ html_content += f"<div class='grid-row'>{row_html}</div>"
25
+ return html_content
26
+
27
+ def submit_guess():
28
+ """Submit the guess, update the grid, and apply color coding."""
29
+ global current_guess, grid_content, attempts, answer
30
+
31
+ feedback = [{"letter": char, "color": "grey"} for char in current_guess]
32
+ answer_copy = list(answer) # Copy of answer to manage repeated letters correctly
33
+
34
+ # First pass: Check for exact matches (green)
35
+ for i, char in enumerate(current_guess):
36
+ if char == answer[i]:
37
+ feedback[i]["color"] = "green"
38
+ answer_copy[i] = None
39
+
40
+ # Second pass: Check for misplaced matches (yellow)
41
+ for i, char in enumerate(current_guess):
42
+ if feedback[i]["color"] != "green" and char in answer_copy:
43
+ feedback[i]["color"] = "yellow"
44
+ answer_copy[answer_copy.index(char)] = None
45
+
46
+ # Update the grid with feedback
47
+ if attempts < 6:
48
+ grid_content[attempts] = feedback
49
+ attempts += 1
50
+
51
+ if current_guess == answer:
52
+ return generate_grid_html(), f"🎉 Correct! The raga was {answer.upper()}!"
53
+ elif attempts == 6:
54
+ return generate_grid_html(), f"😢 Game Over! The raga was {answer.upper()}."
55
+
56
+ current_guess = ""
57
+ return generate_grid_html(), "Enter your next guess."
58
+
59
+ def handle_key_press(key):
60
+ """Handle key presses from the virtual keyboard."""
61
+ global current_guess
62
+
63
+ if key == "Backspace":
64
+ current_guess = current_guess[:-1]
65
+ elif len(current_guess) < 5 and key.isalpha():
66
+ current_guess += key.lower()
67
+
68
+ if len(current_guess) == 5:
69
+ return submit_guess()
70
+
71
+ return generate_grid_html(), f"Current guess: {current_guess}"
72
+
73
+ def restart_game():
74
+ """Reset the game to the initial state."""
75
+ global answer, current_guess, attempts, grid_content
76
+ answer = random.choice(raga_names).lower()
77
+ current_guess = ""
78
+ attempts = 0
79
+ grid_content = [[{"letter": "", "color": ""} for _ in range(5)] for _ in range(6)]
80
+ return generate_grid_html(), "Game restarted. Enter a 5-letter raga name."
81
+
82
+ def reveal_answer():
83
+ """Reveal the answer to the user."""
84
+ return generate_grid_html(), f"The raga was: {answer.upper()}."
85
+
86
+ # Gradio interface with CSS for layout
87
+ with gr.Blocks(css="""
88
+ .grid-row { display: flex; justify-content: center; margin-bottom: 5px; }
89
+ .grid-cell { width: 50px; height: 50px; border: 1px solid #ccc; margin: 2px;
90
+ display: flex; align-items: center; justify-content: center;
91
+ font-size: 20px; font-weight: bold; text-transform: uppercase; }
92
+ .green { background-color: lightgreen; }
93
+ .yellow { background-color: gold; }
94
+ .grey { background-color: lightgrey; }
95
+ """) as demo:
96
+ gr.Markdown("# 🎶 RagaDle: Guess the Raga!")
97
+ gr.Markdown("Test your knowledge of Carnatic and Hindustani music by guessing the 5-letter raga!")
98
+ gr.Markdown("**Developed by [Venkat](https://www.linkedin.com/in/venkataraghavansrinivasan/)**")
99
+
100
+ grid_display = gr.HTML(generate_grid_html())
101
+ status_display = gr.Textbox("Enter your next guess.", interactive=False)
102
+
103
+ with gr.Row():
104
+ for key in "qwertyuiop":
105
+ gr.Button(key).click(lambda k=key: handle_key_press(k), outputs=[grid_display, status_display])
106
+
107
+ with gr.Row():
108
+ for key in "asdfghjkl":
109
+ gr.Button(key).click(lambda k=key: handle_key_press(k), outputs=[grid_display, status_display])
110
+
111
+ with gr.Row():
112
+ for key in "zxcvbnm":
113
+ gr.Button(key).click(lambda k=key: handle_key_press(k), outputs=[grid_display, status_display])
114
+
115
+ with gr.Row():
116
+ gr.Button("Backspace").click(lambda: handle_key_press("Backspace"), outputs=[grid_display, status_display])
117
+ gr.Button("Restart Game").click(restart_game, outputs=[grid_display, status_display])
118
+ gr.Button("Reveal Answer").click(reveal_answer, outputs=[grid_display, status_display])
119
+
120
+ # Launch the Gradio app
121
+ demo.launch()