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
20 changes: 20 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 68 additions & 21 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import code

from room import Room
from player import Player
from item import Item

# Declare all the rooms

room = {
'outside': Room("Outside Cave Entrance",
"North of you, the cave mount beckons"),

'foyer': Room("Foyer", """Dim light filters in from the south. Dusty
passages run north and east."""),
'foyer': Room("Foyer",
"Dim light filters in from the south. Dusty passages run north and east."),

'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."""),
'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."),

'narrow': Room("Narrow Passage", """The narrow passage bends here from west
to north. The smell of gold permeates the air."""),
'narrow': Room("Narrow Passage",
"The narrow passage bends here from west to north. The smell of gold permeates the air."),

'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."""),
'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."),
}


Expand All @@ -33,19 +35,64 @@
room['narrow'].n_to = room['treasure']
room['treasure'].s_to = room['narrow']

# Add room items
room['outside'].add_item(Item("Sword", "A pointy object"))
room['foyer'].add_item(Item("Egg", "Don't drop it!"))
room['overlook'].add_item(Item("Phone", "Find the password, find the help"))
room['narrow'].add_item(Item("Water", "Hydration is a necessary evil"))
room['treasure'].add_item(Item("Key", "What could this be for?"))

#
# Main
#

# Make a new player object that is currently in the 'outside' room.
directions = ['n', 's', 'e', 'w']
item_actions = ['get', 'take', 'drop']

# Write a loop that:
#
# * Prints the current room name
# * Prints the current description (the textwrap module might be useful here).
# * Waits for user input and decides what to do.
#
# If the user enters a cardinal direction, attempt to move to the room there.
# Print an error message if the movement isn't allowed.
#
# If the user enters "q", quit the game.
p = Player("Amanda", room['outside'])

print(f'Welcome {p.name}!\nExplore the map by moving North(n), South(s), East(e), or West(w)\nTo exit the game, enter q\n')
print(f'You are in the {p.current_room.name} - {p.current_room.description}\n')
p.current_room.print_items()

while True:
selection = input('Where to? ').lower().split(' ')

if len(selection) > 2 or len(selection) < 1:
print("Please enter a one or two word input for the game. To get a list of valid commands, type 'help' or 'h")
elif len(selection) == 2:
if selection[0] in item_actions:
if selection[0] == 'get' or selection[0] == 'take':
item = p.current_room.search_items(selection[1])
p.current_room.drop_item(item)
p.add_item(item)
item.on_take(item)
elif selection[0] == 'drop':
item = p.search_items(selection[1])
p.current_room.add_item(item)
p.drop_item(item)
item.on_drop(item)
else:
print("Please enter a valid action for the item. To get a list of valid commands, type 'help' or 'h")
else:
if selection[0] == 'q' or selection[0] == 'quit':
print(f'Thanks for playing {p.name}!')
break

if selection[0] == 'h' or selection[0] == 'help':
print("Valid game commands:\n'n' - Move North\n's' - Move South\n'e' - Move East\n'w' - Move West\n'i' or 'inventory' - Get a list of your current items\n'get' or 'take' - Pick up an item\n'drop' - Drop an item\n'q' or 'quit' - Exit Game\n")
continue

if selection[0] == 'i' or selection[0] == 'inventory':
p.print_items()
continue

if selection[0] in directions:
try:
p.move_room(selection[0])
print(f'You are in the {p.current_room.name} - {p.current_room.description}\n')
p.current_room.print_items()
except AttributeError:
print('No room there, try another direction')
else:
print('Movement not allowed! Please enter a direction (n, s, e, w) to move around the map')
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):
super().__init__()
self.name = name
self.description = description

def on_take(self, item):
print(f'You have successfully picked up {self.name}')

def on_drop(self, item):
print(f'You have dropped {self.name}')
39 changes: 37 additions & 2 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,37 @@
# Write a class to hold player information, e.g. what room they are in
# currently.
from room import Room
from item import Item

class Player:
def __init__(self, name, current_room):
super().__init__()
self.name = name
self.current_room = current_room
self.items = []

def __str__(self):
return f'Name: {self.name}, Current Room: {self.current_room}'

def move_room(self, direction):
if getattr(self.current_room, f'{direction}_to'):
self.current_room = getattr(self.current_room, f'{direction}_to')

def print_items(self):
if len(self.items) > 0:
print('Your current items:\n')
for i in self.items:
print(f'{i.name} - {i.description}')
else:
print('You have no items - explore the map to find some and add them to your collection!')

def search_items(self, item):
for i in self.items:
if i.name.lower() == item:
return i
else:
return None

def add_item(self, item):
self.items.append(item)

def drop_item(self, item):
self.items.remove(item)
35 changes: 33 additions & 2 deletions src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,33 @@
# Implement a class to hold room information. This should have name and
# description attributes.
from item import Item

class Room:
def __init__(self, name, description):
super().__init__()
self.name = name
self.description = description
self.items = []

def __str__(self):
return f'{self.name} - {self.description}'

def print_items(self):
if len(self.items) > 0:
print('It has the following items:\n')
for i in self.items:
print(f'{i.name} - {i.description}')
else:
print('There are no items in this room')

def search_items(self, item):
for i in self.items:
if i.name.lower() == item:
return i
else:
print('Item does not exist in this room.')
self.print_items()

def add_item(self, item):
self.items.append(item)

def drop_item(self, item):
self.items.remove(item)