Advertisement
gruntfutuk

Coeyman

Feb 17th, 2023
785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.81 KB | None | 0 0
  1. # for Ronald Coeyman
  2.  
  3. from dataclasses import dataclass, field
  4. from enum import Enum, auto
  5.  
  6. # common ways in English to say yes or no
  7. AFFIRMATION: frozenset[str] = frozenset(('y', 'yes', 'yup', 'yeh', 'ok', '1'))
  8. REJECTION = frozenset(('n', 'no', 'nah', 'nope', '0'))
  9.  
  10. TAB = "\t"  # can't use a backslash inside an f-string
  11. NL = "\n"
  12.  
  13. def is_yes(prompt: str = "Yes or No?", default: bool | None = None) -> bool:
  14.     """ prompt user and only accept answer that is in AFFIRMATION or REJECTION sets
  15.    and return True or False accordingly; return alone can be accepted if default
  16.    set to either True or False in which case the default is returned"""
  17.     while True:
  18.         response = input(prompt).strip().casefold()
  19.         if not response and default is not None:
  20.             return default
  21.         if response in AFFIRMATION:
  22.             return True
  23.         if response in REJECTION:
  24.             return False
  25.         print('Sorry, I did not understand that')
  26.  
  27.  
  28. class Dirs(Enum):  # defining the only allowed cardinal direction terms
  29.     def _generate_next_value_(name, start, count, last_values):
  30.         return name[0]
  31.  
  32.     NORTH = auto()
  33.     SOUTH = auto()
  34.     EAST = auto()
  35.     WEST = auto()
  36.  
  37.  
  38. @dataclass
  39. class Item:
  40.     name: str
  41.     status: str = ""
  42.  
  43.     def __str__(self):
  44.         return (
  45.             f"{self. name}(status: {self.status})"
  46.         )
  47.  
  48.  
  49. class Direction:
  50.     pass  # so we can include Direction type in definition of Room
  51.  
  52. @dataclass
  53. class Room:
  54.     name: str
  55.     desc: str = ""
  56.     directions: list[Direction] = field(default_factory=list)  # empty list default
  57.     items: list[Item] = field(default_factory=list)  # empty list default
  58.  
  59.     def __str__(self):
  60.         # as there can be multiple entries of directions and items
  61.         # will create string versions of these first to including in return
  62.         dirs = f'{NL}{TAB}{TAB}'.join(str(dir) for dir in self.directions)
  63.         items = f'{NL}{TAB}{TAB}'.join(str(item) for item in self.items)
  64.         return (
  65.             f"{NL}Room: {self.name}{NL}"
  66.             f"{'{TAB}{self.desc}' if self.desc else ''}"
  67.             f"{TAB}Directions: {NL}{TAB}{TAB}{dirs}{NL}"
  68.             f"{TAB}Items:  {NL}{TAB}{TAB}{items}{NL}"
  69.             f"{NL}"
  70.         )
  71.  
  72. @dataclass
  73. class Direction:  # now redefining as we can reference Room type
  74.     direction: Dirs
  75.     room: Room
  76.  
  77.     def __str__(self):
  78.         return (
  79.             f"{self.direction.name} leads to room {self.room.name}"
  80.         )
  81.  
  82.  
  83. roomList = {
  84.     'the Crew Quarters': {'East': 'Pod B1'},
  85.     'Pod A1': {'Item': Item(name='Helmet', status='your Protective Helmet hanging on a hook by your desk'),
  86.                'South': 'Pod B1',
  87.                'East': 'Pod A2'},
  88.     'Pod A2': {'Item': Item(name='Shock Gun', status='a Shock Gun just inside the weapon\'s locker'),
  89.                'South': 'Pod B2',
  90.                'East': 'Pod A3', 'West': 'Pod A1'},
  91.     'Pod A3': {'Item': Item(name='C.C.Remote',
  92.                             status='the Contamination Control Remote laying on a desk, covered by loose papers and dust'),
  93.                'South': 'Pod B3',
  94.                'West': 'Pod A2'},
  95.     'Pod B1': {'Item': Item(name='Lucky Boots',
  96.                             status='your Lucky Boots which you remember throwing off last night right outside of your '),
  97.                'North': 'Pod A1',
  98.                'East': 'Pod B2',
  99.                'West': 'the Crew Quarters',
  100.                'South': 'Pod C1'},
  101.     'Pod B3': {'Item': Item(name='Vest',
  102.                             status='your Standard Issue Protective Vest sticking out from '
  103.                                    'behind the improperly closed clothing locker door'),
  104.                'North': 'Pod A3',
  105.                'West': 'Pod B2',
  106.                'South': 'Pod C3'},
  107.     'Pod C1': {'Item': Item(name='O2 Tank',
  108.                             status='a Spare Oxygen Tank in an auxiliary cabinet'),
  109.                'North': 'Pod B1',
  110.                'East': 'Pod C2'},
  111.     'Pod C2': {'Item': Item(name='E.C. Remote',
  112.                             status='the Environmental Control Remote placed on a console beside a couple of flashing screens'),
  113.                'North': 'Pod B2',
  114.                'East': 'Pod C3',
  115.                'West': 'Pod C1'},
  116.     'Pod C3': {'Item': Item(name='Explosives',
  117.                             status='a pack of Explosives in a locker. Just looking at them causes you to nervously sweat..'),
  118.                'North': 'Pod B3',
  119.                'West': 'Pod C2'}
  120. }
  121.  
  122. directions = {
  123.             Dirs.NORTH: ('North', 'N', 'Up', 'U'),
  124.             Dirs.SOUTH: ('South', 'S', 'Down', 'D'),
  125.             Dirs.EAST: ('East', 'E', 'Right', 'R'),
  126.             Dirs.WEST: ('West', 'W', 'Left', 'L')
  127.             }
  128. commands = directions | {'p_exit': ('Exit', 'Quit')}
  129. command_lookup = {possible: dir for dir, possibles in commands.items() for possible in possibles}
  130.  
  131. # let's create our dictionary of rooms
  132. # firstly basic Room object entries
  133. rooms: dict[str, Room] = {room_name: Room(room_name) for room_name in roomList}
  134. # and now convert and add the original Item and direction fields to new objects
  135. for room_name, info in roomList.items():
  136.     if 'Item' in info:
  137.         rooms[room_name].items = [info['Item']]
  138.     for key, value in info.items():
  139.         if key == 'Item':
  140.             continue
  141.         command = command_lookup.get(key, None)
  142.         if command in Dirs:
  143.             if value not in rooms:
  144.                 rooms[value] = Room(value)
  145.             rooms[room_name].directions.append(Direction(command, rooms[value]))
  146.  
  147. # just to see what we have, let's output all of the room output
  148. # note that this depends on the __str__ (human readable representation) definitions
  149. # written for some of the classes
  150. for room in rooms.values():  # only looking at the Room objects
  151.     print(room)
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement