$35
CS10A
“Text Twist”
Integrating lists, sets, strings, loops, file input and the random and itertools module.
Hints: list(str) explodes a string into characters, ‘’.join(list) combines letters into a word, random.choice
picks a random item from a list, random.shuffle permutes a list, text.split(‘’) splits a text into words.
You’ve seen this game or current crossword variants. You are given a set of 7 letters and have to use the
letters to find words between 3 and 7 letters long. If you get stuck, you can “twist” the letters into a new
order which might suggest hints.
First you read in a dictionary file as a big string and split it into words. You can store the dictionary in
a global variable called WORDS. You can make up your own dictionary file for testing, the official 3000
word dictionary will be supplied in Latte. You have to put the file in a known directory on your computer
which you read from using basic file commands (CH 6).
In your main function, you filter WORDS for 7 letter words then pick one using random.choice as
the clue. I stored the clue in exploded form as a list of letters. main calls getallwords(clue)
which calls getwords(clue,n) in a for loop with n ranging from 3 to 7. getwords uses
itertools.permutations(clue,n) to find all n-letter extracts from clue, which must be
joined together and tested for membership in WORDS. Be careful to remove duplicate words, and sort
each list of words alphabetically. For example, getallwords(‘wonder’) returns
['end','new','nod','nor','now','one','owe','own','red','row','down',
'word','owner','wonder'] where the 3-letter words are sorted as are the 4-letter words.
Now that you have the clue and the answers you can have main call twist(clue,answers)
which makes up the top-level while loop of the game. The user has to guess which 3-7 letter words can
be assembled from the letters in the clue. The loop also allows “q” to quit and “t” to “twist”
(random.shuffle) the clue.
TWIST uses a sentinel flag for quitting and a SET called guesses. It uses a subroutine
printboard(clue,guesses,answers) which shows the user the progress so far followed by
the clue, and asks for input of a word or command. If a word is entered, it is added to the set
guesses. Here is a printout of a game, where answers IN guesses are printed out and answers
NOT IN guesses are printed as blanks e.g “_ _ _ _” for a 4 letter word.
The while loop should end when all the words have been found, or if the user types “q”.
_ _ _
_ _ _
_ _ _
_ _ _
_ _ _
_ _ _ _
_ _ _ _
_ _ _ _ _ _ _
tsunais
Guess a word, or q for quit or t for twist: sun
_ _ _
_ _ _
_ _ _
_ _ _
sun
_ _ _ _
_ _ _ _
_ _ _ _ _ _ _
tsunais
Guess a word, or q for quit or t for twist: sin
_ _ _
_ _ _
sin
_ _ _
sun
_ _ _ _
_ _ _ _
_ _ _ _ _ _ _
tsunais
Guess a word, or q for quit or t for twist: h
_ _ _
nut
sin
_ _ _
sun
_ _ _ _
_ _ _ _
_ _ _ _ _ _ _
tsunais
Guess a word, or q for quit or t for twist: t
_ _ _
nut
sin
_ _ _
sun
_ _ _ _
_ _ _ _
_ _ _ _ _ _ _
tissuna
Guess a word, or q for quit or t for twist: q
Thanks for playing!