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
142 changes: 95 additions & 47 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
from item import Item
from room import Room
from player import Player
import time


# Declare all the rooms
room = {
'outside': Room("Outside Cave Entrance",
"North of you, the cave mount beckons"),
# Declare all expected commands
# - Expected Basic Command List:
basic_movement = ['n', 'north', 's', 'south', 'e', 'east', 'w', 'west']
basic_inventory = ['i', 'items', 'inventory', 'b', 'bag']
basic_search = ['find', 'look', 'search']
exit_commands = ['q', 'quit', 'exit']
# - Expected Verbose Command List:
movement_verbs = ['move', 'go']
movement_directions = ['n', 'north', 's', 'south', 'e', 'east', 'w', 'west']
inventory_verbs = ['check', 'open']
inventory_objects = ['inventory', 'bag']
search_verbs = ['search']
search_objects = ['room', 'items']
pickup_verbs = ['get', 'take', 'pickup'] # Followed by "item name"
drop_verbs = ['drop', 'leave'] # Followed by "item name"
inspection_verbs = ['inspect'] # Followed by "item name"

'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."""),
# Declare all of the items
item = {
'pez dispenser': Item("Pez Dispenser", "Never leave home without it!"),
'rusty sword': Item("Rusty Sword", "Looks like it might break if you swing it..."),
'moldy shield': Item("Moldy Shield", "There's so much mold you can't see what it is made of!"),
}

'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."""),
# Declare all the rooms
room = {
'outside': Room("Outside Cave Entrance", "North of you, the cave mount beckons", [item['rusty sword']]),
'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.", [item['moldy shield']]),
'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."),
}


Expand All @@ -36,10 +52,10 @@


# Main Loop
player = Player(room['outside'])
player = Player(room['outside'], [item['pez dispenser']])

user_input = ['']
while not user_input[0].lower() in ['q', 'quit', 'exit']:
while not user_input[0].lower() in exit_commands:

# Location Callout and Entry Prompt
time.sleep(0.5)
Expand All @@ -50,36 +66,68 @@
user_input = ['']
time.sleep(0.5)

# Player Movement
if user_input[0].lower() in ['move', 'go', 'n', 'north', 's', 'south', 'e', 'east', 'w', 'west']:
if(user_input[0].lower() in ['n', 'north']) or (len(user_input) > 1 and ((user_input[0].lower() in ['move', 'go']) and (user_input[1].lower() in ['n', 'north']))):
new_location = player.get_location().to_n()
error = player.set_location(new_location)
if error:
print(error)
elif (user_input[0].lower() in ['s', 'south']) or (len(user_input) > 1 and ((user_input[0].lower() in ['move', 'go']) and (user_input[1].lower() in ['s', 'south']))):
new_location = player.get_location().to_s()
error = player.set_location(new_location)
if error:
print(error)
elif (user_input[0].lower() in ['e', 'east']) or (len(user_input) > 1 and ((user_input[0].lower() in ['move', 'go']) and (user_input[1].lower() in ['e', 'east']))):
new_location = player.get_location().to_e()
error = player.set_location(new_location)
if error:
print(error)
elif (user_input[0].lower() in ['w', 'west']) or (len(user_input) > 1 and ((user_input[0].lower() in ['move', 'go']) and (user_input[1].lower() in ['w', 'west']))):
new_location = player.get_location().to_w()
error = player.set_location(new_location)
if error:
print(error)
# Simple Commands:
if len(user_input) == 1:
if user_input[0].lower() in basic_movement:
player.change_location(user_input[0].lower())
elif user_input[0].lower() in basic_inventory:
player.check_items()
elif user_input[0].lower() in basic_search:
player.get_location().check_items()
elif user_input[0].lower() in exit_commands:
print('> Now exiting the program. Thank you for your time.')
else:
print('> Please enter a valid command to proceed')

# Exit Condition
elif user_input[0].lower() in ['q', 'quit', 'exit']:
print('> Now exiting the program. Thank you for your time.')
print('> Please enter a valid command to proceed.')

# Invalid Command
else:
print('> Please enter a valid command to proceed.')
user_input = ['']
# Verbose Commands:
elif len(user_input) > 1:
if user_input[0].lower() in movement_verbs:
if user_input[1].lower() in movement_directions:
player.change_location(user_input[1].lower())
else:
print('> Please enter a valid command to proceed.')
elif user_input[0].lower() in inventory_verbs:
if user_input[1].lower() in inventory_objects:
player.check_items()
else:
print('> Please enter a valid command to proceed.')
elif user_input[0].lower() in search_verbs:
if user_input[1].lower() in search_objects:
player.get_location().check_items()
else:
print('> Please enter a valid command to proceed.')
elif user_input[0].lower() in pickup_verbs:
if len(user_input) > 1:
item_name = " ".join(user_input[1:])
item = player.get_location().get_item(item_name)
if item is None:
print(f'> Unable to locate {item_name}.')
else:
player.give_item(item)
else:
print('> You must specify which item you which to interact with.')
elif user_input[0].lower() in drop_verbs:
if len(user_input) > 1:
item_name = " ".join(user_input[1:])
item = player.get_item(item_name)
if item is None:
print(f'> Unable to drop {item_name}')
else:
player.get_location().give_item(item)
else:
print('> You must specify which item you which to interact with.')
elif user_input[0].lower() in inspection_verbs:
if len(user_input) > 1:
item_name = " ".join(user_input[1:])
retrieved_item = player.check_for_item(item_name)
if retrieved_item is None:
retrieved_item = player.get_location().check_for_item(item_name)
if retrieved_item is None:
print(f'> Unable to inspect {item_name}.')
else:
print(retrieved_item)
else:
print('> You must specify which item you which to interact with.')
else:
print('> Please enter a valid command to proceed.')
user_input = ['']
15 changes: 15 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Item:
def __init__(self, name, description):
self.name = "_".join(name.lower().split())
self.description = description

def __str__(self):
return (f"""You are currently staring at a {self.name}\n{self.description}""")

def on_take(self):
print(
f"""You have picked up {' '.join(self.name.split('_')).capitalize()}""")

def on_drop(self):
print(
f"""You have dropped {' '.join(self.name.split("_")).capitalize()}""")
65 changes: 64 additions & 1 deletion src/player.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# CARDINAL DIRECTIONS:
north = ['n', 'north']
south = ['s', 'south']
east = ['e', 'east']
west = ['w', 'west']


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

def get_location(self):
return self.current_room
Expand All @@ -10,3 +18,58 @@ def set_location(self, room):
return '> You are unable to proceed in that direction.'
else:
self.current_room = room

def change_location(self, direction):
if (direction in north):
new_location = self.get_location().to_n()
error = self.set_location(new_location)
if error:
print(error)
elif (direction in south):
new_location = self.get_location().to_s()
error = self.set_location(new_location)
if error:
print(error)
elif (direction in east):
new_location = self.get_location().to_e()
error = self.set_location(new_location)
if error:
print(error)
elif (direction in west):
new_location = self.get_location().to_w()
error = self.set_location(new_location)
if error:
print(error)

def check_items(self):
item_names = []
for item in self.items:
item_names.append(" ".join(item.name.split("_")).capitalize())
if len(item_names) > 0:
print('> Your inventory currently holds the following items:')
for item in item_names:
print(f'> - {item}')
else:
print('> Your bag is currently empty.')

def check_for_item(self, item_name):
for item in self.items:
if "_".join(item_name.split()) == item.name:
return item
return None

def get_item(self, item_name):
for index, item in enumerate(self.items):
if "_".join(item_name.split()) == item.name:
retrieved_item = item
self.items.pop(index)
retrieved_item.on_drop()
return retrieved_item
return None

def give_item(self, new_item):
if new_item is not None:
self.items.append(new_item)
new_item.on_take()
else:
return '> I cannot take that which is not available.'
34 changes: 33 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
class Room:
def __init__(self, name, description):
def __init__(self, name, description, items=[]):
self.name = name
self.description = description
self.n_to = None
self.s_to = None
self.e_to = None
self.w_to = None
self.items = items

def __str__(self):
return (f"""You are currently in the {self.name}\n{self.description}""")
Expand All @@ -21,3 +22,34 @@ def to_e(self):

def to_w(self):
return self.w_to

def check_items(self):
item_names = []
for item in self.items:
item_names.append(" ".join(item.name.split("_")).capitalize())
if len(item_names) > 0:
print('> As you search around the room you see the following items.')
for item in item_names:
print(f'> - {item}')
else:
print('> There are no items in this room.')

def check_for_item(self, item_name):
for item in self.items:
if "_".join(item_name.split()) == item.name:
return item
return None

def get_item(self, item_name):
for index, item in enumerate(self.items):
if "_".join(item_name.split()) == item.name:
retrieved_item = item
self.items.pop(index)
return retrieved_item
return None

def give_item(self, new_item):
if new_item is not None:
self.items.append(new_item)
else:
return '> I cannot drop that which I do not have...'