Yo les gars,

Chapitre 36 du bouquin. Le mec demande de créer un jeu. J'ai été trop gourmand je suis allé chercher un jeu de pendu dans un autre bouquin. Je galère à mort pour reverse engineer la mécanique du code. Est-ce que c'est vraiment tendu et personne ne pige ou c'est moi qui suis trop débutant encore ? Bien sûr je comprends tous les bouts de code, mais pour reconstituer la logique de l'ensemble je suis largué (surtout la fonction displayboard).
Bref est ce que c'est normal je pige pas encore, et je dois revoir à la baisse ou ça devrait être basique à ce stade ?
Edit : Oui le pendu est dessiné chelou quand je copie ici, pourtant avec mon idle python ça ressort net.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
# 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, 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.
def getRandomWord(wordList): 
    wordIndex = random.randint(0, len(wordList) - 1) # random.randint stores a random index in "wordIndex"
    return wordList[wordIndex] # "wordList" returns the random 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)]) # will be printed the state of the hangman that corresponds to the number of letters missed
    print()
 
    print('Missed letters:', end=' ')
    for letter in missedLetters:
        print(letter, end=' ')
    print()
 
    blanks = '_' * len(secretWord)
 
    for i in range(len(secretWord)): 
        if secretWord[i] in correctLetters:
            blanks = blanks[:i] + secretWord[i] + blanks[i+1:]
 
    for letter in blanks: 
        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 isn’t in "alreadyGuessed".
def getGuess(alreadyGuessed):
    while True:
        print('Guess a letter.')
        guess = input()
        guess = guess.lower()
        if len(guess) != 1:
            print('Please enter a single letter.')
        elif guess in alreadyGuessed:
            print('You have already guessed that letter. Choose again.')
        elif guess not in 'abcdefghijklmnopqrstuvwxyz':
            print('Please enter a LETTER.')
        else:
            return 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')
 
 
print('H A N G M A N')
missedLetters = ''
correctLetters = ''
secretWord = getRandomWord(words)
gameIsDone = False
 
 
while True:
    displayBoard(missedLetters, correctLetters, secretWord)
 
    guess = getGuess(missedLetters + correctLetters)
 
    if guess in secretWord:
        correctLetters = correctLetters + guess
 
        foundAllLetters = True
        for i in range(len(secretWord)):
            if secretWord[i] not in correctLetters:
                foundAllLetters = False
                break
        if foundAllLetters:
            print('Yes! The secret word is "' + secretWord + '"! You have won!')
            gameIsDone = True
    else:
        missedLetters = missedLetters + guess
 
    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
 
    if gameIsDone:
        if playAgain():
            missedLetters = ''
            correctLetters = ''
            gameIsDone = False
            secretWord = getRandomWord(words)
        else:
            break