Here is the code that we already have

hangman.py

So far, here are the functions that we have:

  • Load word file: loadWords()
  • Choose random word: chooseWord(wordlist)
  • Judge is the whold word is guessed : isWordGuessed(secretWord, lettersGuessed)
  • Get the guessed word like h_pp_ : getGuessedWord(secretWord, lettersGuessed)
  • Get available letters: getAvailableLetters(lettersGuessed)

Also, you may want to make sure you have the word list as before:

words.txt

Block Graph

graph

Sudo Code

Looking at the graph above, we could actually write some “sudo code” to show the logic:


    lettersGuessed = []
    mistakesMade = 0
    availableLetters = ""
    
    # Print : Welcome to the game, Hangman!

    # Print: I am thinking of a word that is X letters long (X is the lenth)


    # While loop : when we still have guesses, keep the loop

        # Print: ---------------------
        
        # Print: You have X guess left (X is the remaining gesses)

        # Print : Available letters : XXXXX (call lettersGuessed to get them)
        
        # Ask: Please guess a letter: (user input guess letter)

        # If: letter already guessed:

            # Print: ops! You've already guessed that letter:

        # Otherwise: (it's a new letter)

            # Get the previous guessed word (use getGuessedWord)

            # Change the letter to lower case string use string.lower()

            # Add the guessing letter to lettersGuessed list

            # Get the new guessed word (use getGuessedWord)

            # Compare the guessed word with previous guessed word (If):
            # If old_guessed == new_guessed: (Not a good guess)
            
                # Print : Oops! That letter is not in my word: XXX
                # XXX is the guessed word

                # Count one mistake

            # Else: (It's a good guess)

                # Print: Good Guess: XXX
                # XXX is the new guessed word
                

            # Check if the word is guessed (use isWordGuessed)

                # Print: Congratulations, you won!

                # Break out of the loop 
    

        # Here goes outside the loop

        # Print: Sorry, you ran out of guesses. The word was else.

        # Print: Here is the secret: XXX (the secret word)


Reference code so far

Here is the code of hangman!

hangman_finished.py

So far, you are able to create the above code to build a hangman game!

However, it has too many complicate words! Let’s give the players a hint, to make the game easier.

  • The hint is given everytime the the player guesses wrong

  • The hint is : “I will give you a hint, there is no: X”, where X is a hint letter that is:

    • not in the secret word
    • not yet guessed

It will look like this when running:

Loading word list from file...
   55909 words loaded.
Welcome to the game, Hangman!
I am thinking of a word that is 9 letters long.
-------------
You have 8 guesses left.
Available letters: abcdefghijklmnopqrstuvwxyz
Please guess a letter: e
Good guess: _______e_
-------------
You have 8 guesses left.
Available letters: abcdfghijklmnopqrstuvwxyz
Please guess a letter: i
Oops! That letter is not in my word: _______e_
I will give you a hint, there is no: j
-------------
You have 7 guesses left.
Available letters: abcdfghklmnopqrstuvwxyz
Please guess a letter: o
Oops! That letter is not in my word: _______e_
I will give you a hint, there is no: w
-------------
You have 6 guesses left.
Available letters: abcdfghklmnpqrstuvxyz
Please guess a letter: 

In order to do this, you might want to define a function:



def giveHint(secretWord,lettersGuessed):
    '''
    input1: secretWord(string), the secret word
    input2: lettersGuessed(list), what letters have been guessed so far
    return: string, comprised of letters that represents what letters have notyet been guessed.
    '''
    # FILL IN YOUR CODE HERE...
        

In the hangman function, you could call the above function to give the hint:

  • Print the hint letter
  • add this hint letter into lettersGuessed list, so that player should not guess it again.