def connect_places(start: dict, end: dict, direction: str, dir_options: dict): """ Connect two places to each other. Simultaneously add opposite directions to both places. Args: start: go from {place} end: go to {place} direction: "direction" dir_options: dict with corresponding directions Returns: {p1}, {p2} place dictionaries with updated directions """ # Check that both start and end are {place} objects: if not (general.is_place(start) and general.is_place(end)): print('Error:', f'\n\t{start["name"]} is', 'not' * general.is_place(start), 'a place', f'\n\t{end["name"]} is', 'not' * general.is_place(end), 'a place') # Create connection only if the direction is valid: elif direction not in dir_options: print(f'There is no such direction as {direction}') # Check that start has no previous connection in that direction: elif start['connections'].get(direction, False): # If there is existing connection, print message and do not change it: print( f'{start["name"]} is already connected in {direction} to {start["connections"].get(direction)["name"]}' ) # Check that end has no previous connection in opposite direction: elif end['connections'].get(dir_options[direction], False): # If there is existing connection, print message and do not change it: print("{0} is already connected in opposite direction to {1}".format( end['name'], end['connections'].get(dir_options[direction])['name'])) else: # Create connection for both objects: start['connections'][direction] = end end['connections'][dir_options[direction]] = start # Print info message: print( f'Successfully created route: {start["name"]:>20} <---> {end["name"]:<20}' ) return start, end
def pickup_item(player: dict, item: dict, place: dict): """Move {item} from it current location and place it in Player's Inventory. Args: player: {player} object; item: {item} to be moved; place: {place} where {item} is located now Return: Updated {player}, {item} and {place} objects """ # Check that {player} and {item} objects was passed and that: if not (general.is_player(player) and general.is_item(item) and general.is_place(place)): print( f'{player["name"]}, {item["name"]} or {place["name"]} is not an appropriate object.' ) # Check if the {item} is located in {place}: elif item.get('location') == place: # Remove {item} from its current location: place['items'].remove(item) # Move the {item} to Inventory: item['location'] = player player['items'].append(item) # Update player's actions: player = update_actions(player) else: print(f"There is no {item['name']} in {place['name']}") return player, item, place
def drop_item(player: dict, item: dict, place: dict): """Move {item} from Player's Inventory to a specified location. Args: player: {player} object; item: {item} to be moved; place: {place} where {item} should be placed Return: Updated {player}, {item} and {place} objects """ # Check that {player} and {item} objects was passed and that: if not (general.is_player(player) and general.is_item(item) and general.is_place(place)): print( f'{player["name"]}, {item["name"]} or {place["name"]} is not an appropriate object.' ) else: # Remove {item} from Inventory: player['items'].remove(item) # Move the {item} to Inventory: item['location'] = place place['items'].append(item) # Update player's actions: player = update_actions(player) print(f"You have dropped {item['name']} in {place['name']}") return player, item, place