dindizz commited on
Commit
01a5831
·
verified ·
1 Parent(s): 7a408f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -28
app.py CHANGED
@@ -26,13 +26,17 @@ def generate_grid_html():
26
  html_content += f"<div class='grid-row'>{row_html}</div>"
27
  return html_content
28
 
29
- def submit_guess():
 
 
 
 
30
  """Submit the guess, update the grid, and apply color coding."""
31
  global current_guess, grid_content, attempts, answer
32
 
33
- guess = "".join(current_guess).lower()
34
  if len(guess) < 5:
35
- return generate_grid_html(), "Please enter a 5-letter word."
36
 
37
  feedback = [{"letter": char, "color": "grey"} for char in guess]
38
  answer_copy = list(answer)
@@ -58,31 +62,29 @@ def submit_guess():
58
  elif attempts == 6:
59
  return generate_grid_html(), f"😢 Game Over! The word was {answer.upper()}."
60
 
61
- current_guess[:] = [""] * 5 # Reset the current guess
62
  return generate_grid_html(), "Enter your next guess."
63
 
64
- def handle_key_press(key):
65
  """Handle key presses from the virtual keyboard or text input."""
66
  global current_guess
67
 
68
  if key == "Backspace":
69
  for i in reversed(range(5)):
70
- if current_guess[i] != "":
71
- current_guess[i] = ""
72
  break
73
- elif len("".join(current_guess).strip()) < 5 and key.isalpha():
74
  for i in range(5):
75
- if current_guess[i] == "":
76
- current_guess[i] = key.lower()
77
  break
78
 
79
- return generate_grid_html(), f"Current guess: {''.join(current_guess).upper()}"
80
 
81
  def restart_game():
82
  """Reset the game to the initial state."""
83
- global answer, current_guess, attempts, grid_content
84
  answer = random.choice(food_words).lower()
85
- current_guess[:] = [""] * 5
86
  attempts = 0
87
  grid_content = [[{"letter": "", "color": ""} for _ in range(5)] for _ in range(6)]
88
  return generate_grid_html(), "Game restarted. Enter a 5-letter word."
@@ -108,34 +110,32 @@ with gr.Blocks(css="""
108
  grid_display = gr.HTML(generate_grid_html())
109
  status_display = gr.Textbox("Enter your next guess.", interactive=False)
110
 
111
- with gr.Row():
112
- letter_inputs = [gr.Textbox(label="", max_lines=1, interactive=True) for _ in range(5)]
113
-
114
- def get_current_guess():
115
- return "".join(box.value or "" for box in letter_inputs).lower()
116
-
117
- def on_submit():
118
- if len(get_current_guess()) == 5:
119
- return submit_guess()
120
- return generate_grid_html(), "Please enter a complete 5-letter word."
121
 
122
  submit_button = gr.Button("Submit")
123
- submit_button.click(on_submit, outputs=[grid_display, status_display])
 
124
 
 
125
  with gr.Row(variant="compact"):
126
  for key in "qwertyuiop":
127
- gr.Button(key).click(lambda k=key: handle_key_press(k), outputs=[grid_display, status_display])
 
128
 
129
  with gr.Row(variant="compact"):
130
  for key in "asdfghjkl":
131
- gr.Button(key).click(lambda k=key: handle_key_press(k), outputs=[grid_display, status_display])
 
132
 
133
  with gr.Row(variant="compact"):
134
  for key in "zxcvbnm":
135
- gr.Button(key).click(lambda k=key: handle_key_press(k), outputs=[grid_display, status_display])
 
136
 
137
  with gr.Row():
138
- gr.Button("Backspace").click(lambda: handle_key_press("Backspace"), outputs=[grid_display, status_display])
 
139
  gr.Button("Restart Game").click(restart_game, outputs=[grid_display, status_display])
140
  gr.Button("Reveal Answer").click(reveal_answer, outputs=[grid_display, status_display])
141
 
 
26
  html_content += f"<div class='grid-row'>{row_html}</div>"
27
  return html_content
28
 
29
+ def get_current_guess(letter_inputs):
30
+ """Retrieve the current guess from the input boxes."""
31
+ return "".join([box.strip() for box in letter_inputs]).lower()
32
+
33
+ def submit_guess(letter_inputs):
34
  """Submit the guess, update the grid, and apply color coding."""
35
  global current_guess, grid_content, attempts, answer
36
 
37
+ guess = get_current_guess(letter_inputs)
38
  if len(guess) < 5:
39
+ return generate_grid_html(), "Please enter a complete 5-letter word."
40
 
41
  feedback = [{"letter": char, "color": "grey"} for char in guess]
42
  answer_copy = list(answer)
 
62
  elif attempts == 6:
63
  return generate_grid_html(), f"😢 Game Over! The word was {answer.upper()}."
64
 
 
65
  return generate_grid_html(), "Enter your next guess."
66
 
67
+ def handle_key_press(key, letter_inputs):
68
  """Handle key presses from the virtual keyboard or text input."""
69
  global current_guess
70
 
71
  if key == "Backspace":
72
  for i in reversed(range(5)):
73
+ if letter_inputs[i] != "":
74
+ letter_inputs[i] = ""
75
  break
76
+ elif len("".join(letter_inputs).strip()) < 5 and key.isalpha():
77
  for i in range(5):
78
+ if letter_inputs[i] == "":
79
+ letter_inputs[i] = key.lower()
80
  break
81
 
82
+ return generate_grid_html(), f"Current guess: {''.join(letter_inputs).upper()}"
83
 
84
  def restart_game():
85
  """Reset the game to the initial state."""
86
+ global answer, attempts, grid_content
87
  answer = random.choice(food_words).lower()
 
88
  attempts = 0
89
  grid_content = [[{"letter": "", "color": ""} for _ in range(5)] for _ in range(6)]
90
  return generate_grid_html(), "Game restarted. Enter a 5-letter word."
 
110
  grid_display = gr.HTML(generate_grid_html())
111
  status_display = gr.Textbox("Enter your next guess.", interactive=False)
112
 
113
+ # Input boxes for manual typing
114
+ letter_inputs = [gr.Textbox(label="", max_lines=1, interactive=True) for _ in range(5)]
 
 
 
 
 
 
 
 
115
 
116
  submit_button = gr.Button("Submit")
117
+ submit_button.click(lambda: submit_guess([inp.value for inp in letter_inputs]),
118
+ outputs=[grid_display, status_display])
119
 
120
+ # Virtual Keyboard
121
  with gr.Row(variant="compact"):
122
  for key in "qwertyuiop":
123
+ gr.Button(key).click(lambda k=key: handle_key_press(k, [inp.value for inp in letter_inputs]),
124
+ outputs=[grid_display, status_display])
125
 
126
  with gr.Row(variant="compact"):
127
  for key in "asdfghjkl":
128
+ gr.Button(key).click(lambda k=key: handle_key_press(k, [inp.value for inp in letter_inputs]),
129
+ outputs=[grid_display, status_display])
130
 
131
  with gr.Row(variant="compact"):
132
  for key in "zxcvbnm":
133
+ gr.Button(key).click(lambda k=key: handle_key_press(k, [inp.value for inp in letter_inputs]),
134
+ outputs=[grid_display, status_display])
135
 
136
  with gr.Row():
137
+ gr.Button("Backspace").click(lambda: handle_key_press("Backspace", [inp.value for inp in letter_inputs]),
138
+ outputs=[grid_display, status_display])
139
  gr.Button("Restart Game").click(restart_game, outputs=[grid_display, status_display])
140
  gr.Button("Reveal Answer").click(reveal_answer, outputs=[grid_display, status_display])
141