Skip to content

MVP 1 complete #497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 59 additions & 7 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -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']
Expand All @@ -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:
#
Expand All @@ -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!')

11 changes: 11 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -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}')

55 changes: 55 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -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)



30 changes: 29 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# 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)))