Alexis Palmer commited on
Commit
06dc31a
1 Parent(s): bb4edc6

first version of scramble

Browse files
Files changed (1) hide show
  1. app.py +23 -92
app.py CHANGED
@@ -32,109 +32,40 @@ for word in lexicon:
32
  print(len(filtered_lexicon))
33
 
34
 
35
- def create_hangman_clue(word, guessed_letters):
36
  """
37
- Given a word and a list of letters, create the correct clue.
38
-
39
- For instance, if the word is 'apple' and the guessed letters are 'a' and 'l', the clue should be 'a _ _ l _'
40
- """
41
- clue = ''
42
- for letter in word:
43
- if letter in guessed_letters:
44
- clue += letter + ' '
45
- else:
46
- clue += '_ '
47
- return clue
48
-
49
- def free_hint(current_state):
50
  """
51
- Give user a free hint by filling in one randomly-selected letter.
52
- """
53
- word = current_state['word']
54
- guessed_letters = current_state['guessed_letters']
55
-
56
- hint = random.choice(word)
57
- while hint in guessed_letters:
58
- hint = random.choice(word)
59
-
60
- guessed_letters.add(hint)
61
- clue = create_hangman_clue(word, guessed_letters)
62
- return clue
63
-
64
-
65
- def pick_new_word(lexicon):
66
- lexicon = list(lexicon)
67
-
68
- return {
69
- 'word': random.choice(lexicon),
70
- 'guessed_letters': set(),
71
- 'remaining_chances': 6
72
- }
73
-
74
-
75
- def hangman_game(current_state, guess):
76
- """Update the current state based on the guess."""
77
- guess = guess.lower()
78
-
79
-
80
- if guess in current_state['guessed_letters'] or len(guess) > 1:
81
- # Illegal guess, do nothing
82
- return (current_state, 'Already guessed!')
83
-
84
- current_state['guessed_letters'].add(guess)
85
-
86
- if guess not in current_state['word']:
87
- # Wrong guess
88
- current_state['remaining_chances'] -= 1
89
-
90
- if current_state['remaining_chances'] == 0:
91
- old_word = current_state['word']
92
- # No more chances! New word
93
- current_state = pick_new_word(filtered_lexicon)
94
- return (current_state, 'Out of guesses! The word is: '+old_word)
95
- else:
96
- return (current_state, 'Wrong guess :(')
97
-
98
  else:
99
-
100
- # Right guess, check if there's any letters left
101
- for letter in current_state['word']:
102
- if letter not in current_state['guessed_letters']:
103
- # Still letters remaining
104
- return (current_state, 'Correct guess!')
105
-
106
- # If we made it here, there's no letters left.
107
- current_state = pick_new_word(filtered_lexicon)
108
- return (current_state, 'You win!')
109
 
110
 
111
- def state_changed(current_state):
112
- clue = create_hangman_clue(current_state['word'], current_state['guessed_letters'])
113
- guessed_letters = current_state['guessed_letters']
114
- remaining_chances = current_state['remaining_chances']
115
- return (clue, guessed_letters, remaining_chances)
116
-
117
-
118
- with gr.Blocks(theme=gr.themes.Soft(), title="Mapudungun Hangman") as hangman:
119
- current_word = gr.State(pick_new_word(filtered_lexicon))
120
 
121
- gr.Markdown("# Mapudungun Hangman")
122
 
123
- with gr.Row():
124
- current_word_textbox = gr.Textbox(label="Clue", interactive=False, value=create_hangman_clue(current_word.value['word'], current_word.value['guessed_letters']))
125
- guessed_letters_textbox = gr.Textbox(label="Guessed letters", interactive=False)
126
- remaining_chances_textbox = gr.Textbox(label="Remaining chances", interactive=False, value=6)
127
 
128
  guess_textbox = gr.Textbox(label="Guess")
129
  guess_button = gr.Button(value="Submit")
130
- hint_button = gr.Button(value="Click here for a free hint")
 
131
 
132
  output_textbox = gr.Textbox(label="Result", interactive=False)
133
- #hints_textbox = gr.Textbox(label="Would you like a hint?", interactive=False)
134
-
135
 
136
- guess_button.click(fn=hangman_game, inputs=[current_word, guess_textbox], outputs=[current_word, output_textbox])\
137
- .then(fn=state_changed, inputs=[current_word], outputs=[current_word_textbox, guessed_letters_textbox, remaining_chances_textbox])
138
- hint_button.click(fn=free_hint, inputs=[current_word], outputs=[current_word_textbox])
139
 
140
- hangman.launch(share=True)
 
32
  print(len(filtered_lexicon))
33
 
34
 
35
+ def scrambler_game(current_word, guess: str):
36
  """
37
+ If `guess` is the correct word, return 'Correct' and pick a new word. Otherwise, return 'Incorrect'
38
+ Returns (correct_label, scrambled_word, current_word)
 
 
 
 
 
 
 
 
 
 
 
39
  """
40
+ if guess == current_word['original']:
41
+ current_word = random_scramble(filtered_lexicon)
42
+ return ('Correct', current_word['shuffled'], current_word)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  else:
44
+ return ('Incorrect', current_word['shuffled'], current_word)
 
 
 
 
 
 
 
 
 
45
 
46
 
47
+ def new_word():
48
+ current_word = random_scramble(filtered_lexicon)
49
+ return ('', current_word['shuffled'], current_word)
50
+
51
+
52
+ with gr.Blocks(theme=gr.themes.Soft(), title="Latin Unscramble") as unscramble:
53
+ # Start with some initial word
54
+ current_word = gr.State(random_scramble(filtered_lexicon))
 
55
 
56
+ gr.Markdown("# Latin Unscramble")
57
 
58
+ # Notice how we set the initial value based on the State
59
+ scrambled_textbox = gr.Textbox(label="Scrambled", interactive=False, value=current_word.value['shuffled'])
 
 
60
 
61
  guess_textbox = gr.Textbox(label="Guess")
62
  guess_button = gr.Button(value="Submit")
63
+
64
+ new_word_button = gr.Button(value="New Word")
65
 
66
  output_textbox = gr.Textbox(label="Result", interactive=False)
 
 
67
 
68
+ guess_button.click(fn=scrambler_game, inputs=[current_word, guess_textbox], outputs=[output_textbox, scrambled_textbox, current_word])
69
+ new_word_button.click(fn=new_word, inputs=[], outputs=[output_textbox, scrambled_textbox, current_word])
 
70
 
71
+ unscramble.launch(share=True)