Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.32 KB | None | 0 0
  1. import random
  2. import time
  3. import datetime
  4.  
  5. color = {
  6.     'white':    "\033[1,37m",
  7.     'yellow':   "\033[1,33m",
  8.     'green':    "\033[1,32m",
  9.     'blue':     "\033[1,34m",
  10.     'cyan':     "\033[1,36m",
  11.     'red':      "\033[1,31m",
  12.     'magenta':  "\033[1,35m",
  13.     'black':    "\033[1,30m",
  14.     'darkwhite':  "\033[0,37m",
  15.     'darkyellow': "\033[0,33m",
  16.     'darkgreen':  "\033[0,32m",
  17.     'darkblue':   "\033[0,34m",
  18.     'darkcyan':   "\033[0,36m",
  19.     'darkred':    "\033[0,31m",
  20.     'darkmagenta': "\033[0,35m",
  21.     'darkblack':  "\033[0,30m",
  22.     'off':        "\033[0,0m"
  23. }
  24. yellow = "\033[1;33m"
  25. darkblue = "\033[0;34m"
  26. red = "\033[1;31m"
  27. off = "\033[0;0m"
  28. darkcyan = "\033[0;36m"
  29. darkgreen = "\033[0;32m"
  30. darkmagenta = "\033[0;35m"
  31. darkwhite = "\033[0;37m"
  32. darkyellow = "\033[0;33m"
  33. blue = "\033[1;34m"
  34. darkred = "\033[0;31m"
  35.  
  36. repair_sortowanie = []
  37. highscores = []
  38. today = datetime.date.today()
  39. hangman = []
  40. hangman_looks = [
  41.  """
  42.               +---+
  43.               |   |
  44.                    |
  45.                    |
  46.                    |
  47.                   |
  48.            =========
  49.          5 LIVES LEFT
  50. """,
  51.  """
  52.               +---+
  53.               |   |
  54.               O   |
  55.               |   |
  56.                    |
  57.                   |
  58.            =========
  59.          4 LIVES LEFT
  60. """,
  61.  """
  62.               +---+
  63.               |   |
  64.               O   |
  65.              /|   |
  66.                    |
  67.                   |
  68.            =========
  69.          3 LIVES LEFT
  70. """,
  71.  """
  72.               +---+
  73.               |   |
  74.               O   |
  75.              /|\ |
  76.                    |
  77.                   |
  78.            =========
  79.          2 LIVES LEFT
  80. """,
  81.  """
  82.               +---+
  83.               |   |
  84.               O   |
  85.              /|\ |
  86.              /    |
  87.                   |
  88.            =========
  89.          1 LIFE LEFT
  90. """,
  91.  """
  92.               +---+
  93.               |   |
  94.               O   |
  95.              /|\ |
  96.              / \ |
  97.                   |
  98.            =========
  99.          YOU ARE DEAD
  100. """,
  101.  """
  102. _
  103. | |
  104. | |__   __ _ _ __   __ _ _ __ ___   __ _ _ ___
  105. | '_ \ / _` | '_ \ / _` | '_ ` _ \ / _` | '_  |
  106. | | | | (_| | | | | (_| | | | | | | (_| | | | |
  107. |_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|
  108.                    __/ |
  109.                   |___/
  110. """,
  111.  
  112. ]
  113.  
  114.  
  115. def download_data_from_file():
  116.     """ it opens a .csv file with countries and capitals,
  117.    removing \n from the end of every line
  118.    """
  119.     with open('countries_and_capitals.txt') as f:  # read the file
  120.         lines = f.readlines()
  121.     # remove '\n' from the end of a pair
  122.     lines = [line.rstrip('\n') for line in open('countries_and_capitals.txt')]
  123.     return lines
  124.  
  125.  
  126. def choose_random_capital(data_storage):
  127.     # choose random pair from the list
  128.     pair = random.choice(data_storage).upper()
  129.     return pair
  130.  
  131.  
  132. def sorting_the_highscore(length_of_game_time, output):
  133.     """
  134.    Highscores - is a list which contain string
  135.    repair_sortowanie - contain each value of length game_time
  136.    repair_sortowanie is a list which is sorted from the lowest to the highest number
  137.    strings in highscores are sorted the same as repair_sortowanie
  138.    """
  139.     highscores = output
  140.     repair_sortowanie = length_of_game_time
  141.     for i in range(len(repair_sortowanie)):
  142.         for q in range(len(repair_sortowanie)):
  143.  
  144.             if repair_sortowanie[q] > repair_sortowanie[i]:
  145.                 save_HighS_for_replace = repair_sortowanie[i]
  146.                 repair_sortowanie[i] = repair_sortowanie[q]
  147.                 repair_sortowanie[q] = save_HighS_for_replace
  148.  
  149.                 sort_second_list = highscores[i]
  150.                 highscores[i] = highscores[q]
  151.                 highscores[q] = sort_second_list
  152.  
  153.     highscores = highscores[:10]
  154.     repair_sortowanie = repair_sortowanie[:10]
  155.  
  156.  
  157. def guessing_a_word_correctly(
  158.                             guesses_number, name,
  159.                             chosen_capital, pair2, start_time):
  160.     """ if user guesses a whole word and his
  161.        guess is correct, then his attemptt is
  162.        saved to highscore, highscore is being sorted
  163.        then user is asked if he'd like to play again
  164.    """
  165.     print("YOU WIN, the capital was " + darkmagenta
  166.           + "%s" % chosen_capital + off)
  167.     print("It's the capital of " + darkyellow + "%s" % pair2[0] + off)
  168.     game_time = ("%.2f" % (time.time() - start_time))
  169.     print("It took you %s seconds" % game_time)  # showing the time
  170.     print("It took you %s guesses" % guesses_number)
  171.     highscores.append("")
  172.     highscores[len(highscores) - 1] = "%s seconds" % game_time
  173.     + " | " + "Guesses: #%s" % guesses_number
  174.     + " | " + name
  175.     + " | " + str(today)
  176.     repair_sortowanie.append(float(game_time))
  177.     """sort the elements of highscores list
  178.        then sort second list in the way of first"""
  179.     if len(highscores) > 1:
  180.         sorting_the_highscore(repair_sortowanie, highscores)
  181.     print(red + "\n" + "HIGHSCORES:" + off)
  182.     print("Position | Seconds | Guesses | Name | Date" + '\n')
  183.     print('\n'.join(highscores), end="\n")
  184.     answer = ""
  185.     while answer not in ["Y", "N"]:
  186.         answer = input('\n' + "Would you like to play again? (Y/N)").upper()
  187.     if answer == "N":
  188.         gameplay = 1
  189.         return gameplay
  190.     else:
  191.         gameplay = 0
  192.         return gameplay
  193.  
  194.  
  195. def guessing_a_word_incorrectly(lives, chosen_capital):
  196.     """ if the user guesses a whole word incorrectly,
  197.        then it's checked if he has any more lives,
  198.        if he does, then he sees another hangman
  199.        visual presentation, otherwise he looses
  200.        the game and is asked if he'd like
  201.        to play again
  202.    """
  203.     print(red + "Wrong word!" + off)
  204.     print(hangman_looks[lives])
  205.     if lives > 4:
  206.         print(red + "GAME OVER" + off)
  207.         print(darkgreen + "The correct word was" + off, red
  208.               + "%s" % chosen_capital + off)
  209.         answer = ""
  210.         while answer not in ["Y", "N"]:
  211.             answer = input('\n'
  212.                            + "Would you like to play again? (Y/N): ").upper()
  213.         if answer == "N":
  214.             gameplay = 1
  215.         elif answer == "Y":
  216.             gameplay = 0
  217.  
  218.         return gameplay
  219.  
  220.  
  221. def guessing_a_letter_correctly(guess, lives, chosen_capital, hangman):
  222.     """if user guesses a letter correctly '_' is removed,
  223.        and correct letter is added on its place.
  224.        Function also removes every blank spaces in the city name
  225.    """
  226.     counter = 0
  227.     element_pos = []
  228.     # Correct letter replaces the '_' in our word to guess
  229.     for i in chosen_capital:
  230.         if i == guess:
  231.             element_pos.append(counter)
  232.         counter = counter + 1
  233.     if len(element_pos) > 0:  # it replaces the '_' with typed letter
  234.         c = 0  # position counter
  235.         for i in element_pos:
  236.             a = element_pos[c]
  237.             hangman.pop(a)
  238.             hangman.insert(a, guess)
  239.             c = c + 1  # counter value rises by 1
  240.         # Removing the blank space:
  241.     if " " in chosen_capital:  # if there is a blank in the city name
  242.         counter1 = 0  # counter that checks every single letter in a city name
  243.         element_pos1 = []  # list that cotains the position of blank space
  244.         for i in chosen_capital:
  245.             if i == " ":
  246.                 """if a letter in a word is a blank space,
  247.                    it's position is added to the list"""
  248.                 element_pos1.append(counter1)
  249.             counter1 = counter1 + 1
  250.         if len(element_pos1) > 0:
  251.             c1 = 0
  252.             for i in element_pos1:
  253.                 a1 = element_pos1[c1]
  254.                 hangman.pop(a1)  # removes '_' where blank space should be
  255.                 hangman.insert(a1, " ")  # inserts blank space in that place
  256.                 c1 = c1 + 1
  257.  
  258.  
  259. def guessing_a_letter_incorrectly(lives, pair2, chosen_capital):
  260.     """ if user has any more lives, but less than 3,
  261.        he gets a tip about country name and sees a proper
  262.        image of hanging man.
  263.        Otherwise he looses, game's over and is asked
  264.        if'd like to play again
  265.    """
  266.     print(red + 'Wrong letter' + off)
  267.     if lives > 1:
  268.         print(hangman_looks[lives])
  269.         print(darkcyan + "Tip: A capital of %s" % pair2[0] + off)
  270.     else:
  271.         print(hangman_looks[lives])
  272.     if lives > 4:
  273.         print(red + "GAME OVER" + off)
  274.         print(darkgreen + "The correct word was"
  275.               + off, red + "%s" % chosen_capital + off)
  276.         answer = ""
  277.         while answer not in ["Y", "N"]:
  278.             answer = input('\n'
  279.                            + "Would you like to play again? (Y/N)").upper()
  280.         if answer == "N":
  281.             gameplay = 1
  282.         elif answer == "Y":
  283.             gameplay = 0
  284.         return gameplay
  285.  
  286.  
  287. pick_up_data = download_data_from_file()
  288.  
  289.  
  290. def main():
  291.     gameplay = 0
  292.     while gameplay == 0:
  293.         lives = -1
  294.         records = 0
  295.         pair = choose_random_capital(pick_up_data)
  296.         # splits the pair into: [capital, separator, country]
  297.         pair2 = pair.partition(" | ")[0:5]
  298.         hangman = []
  299.         used_letters = []
  300.         chosen_capital = pair2[2]
  301.         start_time = time.time()  # starting the clock
  302.         lives = -1
  303.         for i in range(len(chosen_capital)):
  304.             hangman.append("_ ")
  305.         print(darkred + (hangman_looks[6]) + off)
  306.         guesses_number = 0
  307.         print(
  308.             "Welcome to the great HANGMAN game.\
  309.            \nAll you have to do is to guess a name of random world capital city.\
  310.            \nGood luck!\n"
  311.         )
  312.         name = input(blue + "What is your name: " + off)
  313.         while lives < 5:
  314.             print(yellow + "Your capital is: " + off
  315.                   + darkblue + ' '.join(hangman) + off)
  316.             hangman2 = (''.join(hangman))
  317.             # winning while the whole word is made out of collected letters
  318.             if hangman2 == chosen_capital:
  319.                 gameplay = guessing_a_word_correctly(
  320.                                 guesses_number, name,
  321.                                 chosen_capital, pair2,
  322.                                 start_time
  323.                                 )
  324.                 break
  325.             print(darkwhite + "Used letters: "
  326.                   + (" ".join(used_letters)) + off)
  327.             # Get a letter or a word
  328.             guess = input(darkblue + "Guess a letter or a whole word: "
  329.                           + off).upper()
  330.             if guess in used_letters:  # Checking if letter was used before
  331.                 print("You've already used this letter!")
  332.                 guesses_number = guesses_number + 1
  333.                 pass
  334.             if len(guess) == 1:  # guessing a letter
  335.                 if guess not in used_letters:
  336.                     guesses_number = guesses_number + 1
  337.                     # Adding a letter to the list of used letters
  338.                     used_letters.append(guess)
  339.                     if guess in chosen_capital:  # If the letter is correct
  340.                         gameplay = guessing_a_letter_correctly(
  341.                                                 guess, lives,
  342.                                                 chosen_capital, hangman
  343.                                                 )
  344.                         # if the letter is incorrect
  345.                     elif guess not in chosen_capital:
  346.                         guesses_number = guesses_number + 1
  347.                         lives = lives + 1
  348.                         gameplay = guessing_a_letter_incorrectly(
  349.                                             lives, pair2, chosen_capital
  350.                                             )
  351.             if len(guess) > 1:   # if you typed a word, instead of a letter
  352.                 guesses_number = guesses_number + 1
  353.                 if guess == chosen_capital:  # if typed the word is correct
  354.                     gameplay = guessing_a_word_correctly(
  355.                                     guesses_number, name, chosen_capital,
  356.                                     pair2, start_time
  357.                                     )
  358.                     break
  359.                 if guess != chosen_capital:  # if the word is incorrect
  360.                     lives = lives + 1
  361.                     gameplay = guessing_a_word_incorrectly(
  362.                                         lives, chosen_capital
  363.                                         )
  364.  
  365.  
  366. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement