Alexis Palmer commited on
Commit
bb475f1
1 Parent(s): 05b28c8

updated with hints

Browse files
Files changed (1) hide show
  1. app.py +25 -3
app.py CHANGED
@@ -40,7 +40,22 @@ def create_hangman_clue(word, guessed_letters):
40
  clue += '_ '
41
  return clue
42
 
43
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  def pick_new_word(lexicon):
45
  lexicon = list(lexicon)
46
 
@@ -53,11 +68,12 @@ def pick_new_word(lexicon):
53
 
54
  def hangman_game(current_state, guess):
55
  """Update the current state based on the guess."""
 
56
 
57
 
58
  if guess in current_state['guessed_letters'] or len(guess) > 1:
59
  # Illegal guess, do nothing
60
- return (current_state, 'Invalid guess')
61
 
62
  current_state['guessed_letters'].add(guess)
63
 
@@ -66,13 +82,15 @@ def hangman_game(current_state, guess):
66
  current_state['remaining_chances'] -= 1
67
 
68
  if current_state['remaining_chances'] == 0:
 
69
  # No more chances! New word
70
  current_state = pick_new_word(filtered_lexicon)
71
- return (current_state, 'You lose!')
72
  else:
73
  return (current_state, 'Wrong guess :(')
74
 
75
  else:
 
76
  # Right guess, check if there's any letters left
77
  for letter in current_state['word']:
78
  if letter not in current_state['guessed_letters']:
@@ -103,10 +121,14 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Mapudungun Hangman") as hangman:
103
 
104
  guess_textbox = gr.Textbox(label="Guess")
105
  guess_button = gr.Button(value="Submit")
 
106
 
107
  output_textbox = gr.Textbox(label="Result", interactive=False)
 
 
108
 
109
  guess_button.click(fn=hangman_game, inputs=[current_word, guess_textbox], outputs=[current_word, output_textbox])\
110
  .then(fn=state_changed, inputs=[current_word], outputs=[current_word_textbox, guessed_letters_textbox, remaining_chances_textbox])
 
111
 
112
  hangman.launch(share=True)
 
40
  clue += '_ '
41
  return clue
42
 
43
+ def free_hint(current_state):
44
+ """
45
+ Give user a free hint by filling in one randomly-selected letter.
46
+ """
47
+ word = current_state['word']
48
+ guessed_letters = current_state['guessed_letters']
49
+
50
+ hint = random.choice(word)
51
+ while hint in guessed_letters:
52
+ hint = random.choice(word)
53
+
54
+ guessed_letters.add(hint)
55
+ clue = create_hangman_clue(word, guessed_letters)
56
+ return clue
57
+
58
+
59
  def pick_new_word(lexicon):
60
  lexicon = list(lexicon)
61
 
 
68
 
69
  def hangman_game(current_state, guess):
70
  """Update the current state based on the guess."""
71
+ guess = guess.lower()
72
 
73
 
74
  if guess in current_state['guessed_letters'] or len(guess) > 1:
75
  # Illegal guess, do nothing
76
+ return (current_state, 'Already guessed!')
77
 
78
  current_state['guessed_letters'].add(guess)
79
 
 
82
  current_state['remaining_chances'] -= 1
83
 
84
  if current_state['remaining_chances'] == 0:
85
+ old_word = current_state['word']
86
  # No more chances! New word
87
  current_state = pick_new_word(filtered_lexicon)
88
+ return (current_state, 'Out of guesses! The word is: '+old_word)
89
  else:
90
  return (current_state, 'Wrong guess :(')
91
 
92
  else:
93
+
94
  # Right guess, check if there's any letters left
95
  for letter in current_state['word']:
96
  if letter not in current_state['guessed_letters']:
 
121
 
122
  guess_textbox = gr.Textbox(label="Guess")
123
  guess_button = gr.Button(value="Submit")
124
+ hint_button = gr.Button(value="Click here for a free hint")
125
 
126
  output_textbox = gr.Textbox(label="Result", interactive=False)
127
+ #hints_textbox = gr.Textbox(label="Would you like a hint?", interactive=False)
128
+
129
 
130
  guess_button.click(fn=hangman_game, inputs=[current_word, guess_textbox], outputs=[current_word, output_textbox])\
131
  .then(fn=state_changed, inputs=[current_word], outputs=[current_word_textbox, guessed_letters_textbox, remaining_chances_textbox])
132
+ hint_button.click(fn=free_hint, inputs=[current_word], outputs=[current_word_textbox])
133
 
134
  hangman.launch(share=True)