Return a list of 12 words that could possibly be the password. The secret password will be the first word in the list. To make the game fair, we try to ensure that there are words with a range of matching numbers of letters as the secret word.
()
| 50 | |
| 51 | |
| 52 | def getWords(): |
| 53 | """Return a list of 12 words that could possibly be the password. |
| 54 | |
| 55 | The secret password will be the first word in the list. |
| 56 | To make the game fair, we try to ensure that there are words with |
| 57 | a range of matching numbers of letters as the secret word.""" |
| 58 | secretPassword = random.choice(WORDS) |
| 59 | words = [secretPassword] |
| 60 | |
| 61 | # Find two more words; these have zero matching letters. |
| 62 | # We use "< 3" because the secret password is already in words. |
| 63 | while len(words) < 3: |
| 64 | randomWord = getOneWordExcept(words) |
| 65 | if numMatchingLetters(secretPassword, randomWord) == 0: |
| 66 | words.append(randomWord) |
| 67 | |
| 68 | # Find two words that have 3 matching letters (but give up at 500 |
| 69 | # tries if not enough can be found). |
| 70 | for i in range(500): |
| 71 | if len(words) == 5: |
| 72 | break # Found 5 words, so break out of the loop. |
| 73 | |
| 74 | randomWord = getOneWordExcept(words) |
| 75 | if numMatchingLetters(secretPassword, randomWord) == 3: |
| 76 | words.append(randomWord) |
| 77 | |
| 78 | # Find at least seven words that have at least one matching letter |
| 79 | # (but give up at 500 tries if not enough can be found). |
| 80 | for i in range(500): |
| 81 | if len(words) == 12: |
| 82 | break # Found 7 or more words, so break out of the loop. |
| 83 | |
| 84 | randomWord = getOneWordExcept(words) |
| 85 | if numMatchingLetters(secretPassword, randomWord) != 0: |
| 86 | words.append(randomWord) |
| 87 | |
| 88 | # Add any random words needed to get 12 words total. |
| 89 | while len(words) < 12: |
| 90 | randomWord = getOneWordExcept(words) |
| 91 | words.append(randomWord) |
| 92 | |
| 93 | assert len(words) == 12 |
| 94 | return words |
| 95 | |
| 96 | |
| 97 | def getOneWordExcept(blocklist=None): |
no test coverage detected