1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
# This module distributes elements randomly.
import random
# This is a constant variable. This is why it is written in uppercase letters.
# This variable defines the different states of the hangman in a list.
# There are 7 states of hangman, which means the list goes from index 0 to index 6.
HANGMAN_PICS = ['''
+---+
|
|
|
===''', '''
+---+
0 |
|
|
===''', '''
+---+
0 |
| |
|
===''', '''
+---+
0 |
/| |
|
===''', '''
+---+
0 |
/|\ |
|
===''', '''
+---+
0 |
/|\ |
/ |
===''', '''
+---+
0 |
/|\ |
/ \ |
===''']
# This variable defines all the words of the game.
words = """ant baboon badger bat bear beaver camel cat clam cobra cougar
coyote crow deer dog donkey duck eagle ferret fox frog goat goose hawk
lion lizard llama mole monkey moose mouse mule newt otter owl panda
parrot pigeon python rabbit ram rat raven rhino salmon seal shark sheep
skunk sloth snake spider stork swan tiger toad trout turkey turtle
weasel whale wolf wombat zebra""".split()
# This function takes a list of strings passed to it and returns one string from it randomly.
def getRandomWord(wordList):
wordIndex = random.randint(0, len(wordList) - 1) # "random.randint" stores a number in "wordIndex". It returned an index between 0 and len - 1 (- 1 because "len" returns a number not an index).
return wordList[wordIndex] # returns the random word that corresponds to the list index.
# This function shows the current state of the board, including how much of the secret word the player has guessed so far and the wrong letters the player has guessed.
def displayBoard(missedLetters, correctLetters, secretWord):
print(HANGMAN_PICS[len(missedLetters)]) # When displayBoard is called , it starts by printing the current state of the hangman. The state is defined by "len(missedLetters)".
print()
print('Missed letters:', end=' ') # When displayBoard is called, it will print just that. Note that ' ' replaces the default newline.
for letter in missedLetters: # On the same line, the loop will print the missed letters.
print(letter, end=' ')
print()
blanks = '_' * len(secretWord) # When displayBoard is called the first time, it will print as many underscores(_) as there are letters in the secret word.
for i in range(len(secretWord)):
if secretWord[i] in correctLetters: # for each iteration, if it is true that the scanned letter from secretWord is in correctLetters, then...
blanks = blanks[:i] + secretWord[i] + blanks[i+1:] # then... in for 0, if secretWord[0] is in correctLetters replace blanks by blanks[:0] + secretWord[0] + blanks[0+1:], etc.
# blanks[:1] and blanks[i+1:] need to be reiterated, otherwise secretWord[i] will replace blanks too.
for letter in blanks: # It is been stored, it is time to print.
print(letter, end=' ')
print()
# This function takes a string of letters the player has already guessed and will keep asking the player for a letter that isnt in "alreadyGuessed".
def getGuess(alreadyGuessed):
while True: # With this while loop, the player is asked to guess a letter until a real letter is entered.
print('Guess a letter.')
guess = input()
guess = guess.lower() # "lower()" converts any uppercase letter into a lowercase letter.
if len(guess) != 1: # If the letter entered is not just one letter, python will print the next line.
print('Please enter a single letter.')
elif guess in alreadyGuessed: # If the letter entered is a letter already found, python will print the next line.
print('You have already guessed that letter. Choose again.')
elif guess not in 'abcdefghijklmnopqrstuvwxyz': # If the letter entered is not a real letter, python will print the next line.
print('Please enter a LETTER.')
else:
return guess # If the letter entered is none of these, python will store the letter in "guess".
# This function Asks if the player wants to play another round of Hangman.
def playAgain():
print('Do you want to play again? (yes or no)')
return input().lower().startswith('y') # This will convert any value entered into lowercase letters and then check if that value start with "y".
print('H A N G M A N')
missedLetters = '' # creates an empty variable
correctLetters = '' # creates an empty variable
secretWord = getRandomWord(words) # calls the function getRandomWord() which picked a random word and stored it in "secretWord".
# Remember "words" is the variable that contains all words.
gameIsDone = False
while True: # With this while loop, the player is showed the display board each time the game is reiterated.
displayBoard(missedLetters, correctLetters, secretWord) # The empty parameters from the function are now defined.
guess = getGuess(missedLetters + correctLetters) # calls the function getGuess() which filtered the guess letter and stored it in "guess".
# adds letters found in "correctLetters"
if guess in secretWord:
correctLetters = correctLetters + guess # adds the letter found to "correctLetters"
# ???
foundAllLetters = True
for i in range(len(secretWord)):
if secretWord[i] not in correctLetters:
foundAllLetters = False
break
# You found all the letters
if foundAllLetters:
print('Yes! The secret word is "' + secretWord + '"! You have won!')
gameIsDone = True # leads to "gameIsDone"
else:
missedLetters = missedLetters + guess # adds missed guesses to "missedLetters"
# You've run out of guesses
if len(missedLetters) == len(HANGMAN_PICS) - 1:
displayBoard(missedLetters, correctLetters, secretWord)
print('You have run out of guesses!\nAfter ' + str(len(missedLetters)) + ' missed guesses and ' + str(len(correctLetters)) + ' correct guesses, the word was ' + secretWord + '')
gameIsDone = True # leads to "gameIsDone"
if gameIsDone: # restarts the game
if playAgain(): # if the function playAgain() applies.
missedLetters = ''
correctLetters = ''
gameIsDone = False
secretWord = getRandomWord(words)
else:
break |
Partager