A valid word belongs to the allowed art-themed vocabulary. That is different from a word you can make with your tiles: a word may be valid but still require letters you do not have. The vocabulary contains alphabetic words, one per line, arranged from shortest to longest. section 19.0 section 19.1
To find which valid words you can play:
- Turn the newline-separated vocabulary string into a tuple of individual words. Each newline marks the end of one word. section 19.1 section 19.2
- Check each word against the tiles. Use each tile at most once; if a needed letter is missing, move to the next word. section 19.1 section 19.2
- Collect the words that pass in a separate tuple of found words. section 19.0 section 19.2
Valid does not always mean playable
With tiles = "hijklmnop", "ink" is playable: its i, n, and k are available. "hue" is in the art vocabulary but is not playable because the tiles lack u and e. Try checking "oil" the same way. section 19.0 section 19.1
Listing 19.2. Scrabble: Art Edition code to get all valid words
for char in words: #1
if char == "\n": #2
all_valid_words = all_valid_words + (words[start:end],) #3
start = end + 1 #4
end = end +1 #4
else:
end = end + 1 #5
See how a loop finds each newline, slices out the preceding word, and adds that word to the valid-words tuple. section 19.2
Listing 19.3. Game code to check whether valid words can be made with tiles
for word in all_valid_words: #1
tiles_left = tiles #2
for letter in word: #3
if letter not in tiles_left: #4
break #4
else:
index = tiles_left.find(letter) #5
tiles_left = tiles_left[:index]+tiles_left[index+1:] #6
if len(word) == len(tiles)-len(tiles_left): #7
found_words = found_words + (word,) #8
print(found_words)
Follow the separate tile check: it starts with a fresh copy of the tiles for each word and removes letters as they are used. section 19.2
