Advertisement
PeterSnk

Untitled

Oct 17th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.86 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. from map import rooms
  4. import string
  5.  
  6.  
  7. def remove_punct(text):
  8.     """This function is used to remove all punctuation
  9.    marks from a string. Spaces do not count as punctuation and should
  10.    not be removed. The funcion takes a string and returns a new string
  11.    which does not contain any puctuation. For example:
  12.  
  13.    >>> remove_punct("Hello, World!")
  14.    'Hello World'
  15.    >>> remove_punct("-- ...Hey! -- Yes?!...")
  16.    ' Hey  Yes'
  17.    >>> remove_punct(",go!So.?uTh")
  18.    'goSouTh'
  19.    """
  20.  
  21.     # define punctuation
  22.     punctuations = '''!()-[]{};:'"\,<>./?@#$£%^&*_~'''
  23.  
  24.  
  25.     # remove punctuation from the string
  26.     out_str = ""
  27.  
  28.     for char in text:
  29.         if char not in punctuations:
  30.             out_str = out_str + char
  31.     return out_str
  32.  
  33.  
  34. def remove_spaces(text):
  35.     """This function is used to remove leading and trailing spaces from a string.
  36.    It takes a string and returns a new string with does not have leading and
  37.    trailing spaces. For example:
  38.  
  39.    >>> remove_spaces("  Hello!  ")
  40.    'Hello!'
  41.    >>> remove_spaces("  Python  is  easy!   ")
  42.    'Python  is  easy!'
  43.    >>> remove_spaces("Python is easy!")
  44.    'Python is easy!'
  45.    >>> remove_spaces("")
  46.    ''
  47.    >>> remove_spaces("   ")
  48.    ''
  49.    """
  50.     t = text.lstrip()
  51.     t = t.rstrip()
  52.     return (t)
  53.  
  54.  
  55.  
  56. def normalise_input(user_input):
  57.     """This function removes all punctuation, leading and trailing
  58.    spaces from a string, and converts the string to lower case.
  59.    For example:
  60.  
  61.    >>> normalise_input("  Go south! ")
  62.    'go south'
  63.    >>> normalise_input("!!! tAkE,. LAmp!?! ")
  64.    'take lamp'
  65.    >>> normalise_input("HELP!!!!!!!")
  66.    'help'
  67.    """
  68.     rpunct = remove_punct(user_input)
  69.     rspace = remove_spaces(rpunct)
  70.     lcase = rspace.lower()
  71.  
  72.  
  73.     return lcase
  74.    
  75. def display_room(room):
  76.     """This function takes a room as an input and nicely displays its name
  77.    and description. The room argument is a dictionary with entries "name",
  78.    "description" etc. (see map.py for the definition). The name of the room
  79.    is printed in all capitals and framed by blank lines. Then follows the
  80.    description of the room and a blank line again. For example:
  81.  
  82.    >>> display_room(rooms["Office"])
  83.    <BLANKLINE>
  84.    THE GENERAL OFFICE
  85.    <BLANKLINE>
  86.    You are standing next to the cashier's till at
  87.    30-36 Newport Road. The cashier looks at you with hope
  88.    in their eyes. If you go west you can return to the
  89.    Queen's Buildings.
  90.    <BLANKLINE>
  91.  
  92.    Note: <BLANKLINE> here means that doctest should expect a blank line.
  93.    """
  94.     print("")
  95.     print((room["name"]).upper())
  96.     print("")
  97.     print(room["description"])
  98.     print("")
  99.  
  100.  
  101.  
  102. def exit_leads_to(exits, direction):
  103.     """This function takes a dictionary of exits and a direction (a particular
  104.    exit taken from this dictionary). It returns the name of the room into which
  105.    this exit leads. For example:
  106.  
  107.    >>> exit_leads_to(rooms["Reception"]["exits"], "south")
  108.    "MJ and Simon's room"
  109.    >>> exit_leads_to(rooms["Reception"]["exits"], "east")
  110.    "your personal tutor's office"
  111.    >>> exit_leads_to(rooms["Tutor"]["exits"], "west")
  112.    'Reception'
  113.    """
  114.  
  115.     return rooms[exits[direction]]["name"]
  116.    
  117.  
  118. def print_menu_line(direction, leads_to):
  119.  
  120.     """This function prints a line of a menu of exits. It takes two strings: a
  121.    direction (the name of an exit) and the name of the room into which it
  122.    leads (leads_to), and should print a menu line in the following format:
  123.  
  124.    Go <EXIT NAME UPPERCASE> to <where it leads>.
  125.  
  126.    For example:
  127.    >>> print_menu_line("east", "you personal tutor's office")
  128.    Go EAST to you personal tutor's office.
  129.    >>> print_menu_line("south", "MJ and Simon's room")
  130.    Go SOUTH to MJ and Simon's room.
  131.    """
  132.     print("Go " + direction.upper() + " to " + leads_to + ".")
  133.  
  134.  
  135. def print_menu(exits):
  136.     """This function displays the menu of available exits to the player. The
  137.    argument exits is a dictionary of exits as exemplified in map.py. The
  138.    menu should, for each exit, call the function print_menu_line() to print
  139.    the information about each exit in the appropriate format. The room into
  140.    which an exit leads is obtained using the function exit_leads_to().
  141.  
  142.    For example, the menu of exits from Reception may look like this:
  143.  
  144.    You can:
  145.    Go EAST to your personal tutor's office.
  146.    Go WEST to the parking lot.
  147.    Go SOUTH to MJ and Simon's room.
  148.    Where do you want to go?
  149.    """
  150.  
  151.  
  152.     print("You can:")
  153.     for x in exits:
  154.         print_menu_line(x, rooms[exits[x]]["name"])
  155.  
  156.     print("Where do you want to go?")
  157.  
  158.  
  159. def is_valid_exit(exits, user_input):
  160.     """This function checks, given a dictionary "exits" (see map.py) and
  161.    a players's choice "user_input" whether the player has chosen a valid exit.
  162.    It returns True if the exit is valid, and False otherwise. Assume that
  163.    the name of the exit has been normalised by the function normalise_input().
  164.    For example:
  165.  
  166.    >>> is_valid_exit(rooms["Reception"]["exits"], "south")
  167.    True
  168.    >>> is_valid_exit(rooms["Reception"]["exits"], "up")
  169.    False
  170.    >>> is_valid_exit(rooms["Parking"]["exits"], "west")
  171.    False
  172.    >>> is_valid_exit(rooms["Parking"]["exits"], "east")
  173.    True
  174.    """
  175.     if user_input in exits:
  176.         return True
  177.     else:
  178.         return False
  179.  
  180.  
  181. def menu(exits):
  182.     """This function, given a dictionary of possible exits from a room, prints the
  183.    menu of exits using print_menu() function. It then prompts the player to type
  184.    a name of an exit where she wants to go. The players's input is normalised
  185.    using the normalise_input() function before further checks are done.  The
  186.    function then checks whether this exit is a valid one, using the function
  187.    is_valid_exit(). If the exit is valid then the function returns the name
  188.    of the chosen exit. Otherwise the menu is displayed again and the player
  189.    prompted, repeatedly, until a correct choice is entered."""
  190.  
  191.     # Repeat until the player enter a valid choice
  192.     while True:
  193.         print_menu(exits)
  194.         # COMPLETE THIS PART:
  195.        
  196.         # Display menu
  197.         player_choice = input()
  198.    # Read player's input
  199.         normalised_input = normalise_input(player_choice)
  200.  #       # Normalise the input
  201.         TF = is_valid_exit(exits, normalised_input)
  202.         if TF == True:
  203.             return normalised_input
  204.  
  205.  
  206.  #       # Check if the input makes sense (is valid exit)
  207.  #           # If so, return the player's choice
  208.  
  209.  
  210. def move(exits, direction):
  211.     """This function returns the room into which the player will move if, from a
  212.    dictionary "exits" of avaiable exits, they choose to move towards the exit
  213.    with the name given by "direction". For example:
  214.  
  215.    >>> move(rooms["Reception"]["exits"], "south") == rooms["Admins"]
  216.    True
  217.    >>> move(rooms["Reception"]["exits"], "east") == rooms["Tutor"]
  218.    True
  219.    >>> move(rooms["Reception"]["exits"], "west") == rooms["Office"]
  220.    False
  221.    """
  222.  
  223.     return rooms[exits[direction]]
  224.  
  225.  
  226.  
  227. # This is the entry point of our program
  228. def main():
  229.     # Start game at the reception
  230.     current_room = rooms["Reception"]
  231.  
  232.     # Main game loop
  233.     while True:
  234.         # Display game status (room description etc.)
  235.         display_room(current_room)
  236.         # What are the possible exits from the current room?
  237.         exits = current_room["exits"]
  238.         # Show the menu with exits and ask the player
  239.         direction = menu(exits)
  240.         # Move the protagonist, i.e. update the current room
  241.         current_room = move(exits, direction)
  242.  
  243.  
  244.  
  245. # Are we being run as a script? If so, run main().
  246. # '__main__' is the name of the scope in which top-level code executes.
  247. # See https://docs.python.org/3.4/library/__main__.html for explanation
  248. if __name__ == "__main__":
  249.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement