diff --git a/src/adv.py b/src/adv.py index c9e26b0f85..bdbb645dae 100644 --- a/src/adv.py +++ b/src/adv.py @@ -1,30 +1,42 @@ -from room import Room +from room import (Room, valid_directions) +from player import Player +from item import Item -# Declare all the rooms +#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]), + + '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'] @@ -33,11 +45,13 @@ room['narrow'].n_to = room['treasure'] room['treasure'].s_to = room['narrow'] + # # Main # # Make a new player object that is currently in the 'outside' room. +player = Player('Pete', room['outside']) # Write a loop that: # @@ -49,3 +63,41 @@ # Print an error message if the movement isn't allowed. # # 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) +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 + 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 + 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 new file mode 100644 index 0000000000..805ed0af4c --- /dev/null +++ b/src/item.py @@ -0,0 +1,11 @@ +class Item: + def __init__(self, name, description): + self.name = name + 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 d79a175029..88cdd773df 100644 --- a/src/player.py +++ b/src/player.py @@ -1,2 +1,57 @@ # 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 + self.current_room = current_room + self.items = [] + + 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 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 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) + + + diff --git a/src/room.py b/src/room.py index 24c07ad4c8..f33d911790 100644 --- a/src/room.py +++ b/src/room.py @@ -1,2 +1,30 @@ # 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, items): + self.name = name + self.description = description + self.items = items + 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}') + + 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)))