def get_next_letter(self, fragment): # always challenge if fragment is not a prefix if not lexicon.has_prefix(fragment): return '!' # choose a random next letter else: next_letter = random.choice(lexicon.valid_continuations(fragment)) # if that next letter completes a word, challenge instead # for now, GhostPlayer does not know about Ghost._MIN_WORD_LENGTH if lexicon.has_word(fragment + next_letter): return '!' # otherwise, just play that letter else: return next_letter
def get_word(self, fragment): # return any word with fragment as a prefix while not lexicon.has_word(fragment): next_letter = random.choice(lexicon.valid_continuations(fragment)) fragment += next_letter return fragment