Run a single game of Hacking.
()
| 22 | |
| 23 | |
| 24 | def main(): |
| 25 | """Run a single game of Hacking.""" |
| 26 | print('''Hacking Minigame, by Al Sweigart al@inventwithpython.com |
| 27 | Find the password in the computer's memory. You are given clues after |
| 28 | each guess. For example, if the secret password is MONITOR but the |
| 29 | player guessed CONTAIN, they are given the hint that 2 out of 7 letters |
| 30 | were correct, because both MONITOR and CONTAIN have the letter O and N |
| 31 | as their 2nd and 3rd letter. You get four guesses.\n''') |
| 32 | input('Press Enter to begin...') |
| 33 | |
| 34 | gameWords = getWords() |
| 35 | # The "computer memory" is just cosmetic, but it looks cool: |
| 36 | computerMemory = getComputerMemoryString(gameWords) |
| 37 | secretPassword = random.choice(gameWords) |
| 38 | |
| 39 | print(computerMemory) |
| 40 | # Start at 4 tries remaining, going down: |
| 41 | for triesRemaining in range(4, 0, -1): |
| 42 | playerMove = askForPlayerGuess(gameWords, triesRemaining) |
| 43 | if playerMove == secretPassword: |
| 44 | print('A C C E S S G R A N T E D') |
| 45 | return |
| 46 | else: |
| 47 | numMatches = numMatchingLetters(secretPassword, playerMove) |
| 48 | print('Access Denied ({}/7 correct)'.format(numMatches)) |
| 49 | print('Out of tries. Secret password was {}.'.format(secretPassword)) |
| 50 | |
| 51 | |
| 52 | def getWords(): |
no test coverage detected