Skip to content

Instantly share code, notes, and snippets.

@roman-on
Created May 17, 2020 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roman-on/34d053f1842f43763b7c4fd982c2234f to your computer and use it in GitHub Desktop.
Save roman-on/34d053f1842f43763b7c4fd982c2234f to your computer and use it in GitHub Desktop.
# Printing the welcome game screen
print ("HANGMAN_ASCII_ART", """
_ _
| | | |
| |__| | __ _ _ __ __ _ _ __ ___ __ _ _ __
| __ |/ _` | '_ \ / _` | '_ ` _ \ / _` | '_ \
| | | | (_| | | | | (_| | | | | | | (_| | | | |
|_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|
__/ |
|__ _/""")
# file_path_example = C:\Hangman\1 hangman.txt
# Path to the file
file_path = input("Please enter the file path:")
# To input a number to locate the hidden word in the file
index = int(input("Please enter a number to choose a hidden word:"))
# Printing the maximum tries in the game
print ("MAX_TRIES = 6")
# Printing lets start!
print ("Let’s start!")
# Printing the first example of hangman paint
print ("""
x-------x""")
def choose_word(file_path, index):
"""
The function choosing a word from the file
"""
element_splited = [] # New variable
with open (file_path, "r") as file_path: # Opens the file path and close automatically
for element in file_path: # Through the loop downloading the file content
element_splited += (element.split()) # Adding the file content to a new variable and converting to a list
b = index % len(element_splited) # If the input number is bigger than the number of words in the list it will start over
result = element_splited[b - 1] # Result as a tuple. A minus one that the list starts from 1
return result.lower() # Return the result
def show_hidden_word(secret_word, old_letters_guessed):
"""
The function is showing the player his progress in guessing the secret word
"""
result = "" # Making a new variable to contain the result of the guessed words and the underscores that will show which letters are still missing and wasn't guessed yet
for letter in secret_word: # Using "for" function to get the letters from the secret word
if letter in old_letters_guessed: # If the letter in the old_letters_guessed list
result += letter # If the letter was in the list it's adding this letter to the "result" string
result += " " # Making space between the letters and the underscores
else:
new = letter.replace(letter, "_ ") # If the letter wasn't guessed the empty space will be replaced by the underscore and one space
result += new # Adding the underscore to the result string
return result # Returning the final answer
def print_hangman(num_of_tries):
"""
The function prints one of the seven states of the dependent man after each wrong guess
"""
if num_of_tries == 0: # First guess try (this is the first try eventhough it's 0)
print (HANGMAN_PHOTOS ["picture 0"]) # Zero picture will appear from the dictionary
if num_of_tries == 1: # Second guess try
print (HANGMAN_PHOTOS ["picture 1"]) # First picture will appear from the dictionary
if num_of_tries == 2: # Third guess try
print (HANGMAN_PHOTOS ["picture 2"]) # Second picture will appear from the dictionary
if num_of_tries == 3: # Forth guess try
print (HANGMAN_PHOTOS ["picture 3"]) # Third picture will appear from the dictionary
if num_of_tries == 4: # Fifth guess try
print (HANGMAN_PHOTOS ["picture 4"]) # Forth picture will appear from the dictionary
if num_of_tries == 5: # Sixth guess try
print (HANGMAN_PHOTOS ["picture 5"]) # Fifth picture will appear from the dictionary
if num_of_tries == 6: # Seventh guess try
print (HANGMAN_PHOTOS ["picture 6"]) # Sixth picture will appear from the dictionary
HANGMAN_PHOTOS = {
"picture 0":
"x-------x",
"picture 1":"""
x-------x
|
|
|
|
|""",
"picture 2":"""
x-------x
| |
| 0
|
|
|""",
"picture 3":"""
x-------x
| |
| 0
| |
|
|""",
"picture 4":"""
x-------x
| |
| 0
| /|\\
|
|""",
"picture 5":"""
x-------x
| |
| 0
| /|\\
| /
|""",
"picture 6":"""
x-------x
| |
| 0
| /|\\
| / \\
|""",
}
def try_update_letter_guessed(letter_guessed, old_letters_guessed):
"""
The function:
A string called letter_guessed. The string represents the character received from the player.
A list called old_letters_guessed. The list contains the letters the player has guessed so far.
"""
secret_word = choose_word(file_path, index) # Secret word equal to function output: choose_word(file_path, index)
print (show_hidden_word(secret_word, old_letters_guessed)) # Printing the hidden word with underscore
i = 1
while i <= 6:
letter_guessed = (input("Guess a letter:")).lower() # Asking the player to input a letter for guessing the hidden word
if len(letter_guessed) == 1 and letter_guessed.isalpha() and letter_guessed.lower() not in old_letters_guessed and letter_guessed in secret_word:
# If the character is correct (ie one English letter) and has not guessed it before --
# -- it will add the letter_guessed character to the old_letters_guessed list
old_letters_guessed.append(letter_guessed)
# Return a true value indicating that the insert was successful.
print ("True")
print (show_hidden_word(secret_word, old_letters_guessed)) # Printing the progress of the hidden word
win_lose = check_win(secret_word, old_letters_guessed) # Win_lose variable equal to a function that checking if the player won the game or not
if win_lose == True: # If statement checking if the win_lose becomes True from the function
print ("WIN") # If the player guessed all the letters in the game so he wins and it will print "win"
break # After the player won the loop is break and the game is finished
continue # If the player guessing the right letters the game continues
elif len(letter_guessed) > 1 or not(letter_guessed.isalpha()) or letter_guessed.lower() in old_letters_guessed:
# If the character is incorrect (or not a single English letter) or is already in the guess list, --
# -- it will will print the character X (the uppercase X)
print("X")
if letter_guessed.lower() not in old_letters_guessed and not (letter_guessed in "!@#$%^""'=+-/*~&()<>|\[]?,.") and not (len(letter_guessed) > 1):
# It will add the letter_guessed character to the old_letters_guessed list if the conditions apply
old_letters_guessed.append(letter_guessed)
# The old_letters_guessed list as a string of lowercase letters
# Separated from one another by arrows
# The print is meant to remind the player which characters he has already guessed
print ((" -> ".join(sorted(old_letters_guessed))).lower())
# Printing a false value
print ("False")
continue
elif (letter_guessed in "!@#$%^""'=+-/*~&()<>|\[]?,.") or not(letter_guessed.isalpha()):
# If the letter letter_guessed contains a non-English character (like: &, *, etc...) --
# -- it will print the character X (the uppercase X)
print("X")
# Printing a false value
print ("False")
continue
elif len(letter_guessed) > 1:
# If the letter letter_guessed contains more than one character --
# -- it will print the character X (the uppercase X)
print("X")
if letter_guessed.lower() not in old_letters_guessed and not (letter_guessed in "!@#$%^""'=+-/*~&()<>|\[]?,."):
# It will add the letter_guessed character to the old_letters_guessed list if the conditions apply
old_letters_guessed.append(letter_guessed)
# Printing a false value
print ("False")
print (show_hidden_word(secret_word, old_letters_guessed)) # Printing the progress of the hidden word
continue
elif letter_guessed not in secret_word:
# It will add the letter_guessed character to the old_letters_guessed list if the condition apply
old_letters_guessed.append(letter_guessed)
# The old_letters_guessed list as a string of lowercase letters
# Separated from one another by arrows
# The print is meant to remind the player which characters he has already guessed
(" -> ".join(sorted(old_letters_guessed))).lower()
# Printing a false value
print ("False")
print (":(") # Printing a sad face
print_hangman(i) # Printing the hangman progressed paint
print (show_hidden_word(secret_word, old_letters_guessed)) # Printing the progress of the hidden word
i += 1
if i == 7: # If statement - When the loop is finish on 6 wrong guessed letters it will add one and i will equal 7
print ("LOSE") # If it's true and game is over it will print "lose"
def check_win(secret_word, old_letters_guessed):
"""
The function checks whether the player managed to guess the secret word so he get True if not he gets False
"""
result = "" # Making a new variable to contain the result of the guessed words and the underscores that will show which letters are still missing and wasn't guessed yet
for letter in secret_word: # Using "for" function to get the letters from the secret word
if letter in old_letters_guessed: # If the letter in the old_letters_guessed list
result += letter # If the letter was in the list it's adding this letter to the "result" string
result += " " # Making space between the letters and the underscores
else:
new = letter.replace(letter, "_ ") # If the letter wasn't guessed the empty space will be replaced by the underscore and one space
result += new # Adding the underscore to the result string
if "_" in result: # If there is underscore in the final result so it will return False
return False
else:
return True # If there is no underscore in the final result so it will return True
def main():
choose_word(file_path, index)
old_letters_guessed = []
secret_word = choose_word(file_path, index)
show_hidden_word(secret_word, old_letters_guessed)
num_of_tries = int
print_hangman(num_of_tries)
letter_guessed = []
try_update_letter_guessed(letter_guessed, old_letters_guessed)
check_win(secret_word, old_letters_guessed)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment