From 91bef1da1ea4938779c6f78366fcff2bf51f064e Mon Sep 17 00:00:00 2001 From: PeteDram Date: Wed, 9 Sep 2020 18:37:40 -0700 Subject: [PATCH 1/5] MVP 1 complete --- src/adv.py | 30 +++++++++++++++++++++++++++++- src/player.py | 17 +++++++++++++++++ src/room.py | 23 ++++++++++++++++++++++- 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/src/adv.py b/src/adv.py index c9e26b0f85..059347afad 100644 --- a/src/adv.py +++ b/src/adv.py @@ -1,4 +1,6 @@ -from room import Room +from room import (Room, valid_directions) +from player import Player + # Declare all the rooms @@ -38,6 +40,7 @@ # # Make a new player object that is currently in the 'outside' room. +player = Player('Pete', room['outside']) # Write a loop that: # @@ -49,3 +52,28 @@ # Print an error message if the movement isn't allowed. # # If the user enters "q", quit the game. + +def is_direction(str): + """ + returns true from string if it is a valid + """ + return str in valid_directions + +print(f'Welcome {player.name}, press q at any time to quit') +print(f'You are currently {player.current_room.name}') +print(player.current_room.description) +current_room = player.current_room + +while True: + if current_room != player.current_room: + print(player.current_room) + current_room = player.current_room + current_room = player.current_room + user_input = input('Where would you like to go? n, e, s or w?: ') + if user_input == 'q': + break + elif is_direction(user_input): + player.move(user_input) + else: + print('Sorry that is not a valid command, please try again!') + diff --git a/src/player.py b/src/player.py index d79a175029..67b321941a 100644 --- a/src/player.py +++ b/src/player.py @@ -1,2 +1,19 @@ # Write a class to hold player information, e.g. what room they are in # currently. +class Player: + def __init__(self, name, current_room): + self.name = name + self.current_room = current_room + + def move(self, direction): + new_room = getattr(self.current_room, f"{direction}_to") + if (new_room) is not None: + self.current_room = new_room + else: + print("Sorry you can't move in that direction") + + def __str__(self): + return '{self.name} {self.room}'.format(self=self) + + + diff --git a/src/room.py b/src/room.py index 24c07ad4c8..1ad56a44c2 100644 --- a/src/room.py +++ b/src/room.py @@ -1,2 +1,23 @@ # Implement a class to hold room information. This should have name and -# description attributes. \ No newline at end of file +# description attributes. +valid_directions = ('n','s','e','w') + +class Room: + def __init__(self, name, description): + self.name = name + self.description = description + self.n_to = None + self.s_to = None + self.e_to = None + self.w_to = None + + def __str__(self): + return '{self.name} {self.description}'.format(self=self) + + def show_directions(self): + possible_directions = filter(lambda d: getattr(self, f"{d}_to") is not None, valid_directions) + return ", ".join(list(map(to_upper, possible_directions))) + + def print_welcome(self): + print(f'Welcome to {self.name}!') + print(f'{self.description}') From 6d507424dacaf55bff939a07e4fa5046e5262649 Mon Sep 17 00:00:00 2001 From: PeteDram Date: Thu, 10 Sep 2020 18:55:22 -0700 Subject: [PATCH 2/5] item.py added with class --- src/item.py | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/item.py diff --git a/src/item.py b/src/item.py new file mode 100644 index 0000000000..a1602e63bb --- /dev/null +++ b/src/item.py @@ -0,0 +1,4 @@ +class Item: + def __init__(self, name, description): + self.name = name + self.description = description \ No newline at end of file From f87d89877f4ac3020b40dace6aee81e7b8102133 Mon Sep 17 00:00:00 2001 From: PeteDram Date: Thu, 10 Sep 2020 20:07:46 -0700 Subject: [PATCH 3/5] items added and printing --- src/adv.py | 20 ++++++++++++++------ src/player.py | 7 +++++++ src/room.py | 9 ++++++++- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/adv.py b/src/adv.py index 059347afad..a2995c0c41 100644 --- a/src/adv.py +++ b/src/adv.py @@ -1,26 +1,32 @@ from room import (Room, valid_directions) from player import Player +from item import Item +#Items +sword = Item('sword', 'a sharp two-edged sword') +coins = Item('coins', 'a bag of gold coins') +torch = Item('torch', 'a bright torch') +cloak = Item('cloak', 'a warm cloak') +helmet = Item('helmet', 'a helmet made of damascus steel') # Declare all the rooms - room = { 'outside': Room("Outside Cave Entrance", - "North of you, the cave mount beckons"), + "North of you, the cave mount beckons", [cloak]), 'foyer': Room("Foyer", """Dim light filters in from the south. Dusty -passages run north and east."""), +passages run north and east.""", [sword]), 'overlook': Room("Grand Overlook", """A steep cliff appears before you, falling into the darkness. Ahead to the north, a light flickers in -the distance, but there is no way across the chasm."""), +the distance, but there is no way across the chasm.""", [helmet]), 'narrow': Room("Narrow Passage", """The narrow passage bends here from west -to north. The smell of gold permeates the air."""), +to north. The smell of gold permeates the air.""", [torch]), 'treasure': Room("Treasure Chamber", """You've found the long-lost treasure chamber! Sadly, it has already been completely emptied by -earlier adventurers. The only exit is to the south."""), +earlier adventurers. The only exit is to the south.""", [coins]), } @@ -35,6 +41,7 @@ room['narrow'].n_to = room['treasure'] room['treasure'].s_to = room['narrow'] + # # Main # @@ -69,6 +76,7 @@ def is_direction(str): print(player.current_room) current_room = player.current_room current_room = player.current_room + print(f'Items in room: {current_room.show_items()}') user_input = input('Where would you like to go? n, e, s or w?: ') if user_input == 'q': break diff --git a/src/player.py b/src/player.py index 67b321941a..9f514bff14 100644 --- a/src/player.py +++ b/src/player.py @@ -4,6 +4,7 @@ class Player: def __init__(self, name, current_room): self.name = name self.current_room = current_room + self.items = [] def move(self, direction): new_room = getattr(self.current_room, f"{direction}_to") @@ -12,6 +13,12 @@ def move(self, direction): else: print("Sorry you can't move in that direction") + def show_items(self): + if self.items.__len__() == 0: + return "you have no items" + else: + return ", ".join(list(map(lambda it: it.name, self.items))) + def __str__(self): return '{self.name} {self.room}'.format(self=self) diff --git a/src/room.py b/src/room.py index 1ad56a44c2..f33d911790 100644 --- a/src/room.py +++ b/src/room.py @@ -3,9 +3,10 @@ valid_directions = ('n','s','e','w') class Room: - def __init__(self, name, description): + def __init__(self, name, description, items): self.name = name self.description = description + self.items = items self.n_to = None self.s_to = None self.e_to = None @@ -21,3 +22,9 @@ def show_directions(self): def print_welcome(self): print(f'Welcome to {self.name}!') print(f'{self.description}') + + def show_items(self): + if self.items.__len__() == 0: + return "room has no items" + else: + return ", ".join(list(map(lambda it: it.name, self.items))) From 4dfcef20be0e95bd63eea2561991141a6874c4d1 Mon Sep 17 00:00:00 2001 From: PeteDram Date: Thu, 10 Sep 2020 20:42:33 -0700 Subject: [PATCH 4/5] added get and drop functionality --- src/adv.py | 12 ++++++++++++ src/item.py | 9 ++++++++- src/player.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/adv.py b/src/adv.py index a2995c0c41..654ea042e5 100644 --- a/src/adv.py +++ b/src/adv.py @@ -60,12 +60,22 @@ # # If the user enters "q", quit the game. +valid_commands = ('get', 'drop') + def is_direction(str): """ returns true from string if it is a valid """ return str in valid_directions +def is_command(str): + #checks if command is valid + split_str = str.split(" ") + if split_str.__len__() == 2: + return split_str[0] in valid_commands + else: + return str + print(f'Welcome {player.name}, press q at any time to quit') print(f'You are currently {player.current_room.name}') print(player.current_room.description) @@ -82,6 +92,8 @@ def is_direction(str): break elif is_direction(user_input): player.move(user_input) + elif is_command(user_input): + player.do(user_input) else: print('Sorry that is not a valid command, please try again!') diff --git a/src/item.py b/src/item.py index a1602e63bb..805ed0af4c 100644 --- a/src/item.py +++ b/src/item.py @@ -1,4 +1,11 @@ class Item: def __init__(self, name, description): self.name = name - self.description = description \ No newline at end of file + self.description = description + + def get(self): + print(f'You have picked up {self.name}') + + def drop(self): + print(f'You have dropped {self.name}') + \ No newline at end of file diff --git a/src/player.py b/src/player.py index 9f514bff14..88cdd773df 100644 --- a/src/player.py +++ b/src/player.py @@ -1,5 +1,13 @@ # Write a class to hold player information, e.g. what room they are in # currently. + +def lookup_item(string, items): + #returns item with matching name + for item in items: + if item.name == string: + return item + else: + return None class Player: def __init__(self, name, current_room): self.name = name @@ -19,6 +27,29 @@ def show_items(self): else: return ", ".join(list(map(lambda it: it.name, self.items))) + def do(self, command): + action, item_name = command.split(" ") + if action == 'get': + item = lookup_item(item_name, self.current_room.items) + if item is not None: + self.current_room.items.remove(item) + self.items.append(item) + item.get() + else: + print(f'{item.name} is not in this room') + elif action == 'drop': + item = lookup_item(item_name, self.items) + if item is not None: + self.items.remove(item) + self.current_room.items.append(item) + item.drop() + else: + print(f'You do not have {item.name}') + else: + print('that is not a valid command') + + + def __str__(self): return '{self.name} {self.room}'.format(self=self) From fced82b98c1dc497aa9e8686e55a583baa2cf5e1 Mon Sep 17 00:00:00 2001 From: PeteDram Date: Thu, 10 Sep 2020 20:47:53 -0700 Subject: [PATCH 5/5] Day 2 MVP and Stretch complete --- src/adv.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/adv.py b/src/adv.py index 654ea042e5..bdbb645dae 100644 --- a/src/adv.py +++ b/src/adv.py @@ -27,12 +27,16 @@ 'treasure': Room("Treasure Chamber", """You've found the long-lost treasure chamber! Sadly, it has already been completely emptied by earlier adventurers. The only exit is to the south.""", [coins]), + + 'pool': Room("Pool", """You've found a pool, grab a drink and chill out""",[]), } # Link rooms together room['outside'].n_to = room['foyer'] +room['outside'].e_to = room['pool'] +room['pool'].w_to = room['outside'] room['foyer'].s_to = room['outside'] room['foyer'].n_to = room['overlook'] room['foyer'].e_to = room['narrow']