Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HANGMAN_PHOTOS = {
- 0:"""
- x-------x
- """,
- 1:"""
- x-------x
- |
- |
- |
- |
- |
- """,
- 2:"""
- x-------x
- | |
- | 0
- |
- |
- |
- """,
- 3:"""
- x-------x
- | |
- | 0
- | |
- |
- |
- """,
- 4:"""
- x-------x
- | |
- | 0
- | /|\\
- |
- |
- """,
- 5:"""
- x-------x
- | |
- | 0
- | /|\\
- | /
- |
- """,
- 6:"""
- x-------x
- | |
- | 0
- | /|\\
- | / \\
- |
- """
- }
- MAX_TRIES = 6
- def print_hangman(num_of_tries):
- """
- Function that print the hangman
- :param num_of_tries:
- :return: none
- """
- print(HANGMAN_PHOTOS[num_of_tries])
- def check_win(secret_word, old_letters_guessed):
- """
- Function that check if the player
- :param secret_word: the word the user need to guess
- :type str
- :param old_letters_guessed: the list of the old letters that the user guessed
- :type list
- :return: if the player win
- """
- flag = True
- for letter in secret_word:
- if letter not in old_letters_guessed:
- flag = False
- return flag
- def show_hidden_word(secret_word, old_letters_guessed):
- """
- Function that print the hidden word
- :param secret_word: the hidden word
- :type string
- :param old_letters_guessed: the letters that the user guess
- :type list
- :return: new string that hidde the letters the user didnt guess
- """
- string = ""
- for letter in secret_word:
- if letter in old_letters_guessed:
- string += letter
- else:
- string += "_"
- string += " "
- string = string[:-1]
- return string
- def check_valid_input(letter_guessed):
- """
- The function check if the input is valid.
- :param letter_guessed: the input that the func check.
- :type letter_guessed: str
- :param old_letters_guessed: the list of the old guesses
- :type old_letters_guessed: list
- :return: True if the input is valid False else
- :rtype boolean
- """
- if not(letter_guessed.isalpha()) or 1 < len(letter_guessed):
- return False
- else:
- return True
- def try_update_letter_guessed(letter_guessed, old_letters_guessed):
- """
- The function check if the input is valid.
- :param letter_guessed: the input that the func check.
- :type letter_guessed: str
- :param old_letters_guessed: the list of the old guesses
- :type old_letters_guessed: list
- :return: True if the input is valid False else
- :rtype boolean
- """
- seperator = " -> "
- if letter_guessed.lower() in old_letters_guessed:
- old_letters_guessed.sort()
- print("X\n" + seperator.join(old_letters_guessed))
- return False
- else:
- if(not(letter_guessed in old_letters_guessed)):
- old_letters_guessed += [letter_guessed]
- return True
- def print_start_screan():
- """
- print the hangman
- :return: none
- """
- print(""" _ _
- | | | |
- | |__| | __ _ _ __ __ _ _ __ ___ __ _ _ __
- | __ |/ _` | '_ \ / _` | '_ ` _ \ / _` | '_ \
- | | | | (_| | | | | (_| | | | | | | (_| | | | |
- |_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|
- __/ |
- |___/""")
- print(MAX_TRIES)
- def choose_word(file_path, index):
- """
- take the word from text file
- :param file_path:
- :type str
- :param index:
- :type int
- :return:
- """
- i = 0
- file_input = open(file_path, "r")
- string = file_input.read()
- string = string.split()
- new_list = []
- for item in string:
- if item not in new_list:
- new_list.append(item)
- while index != 0:
- if i < len(string):
- i += 1
- else:
- i = 1
- index -= 1
- return string[i - 1]
- def main():
- secret_word = ""
- old_letters_guessed = []
- letter_guessed = ""
- num_of_tries = 0
- flag = False
- print_start_screan()
- file_path = input("Enter file path: ")
- index = int(input("Enter index: "))
- print("Let's start!")
- print_hangman(num_of_tries)
- secret_word = choose_word(file_path, index)
- print(show_hidden_word(secret_word, old_letters_guessed))
- while not flag:
- letter_guessed = ""
- while not check_valid_input(letter_guessed):
- letter_guessed = input("Guess a letter: ")
- if not check_valid_input(letter_guessed):
- print("X")
- if not try_update_letter_guessed(letter_guessed,old_letters_guessed):
- pass
- else:
- num_of_tries += 1
- print(":(")
- if num_of_tries != MAX_TRIES + 1:
- print_hangman(num_of_tries)
- print(show_hidden_word(secret_word, old_letters_guessed))
- if check_win(secret_word,old_letters_guessed):
- print("WIN")
- flag = True
- elif num_of_tries == MAX_TRIES + 1:
- flag = True
- print("LOSE")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement