MCPcopy Create free account
hub / github.com/asweigart/PythonStdioGames / main

Function main

src/gamesbyexample/hacking.py:24–49  ·  view source on GitHub ↗

Run a single game of Hacking.

()

Source from the content-addressed store, hash-verified

22
23
24def main():
25 """Run a single game of Hacking."""
26 print('''Hacking Minigame, by Al Sweigart al@inventwithpython.com
27Find the password in the computer's memory. You are given clues after
28each guess. For example, if the secret password is MONITOR but the
29player guessed CONTAIN, they are given the hint that 2 out of 7 letters
30were correct, because both MONITOR and CONTAIN have the letter O and N
31as 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
52def getWords():

Callers 1

hacking.pyFile · 0.70

Calls 4

getWordsFunction · 0.85
getComputerMemoryStringFunction · 0.85
askForPlayerGuessFunction · 0.85
numMatchingLettersFunction · 0.85

Tested by

no test coverage detected