Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.22 KB | None | 0 0
  1. HANGMAN_PHOTOS = {
  2.     0:"""
  3.    x-------x
  4.    """,
  5.     1:"""
  6.    x-------x
  7.    |
  8.    |
  9.    |
  10.    |
  11.    |
  12.    """,
  13.     2:"""
  14.    x-------x
  15.    |       |
  16.    |       0
  17.    |
  18.    |
  19.    |
  20.    """,
  21.     3:"""
  22.    x-------x
  23.    |       |
  24.    |       0
  25.    |       |
  26.    |
  27.    |
  28.    """,
  29.     4:"""
  30.    x-------x
  31.    |       |
  32.    |       0
  33.    |      /|\\
  34.    |
  35.    |
  36.    """,
  37.     5:"""
  38.    x-------x
  39.    |       |
  40.    |       0
  41.    |      /|\\
  42.    |      /
  43.    |
  44.    """,
  45.     6:"""
  46.    x-------x
  47.    |       |
  48.    |       0
  49.    |      /|\\
  50.    |      / \\
  51.    |
  52.    """
  53. }
  54.  
  55. MAX_TRIES = 6
  56.  
  57. def print_hangman(num_of_tries):
  58.     """
  59.    Function that print the hangman
  60.    :param num_of_tries:
  61.    :return: none
  62.    """
  63.     print(HANGMAN_PHOTOS[num_of_tries])
  64.  
  65. def check_win(secret_word, old_letters_guessed):
  66.     """
  67.    Function that check if the player
  68.    :param secret_word: the word the user need to guess
  69.    :type str
  70.    :param old_letters_guessed: the list of the old letters that the user guessed
  71.    :type list
  72.    :return: if the player win
  73.    """
  74.     flag = True
  75.     for letter in secret_word:
  76.         if letter not in old_letters_guessed:
  77.             flag = False
  78.     return flag
  79.  
  80. def show_hidden_word(secret_word, old_letters_guessed):
  81.     """
  82.    Function that print the hidden word
  83.    :param secret_word: the hidden word
  84.    :type string
  85.    :param old_letters_guessed: the letters that the user guess
  86.    :type list
  87.    :return: new string that hidde the letters the user didnt guess
  88.    """
  89.     string = ""
  90.     for letter in secret_word:
  91.         if letter in old_letters_guessed:
  92.             string += letter
  93.         else:
  94.             string += "_"
  95.         string += " "
  96.     string = string[:-1]
  97.     return string
  98.  
  99. def check_valid_input(letter_guessed):
  100.     """
  101.    The function check if the input is valid.
  102.    :param letter_guessed: the input that the func check.
  103.    :type letter_guessed: str
  104.    :param old_letters_guessed: the list of the old guesses
  105.    :type old_letters_guessed: list
  106.    :return: True if the input is valid False else
  107.    :rtype boolean
  108.    """
  109.     if not(letter_guessed.isalpha()) or 1 < len(letter_guessed):
  110.         return False
  111.     else:
  112.         return True
  113.  
  114. def try_update_letter_guessed(letter_guessed, old_letters_guessed):
  115.     """
  116.    The function check if the input is valid.
  117.    :param letter_guessed: the input that the func check.
  118.    :type letter_guessed: str
  119.    :param old_letters_guessed: the list of the old guesses
  120.    :type old_letters_guessed: list
  121.    :return: True if the input is valid False else
  122.    :rtype boolean
  123.    """
  124.     seperator = " -> "
  125.     if letter_guessed.lower() in old_letters_guessed:
  126.         old_letters_guessed.sort()
  127.         print("X\n" + seperator.join(old_letters_guessed))
  128.         return False
  129.     else:
  130.         if(not(letter_guessed in old_letters_guessed)):
  131.             old_letters_guessed += [letter_guessed]
  132.         return True
  133.  
  134. def print_start_screan():
  135.     """
  136.    print the hangman
  137.    :return: none
  138.    """
  139.     print("""  _    _                                        
  140. | |  | |                                        
  141. | |__| | __ _ _ __   __ _ _ __ ___   __ _ _ __  
  142. |  __  |/ _` | '_ \ / _` | '_ ` _ \ / _` | '_ \
  143. | |  | | (_| | | | | (_| | | | | | | (_| | | | |
  144. |_|  |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|
  145.                      __/ |                      
  146.                     |___/""")
  147.     print(MAX_TRIES)
  148.  
  149. def choose_word(file_path, index):
  150.     """
  151.    take the word from text file
  152.    :param file_path:
  153.    :type str
  154.    :param index:
  155.    :type int
  156.    :return:
  157.    """
  158.     i = 0
  159.     file_input = open(file_path, "r")
  160.     string = file_input.read()
  161.     string = string.split()
  162.     new_list = []
  163.     for item in string:
  164.         if item not in new_list:
  165.             new_list.append(item)
  166.     while index != 0:
  167.         if i < len(string):
  168.             i += 1
  169.         else:
  170.             i = 1
  171.         index -= 1
  172.     return string[i - 1]
  173.  
  174.  
  175. def main():
  176.     secret_word = ""
  177.     old_letters_guessed = []
  178.     letter_guessed = ""
  179.     num_of_tries = 0
  180.     flag = False
  181.     print_start_screan()
  182.     file_path = input("Enter file path: ")
  183.     index = int(input("Enter index: "))
  184.     print("Let's start!")
  185.     print_hangman(num_of_tries)
  186.     secret_word = choose_word(file_path, index)
  187.     print(show_hidden_word(secret_word, old_letters_guessed))
  188.     while not flag:
  189.         letter_guessed = ""
  190.         while not check_valid_input(letter_guessed):
  191.             letter_guessed = input("Guess a letter: ")
  192.             if not check_valid_input(letter_guessed):
  193.                 print("X")
  194.         if not try_update_letter_guessed(letter_guessed,old_letters_guessed):
  195.             pass
  196.         else:
  197.             num_of_tries += 1
  198.             print(":(")
  199.             if num_of_tries != MAX_TRIES + 1:
  200.                 print_hangman(num_of_tries)
  201.         print(show_hidden_word(secret_word, old_letters_guessed))
  202.         if check_win(secret_word,old_letters_guessed):
  203.             print("WIN")
  204.             flag = True
  205.         elif num_of_tries == MAX_TRIES + 1:
  206.             flag = True
  207.             print("LOSE")
  208.  
  209.  
  210.  
  211.  
  212. if __name__ == "__main__":
  213.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement