diff --git a/src/adv.py b/src/adv.py index c9e26b0f85..250fc21d0e 100644 --- a/src/adv.py +++ b/src/adv.py @@ -1,4 +1,6 @@ from room import Room +from player import Player +from item import Item # Declare all the rooms @@ -21,6 +23,22 @@ earlier adventurers. The only exit is to the south."""), } +# Declare all the items + +item = { + 'sword': Item("sword", """a close range weapon used to defeat enemies, cut tall grass, and break open clay pots."""), + + 'rupee': Item("rupee", """this is the primary local unit of currency and can be used to purchase items from the local shops."""), + + 'key': Item("key", """this key looks like it would fit into a lock on a treasure chest."""), + + 'potion': Item("potion", """drink this potion to replenish your health if you are running low."""), + + 'hookshot': Item("hookshot", """a spring-loaded, trigger-pulled hooks attached to lengthy chains. It can attack enemies at a distance, + retrieve remote items, and attach onto certain surfaces + (like wood) to pull you accross large distances.""") +} + # Link rooms together @@ -33,6 +51,13 @@ room['narrow'].n_to = room['treasure'] room['treasure'].s_to = room['narrow'] +# Add items to room: + +room['outside'].items = [item['sword']] +room['foyer'].items = [item['rupee'], item['potion']] +room['overlook'].items = [item['hookshot']] +room['treasure'].items = [item['key']] + # # Main # @@ -49,3 +74,49 @@ # Print an error message if the movement isn't allowed. # # If the user enters "q", quit the game. + +def print_valid_commands(): + print("""Valid commands: + \'n\', \'s\', \'e\', or \'w\' move North, South, East, or West + \'take \' pickup an item, where is the item name + \'drop \' drop an item, where is the item name + \'i\' or \'inventory\' view the items currently in your inventory + \'q\' quit\n""") + + + # Program Start: + + possible_directions = ['n', 's', 'e', 'w'] + player = Player("David", room["outside"]) + + player.print_location_status() + print_valid_commands() + + # REPL Start: + while True: + cmd = input("What would you like to do? ").strip().lower().split() + num_words = len(cmd) + + if num_words == 1: + cmd = cmd[0] + if cmd == 'q': + print("\nThanks for playing! Goodbye.\n") + break + if cmd in possible_directions: + player.try_direction(cmd) + continue + elif cmd == 'i' or cmd == 'inventory': + player.print_inventory() + continue + elif num_words == 2: + verb = cmd[0] + item_name = cmd[1] + if verb == 'get' or verb == 'take': + player.try_add_item_to_inventory(item_name) + continue + elif verb == 'drop': + player.try_drop_item_from_inventory(item_name) + continue + + print("Invalid input, please try again.\n") + print_valid_commands() diff --git a/src/item.py b/src/item.py new file mode 100644 index 0000000000..b5145318cf --- /dev/null +++ b/src/item.py @@ -0,0 +1,13 @@ +class Item: + def __init__(self, name, description): + self.name = name + self.description = description + + def __str__(self): + return f"{self.name}: {self:description}" + + def on_take(self): + print(f"\nYou have picked up the {self.name} and added it to your inventory.\n") + + def on_drop(self): + print(f"\nYou dropped the {self.name}.\n") \ No newline at end of file diff --git a/src/player.py b/src/player.py index d79a175029..8ad6cd6e36 100644 --- a/src/player.py +++ b/src/player.py @@ -1,2 +1,78 @@ # Write a class to hold player information, e.g. what room they are in # currently. + +from room import Room + +direction_names = { + 'n': 'North', + 's': 'South', + 'e': 'East', + 'w': 'West' +} + +class Player: + def __init__(self, name, current_room, inventory = []): + self.name = name + self.current_room = current_room + self.inventory = inventory + self.name_of_last_item_handled = "it" + + def try_direction(self, cmd): + attribute = cmd + '_to' + if hasattr(self.current_room, attribute): + self.current_room = getattr(self.current_room, attribute) + self.print_location_status() + else: + print(f"\nYou cannot move {direction_names[cmd]} from here. Please try another direction.\n") + + def print_location_status(self): + print(f'\nYou are now in room: {self.current_room.name}\n') + print(f"{self.current_room.description}\n") + + num_items = len(self.current_room.items) + + if num_items > 0: + s = "\n " + item_descriptions = s + s.join(str(item) for item in self.current_room.items) + item_plural = "item" if num_items == 1 else "items" + print(f"This room has {num_items} {item_plural} in it: {item_descriptions}\n") + else: + print("There are no items in this room.\n") + + def try_add_item_to_inventory(self, item_name): + if item_name == "it": + item_name = self.name_of_last_item_handled + for item in self.current_room.items: + if item.name == item_name: + self.name_of_last_item_handled = item_name + self.inventory.append(item) + self.current_room.items.remove(item) + item.on_take() + break + else: + print(f"\n...ERM, there is no item named \"{item_name}\" in this room.\n") + + def try_drop_item_from_inventory(self, item_name): + if item_name == "it": + item_name = self.name_of_last_item_handled + for item in self.inventory: + if item.name == item_name: + self.name_of_last_item_handled = item_name + self.current_room.items.append(item) + self.inventory.remove(item) + item.on_drop() + print(f"The {item_name} is now in room: {self.current_room.name}.\n") + break + else: + print(f"\n... Erm, you do not have an item named \"{item_name}\" in your inventory.\n") + + def print_inventory(self): + num_items = len(self.inventory) + + if num_items > 0: + s = "\n " + item_descriptions = s + s.join(str(item) for item in self.inventory) + item_plural = "item" if num_items == 1 else "items" + print(f"\nYou have {num_items} {item_plural} in your inventory: {item_descriptions}\n") + else: + print("\nYou have 0 items in your inventory.\n") diff --git a/src/room.py b/src/room.py index 24c07ad4c8..57d52983bd 100644 --- a/src/room.py +++ b/src/room.py @@ -1,2 +1,8 @@ # Implement a class to hold room information. This should have name and -# description attributes. \ No newline at end of file +# description attributes. + +class Room: + def __init__(self, name, description, items = []): + self.name = name + self.description = description + self.items = items \ No newline at end of file