Skip to content
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
43 changes: 43 additions & 0 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from room import Room
from player import Player
from item import Item

# Declare all the rooms

Expand All @@ -21,6 +23,22 @@
earlier adventurers. The only exit is to the south."""),
}

commands = {}

for k, v in {
('n', 'north'): 'n_to',
('s', 'south'): 's_to',
('e', 'east'): 'e_to',
('w', 'west'): 'w_to',
('q', 'quit', 'exit', 'leave'): 'q',
('get', 'pickup', 'take'): 'get',
('drop', 'leave'): 'drop',
('inventory', 'bag', 'items', 'inv', 'i'): 'inv',
('go', 'move', 'travel'): 'go'
}.items():
for key in k:
commands[key] = v


# Link rooms together

Expand All @@ -33,11 +51,18 @@
room['narrow'].n_to = room['treasure']
room['treasure'].s_to = room['narrow']

room['outside'].__items__ = [
Item('Candy', "A tasty sweet"),
Item('key', 'opens the special lock')
]

#
# Main
#

# Make a new player object that is currently in the 'outside' room.
name = input("Greetings Adventurer! What is your name?: ")
new_player = Player(name, room['outside'])

# Write a loop that:
#
Expand All @@ -49,3 +74,21 @@
# Print an error message if the movement isn't allowed.
#
# If the user enters "q", quit the game.
selection = ""
print(new_player)
while(selection != 'q'):
selection = str.lower(input("What do you do?:: "))
selection = selection.split()
command = commands.get(selection[0], "")
if command == 'go':
command = commands.get(selection[1], "")
if command == 'q':
print("Thank you for playing!")
elif command == 'inv':
print(new_player.viewInventory())
elif command == 'get':
print(new_player.get(selection[1]))
elif command == 'drop':
print(new_player.drop(selection[1]))
else:
print(new_player.move(command))
7 changes: 7 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Item:
def __init__(self, name, description):
self.name = name
self.description = description

def __str__(self):
return self.name + "\n" + self.description
45 changes: 45 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,47 @@
# Write a class to hold player information, e.g. what room they are in
# currently.

class Player:
def __init__(self, name, start):
self.name = name
self.room = start
self.items = []

def __str__(self):
return self.room.__str__()

def move(self, dir):
if hasattr(self.room, dir):
self.room = getattr(self.room, dir)
return self.room
else:
return "I can't do that"

def get(self, item):
recieved = self.room.removeItem(item)
if recieved:
self.items.append(recieved)
return f"You successfully picked up {recieved.name}"
else:
return f"{item} is not in this room"

def drop(self, name):
new_items = []
for i, item in enumerate(self.items):
if not str.lower(item.name) == name:
new_items.append(item)
else:
new_items = new_items + self.items[i+1:]
self.items = new_items
self.room.addItem(item)
return f"You have dropped {item.name} in {self.room.name}\n\n{self.room}"
self.items = new_items
return f"You do not possess {name}\n\n{self.room}"

def viewInventory(self):
if not len(self.items):
return "You sadly lack any items in your bag"
output = "In you bag, you have: \n"
for item in self.items:
output += "" + item.__str__() + '\n'
return output
32 changes: 31 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.

class Room:
def __init__(self, name, dscr):
self.name = name
self.dscr = dscr
self.__items__ = []

def __str__(self):
output = self.name + "\n" + self.dscr
if len(self.__items__):
output += "\nIn this room, you can see: "
for item in self.__items__:
output += item.name + ", "
output = output[:-2]
return output

def removeItem(self, name):
new_items = []
for i, item in enumerate(self.__items__):
if not str.lower(item.name) == name:
new_items.append(item)
else:
new_items = new_items + self.__items__[i+1:]
self.__items__ = new_items
return item
self.__items__ = new_items
return None

def addItem(self, item):
self.__items__.append(item)