From 99b790a5457002b7a069f130a67e292990577989 Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Tue, 8 Sep 2020 20:07:04 -0700 Subject: [PATCH 1/6] Created item file --- src/item.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/item.py diff --git a/src/item.py b/src/item.py new file mode 100644 index 0000000000..d1ef0f075d --- /dev/null +++ b/src/item.py @@ -0,0 +1,13 @@ +class item: + def __item__(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 From ff8be1c6a04bedc74c4db0c7ad63d1014a56357b Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Tue, 8 Sep 2020 20:10:46 -0700 Subject: [PATCH 2/6] Created a Room class on room file --- src/room.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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 From 2c44c78578f6cf77f000b5c2fa1ff1c41cf723f0 Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Tue, 8 Sep 2020 20:39:22 -0700 Subject: [PATCH 3/6] Created a Player Class --- src/player.py | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) 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") From 8332eccc2e196f721c37ba2c051ea2dd026dc85c Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Wed, 9 Sep 2020 00:31:50 -0700 Subject: [PATCH 4/6] Modified adv.py file. While loop was missing on the file to make to correct the errors --- src/adv.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/item.py | 2 +- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/src/adv.py b/src/adv.py index c9e26b0f85..c68a90d2c8 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 index d1ef0f075d..efcfdf191f 100644 --- a/src/item.py +++ b/src/item.py @@ -1,4 +1,4 @@ -class item: +class Item: def __item__(self, name, description): self.name = name self.description = description From b66fa68a9b23841683d68a9469f4368b86108c57 Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Wed, 9 Sep 2020 18:35:20 -0700 Subject: [PATCH 5/6] MAde some changes on the adv file. --- src/adv.py | 4 ++-- src/item.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/adv.py b/src/adv.py index c68a90d2c8..ef0783fb90 100644 --- a/src/adv.py +++ b/src/adv.py @@ -26,7 +26,7 @@ # Declare all the items item = { - 'sword': Item("sword", """a close range weapon used to defeat enemies, cut tall grass, and break open clay pots"""), + '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."""), @@ -36,7 +36,7 @@ '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."""), + (like wood) to pull you accross large distances.""") } diff --git a/src/item.py b/src/item.py index efcfdf191f..b5145318cf 100644 --- a/src/item.py +++ b/src/item.py @@ -1,5 +1,5 @@ class Item: - def __item__(self, name, description): + def __init__(self, name, description): self.name = name self.description = description From be47c15988fd170e106c8af2eb050e8fdc934676 Mon Sep 17 00:00:00 2001 From: Christian Lorenzo Date: Thu, 10 Sep 2020 19:51:06 -0700 Subject: [PATCH 6/6] MOdified adv file --- src/adv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adv.py b/src/adv.py index ef0783fb90..250fc21d0e 100644 --- a/src/adv.py +++ b/src/adv.py @@ -94,7 +94,7 @@ def print_valid_commands(): # REPL Start: while True: - cmd = input("What would you like to do?").strip().lower().split + cmd = input("What would you like to do? ").strip().lower().split() num_words = len(cmd) if num_words == 1: