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.

57 changes: 52 additions & 5 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
from room import Room
from player import Player
from item import Item

# Declare all items:
items = {
'sword': Item('Sword', 'The Sword that cuts through fear!'),
'shield': Item('Shield', 'The shield the protects against fear!'),
'map': Item('Map', 'Helps you find your way!'),
'flashlight': Item('Flashlight', 'Portable light'),
'potion': Item('Potion', 'Restores 50 HP points.')
}

# Declare all the rooms

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

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

'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.""", [items['flashlight']]),

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

'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.""", [items['potion']]),
}


Expand Down Expand Up @@ -49,3 +60,39 @@
# Print an error message if the movement isn't allowed.
#
# If the user enters "q", quit the game.

# initial input for the REPL.

print('Welcome to the text room adventure game!!!\n')
player_name = input('Please enter a name for your player!\n')
character = Player(player_name)
user_input = 0
character.current_room = room['outside']
print(character.current_room)
character.current_room.show_inventory()
while user_input != 'q':
user_input = input('\nPlease pick one of the four direction commands [n, s, e, or w],\nuse the phrase commmand take [item] or drop [item],\nor q to quit\n')
user_input = user_input.split(' ')
if len(user_input) == 1:
user_input = user_input[0]
if user_input in ['n','s','w','e']:
character.move(user_input)
elif user_input == 'i':
character.show_inventory()
elif user_input == 'q':
print('\n~~~~~~GoodBye~~~~~~~~\n')
else:
print('\n~~~~~Invalid Command~~~~~\n')
elif len(user_input) == 2:
verb = user_input[0]
item = user_input[1]
if verb == 'take':
character.take_item(item)
elif verb == 'drop':
character.drop_item(item)
else:
print('\n~~~~~~Invalid Command~~~~~~~\n')
else:
print('\n~~~~~Invalid Command Length~~~~~~~~\n')


10 changes: 10 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Item:
def __init__(self, name, description):
self.name = name
self.description = description

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

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

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

def move(self, direction):
print(getattr(self.current_room, f'{direction}_to'))
if type(getattr(self.current_room, f'{direction}_to')) != str:
self.current_room = getattr(self.current_room, f'{direction}_to')
self.current_room.show_inventory()

def take_item(self, item):
result = self.current_room.loss_item(item)
if type(result) != str:
self.items.append(result)
else:
print(result)

def show_inventory(self):
print(f'\n{self.name}s item inventory:')
for item in self.items:
print(item)

def drop_item(self, item):
for item_index in range(len(self.items)):
if self.items[item_index].name == item:
self.current_room.reclaim_item(self.items.pop(item_index))
return None
print(f'Cannot find item {item} in character inventory.')

def __str__(self):
return f'character name: {self.name}, current_room: {self.current_room}'


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, description, items):
self.name = name
self.description = description
self.items = items
self.n_to = f'Error: There is not a room north of {self.name}'
self.s_to = f'Error: There is not a room south of {self.name}'
self.e_to = f'Error: There is not a room east of {self.name}'
self.w_to = f'Error: There is not a room west of {self.name}'

def loss_item(self, item):
for item_index in range(len(self.items)):
if self.items[item_index].name == item:
self.items[item_index].on_take()
return self.items.pop(item_index)
return f'item {item} not found in room {self.name}'

def show_inventory(self):
print(f'\nItems in room {self.name}:')
for item in self.items:
print(f'{item}')

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

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