Return a string representing the "computer memory".
(words)
| 115 | |
| 116 | |
| 117 | def getComputerMemoryString(words): |
| 118 | """Return a string representing the "computer memory".""" |
| 119 | |
| 120 | # Pick one line per word to contain a word. There are 16 lines, but |
| 121 | # they are split into two halves. |
| 122 | linesWithWords = random.sample(range(16 * 2), len(words)) |
| 123 | # The starting memory address (this is also cosmetic). |
| 124 | memoryAddress = 16 * random.randint(0, 4000) |
| 125 | |
| 126 | # Create the "computer memory" string. |
| 127 | computerMemory = [] # Will contain 16 strings, one for each line. |
| 128 | nextWord = 0 # The index in words of the word to put into a line. |
| 129 | for lineNum in range(16): # The "computer memory" has 16 lines. |
| 130 | # Create a half line of garbage characters: |
| 131 | leftHalf = '' |
| 132 | rightHalf = '' |
| 133 | for j in range(16): # Each half line has 16 characters. |
| 134 | leftHalf += random.choice(GARBAGE_CHARS) |
| 135 | rightHalf += random.choice(GARBAGE_CHARS) |
| 136 | |
| 137 | # Fill in the password from words: |
| 138 | if lineNum in linesWithWords: |
| 139 | # Find a random place in the half line to insert the word: |
| 140 | insertionIndex = random.randint(0, 9) |
| 141 | # Insert the word: |
| 142 | leftHalf = (leftHalf[:insertionIndex] + words[nextWord] |
| 143 | + leftHalf[insertionIndex + 7:]) |
| 144 | nextWord += 1 # Update the word to put in the half line. |
| 145 | if lineNum + 16 in linesWithWords: |
| 146 | # Find a random place in the half line to insert the word: |
| 147 | insertionIndex = random.randint(0, 9) |
| 148 | # Insert the word: |
| 149 | rightHalf = (rightHalf[:insertionIndex] + words[nextWord] |
| 150 | + rightHalf[insertionIndex + 7:]) |
| 151 | nextWord += 1 # Update the word to put in the half line. |
| 152 | |
| 153 | computerMemory.append('0x' + hex(memoryAddress)[2:].zfill(4) |
| 154 | + ' ' + leftHalf + ' ' |
| 155 | + '0x' + hex(memoryAddress + (16*16))[2:].zfill(4) |
| 156 | + ' ' + rightHalf) |
| 157 | |
| 158 | memoryAddress += 16 # Jump from, say, 0xe680 to 0xe690. |
| 159 | |
| 160 | # Each string in the computerMemory list is joined into one large |
| 161 | # string to return: |
| 162 | return '\n'.join(computerMemory) |
| 163 | |
| 164 | |
| 165 | def askForPlayerGuess(words, tries): |