Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
dd81874
create class Room with init constructor
iVictoris May 8, 2020
5fb5afc
add str and repr methods
iVictoris May 8, 2020
08d172c
add method assigning rooms + direction
iVictoris May 8, 2020
d6efa2d
add method to get room in direction
iVictoris May 8, 2020
4a6ee8c
remove extra spacing
iVictoris May 8, 2020
406c1ba
link room together + reduce redundancy in linking
iVictoris May 8, 2020
aa90a96
write player class
iVictoris May 8, 2020
a37a5e3
format document with spacing
iVictoris May 8, 2020
afc1d5b
import player and create instance of player
iVictoris May 8, 2020
716476e
add a str method for player
iVictoris May 8, 2020
71ae393
update str method in room
iVictoris May 8, 2020
408e9bf
add get_description method
iVictoris May 8, 2020
f49534b
add method to obtain current room
iVictoris May 8, 2020
80b22e4
remove dunder from directions to keep it simple
iVictoris May 8, 2020
33ce2a1
edit enter room method by fixing logic bug
iVictoris May 8, 2020
0bdaa01
format player message
iVictoris May 8, 2020
60a8d0d
remove comments left by lambda school
iVictoris May 8, 2020
972ab4d
restrict user choices to a set of options
iVictoris May 8, 2020
cb108e1
add exit strategy for repl like command parser
iVictoris May 8, 2020
be95c6d
add while loop
iVictoris May 8, 2020
1cd9f11
add items to Room
iVictoris May 8, 2020
991054c
add logic check user input
iVictoris May 8, 2020
3755da8
split choice to check user input
iVictoris May 8, 2020
0618477
add continue statement after entering room
iVictoris May 8, 2020
87d37f6
unpack verb + item and join items
iVictoris May 8, 2020
371c875
add logic for verb checking
iVictoris May 8, 2020
6cabc27
declare var room that points to current room
iVictoris May 8, 2020
ad1b77c
check if item is in the current room
iVictoris May 8, 2020
0d5fc00
remove item from room + add item to user inventory
iVictoris May 8, 2020
c41d0d3
add scenerio where item doesn't exist in room
iVictoris May 8, 2020
759a532
create item class with str and repr methods
iVictoris May 8, 2020
e4402b6
add on_take and on_drop methods
iVictoris May 8, 2020
e5e65fc
add inventory to Player
iVictoris May 8, 2020
6ba7105
add an add item method
iVictoris May 8, 2020
5e09109
added other info about current room to str method
iVictoris May 8, 2020
a9702af
add items prop to Room
iVictoris May 8, 2020
4d722b4
add a contains method to use the in operator
iVictoris May 8, 2020
999efd6
add the remove item method
iVictoris May 8, 2020
79078dc
move comment to appropriate place
iVictoris May 8, 2020
fab249e
add logic to check verb for drop or remove
iVictoris May 8, 2020
a6bbfa9
move current room variable to higher scope
iVictoris May 8, 2020
18839ab
check if item is in the player's inventory
iVictoris May 8, 2020
8b5a79d
remove item from player and add to room
iVictoris May 8, 2020
84f2329
remove Item class outside of class implementation
iVictoris May 8, 2020
d5ecd57
add Item class to add_item method
iVictoris May 8, 2020
80d90fc
write a contains dunder method
iVictoris May 8, 2020
85a032c
add the add_item method
iVictoris May 8, 2020
f02bfcf
add the drop item method to player
iVictoris May 8, 2020
84f2aa0
update the list of valid choices
iVictoris May 8, 2020
19b43b1
format message to show clear separation of prompt
iVictoris May 8, 2020
7ccd8ca
update logic in if statement
iVictoris May 8, 2020
1dff595
rejoin choice to be a single string
iVictoris May 8, 2020
b04f6ec
add logic for inventory choice
iVictoris May 8, 2020
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
118 changes: 90 additions & 28 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,106 @@
from room import Room
from player import Player

# Declare all the rooms
def main():

room = {
'outside': Room("Outside Cave Entrance",
"North of you, the cave mount beckons"),
room = {
'outside': Room("Outside Cave Entrance", "North of you, the cave mount beckons", ['Common Staff', 'Common Sword']),
'foyer': Room("Foyer", """Dim light filters in from the south. Dusty
passages run north and east.""", ['Rare Staff', 'Rare Sword', 'Rare Armor']),

'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.""", ['Rare Gloves', 'Epic Sword', 'Epic Staff', 'Epic Bow', 'Epic Glove']),

'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."""),
}
room['outside'].assign_room('s', room['foyer'])
room['foyer'].assign_room('s', room['overlook'])
room['foyer'].assign_room('w', room['narrow'])
room['overlook'].assign_room('n', room['foyer'])
room['narrow'].assign_room('s', room['treasure'])
valid_choices = list('newsqi') + ['inventory']

# Make a new player object that is currently in the 'outside' room.
player = Player('Me', room['outside'])

# Link rooms together
# Player's __str__ method prints its name and room location
while True:
print('\n------------------------------------------------- \n')
print(player)

# get room will return the room the player is currently in
print('\n' + player.get_room().get_description())

choice = input('Please choose what direction you would like to go or enter a take/drop command: ').split(' ')

# only if len(choice) < 1 or take or drop not inside choice
if (len(choice) < 1 or (not len(choice) == 1 and 'take' not in choice and 'drop' not in choice)):
continue

elif (len(choice) == 1):
choice = ''.join(choice)
if (choice not in valid_choices):
print(f'Choices can only be {", ".join(valid_choices).rstrip() }.')
continue

if (choice == 'q'):
return

if (choice == 'i' or choice == 'inventory'):
print(f"""\n\t\tPlayer Inventory\n-------------------------------------------------
{', '.join([ str(item) for item in player.inventory])}
""")
continue
try:
player.enter_room(choice)
continue
except ValueError:
print('Unfortunately, that path does not exist, please try again')
continue

# here the choices > 1
current_room = player.get_room()
verb, *item = choice
item = ' '.join(item)
# if user enters get/take
if verb == 'get' or verb == 'take':

# check current room for item name
# if item in room
if (item in current_room):

# remove item from room and add to player's inventory
current_room.remove_item(item)
player.add_item(item)
continue

# if not in the room
# print error -> continue
print(f"{item} does not exist in room. Please try again")
continue

# check verb to be remove or drop
if verb == 'remove' or verb == 'drop':

# now we want to check if the item is in the player's inventory
if item in player:

# remove item from player and drop into room if it is
player.drop_item(item)
current_room.add_item(item)

room['outside'].n_to = room['foyer']
room['foyer'].s_to = room['outside']
room['foyer'].n_to = room['overlook']
room['foyer'].e_to = room['narrow']
room['overlook'].s_to = room['foyer']
room['narrow'].w_to = room['foyer']
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.

if __name__ == '__main__':
main()
# Write a loop that:
#
# * Prints the current room name
Expand Down
16 changes: 16 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Item:
def __init__(self, name):
self.__name = name

def __str__(self):
return self.__name

def __repr__(self):
return f"Item({self.__name})"

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

def on_drop(self):
print(f'You have dropped {self.__name}')

48 changes: 48 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,50 @@
from item import Item
# Write a class to hold player information, e.g. what room they are in
# currently.

class Player:
def __init__(self, name, room, inventory=[]):
self.__name = name
self.__current_room = room
self.inventory = inventory

def __str__(self):
room = self.__current_room
return f'''Player: {self.__name}, currently in room: {self.__current_room}

\t\t\t Map Overview:

\t\t\t\t{room.n}
\t{room.w}\t\t{room}\t\t\t{room.e}
\t\t\t\t{room.s}

Other info:
Items: {', '.join([str(item) for item in room.items])}'''

def get_room(self):
return self.__current_room

def enter_room(self, direction):
next_room = getattr(self.__current_room, direction)
if not next_room:
raise ValueError()
self.__current_room = next_room

def add_item(self, item):
item = Item(item)
self.inventory.append(item)
item.on_take()

def drop_item(self, item):
item_location = 0
for index in range(len(self.inventory)):
if str(self.inventory[index]) == item:
item_location = index
break

self.inventory = self.inventory[0:item_location] + self.inventory[item_location+1:]

def __contains__(self, item):
for i in self.inventory:
if str(i) == item:
return True
54 changes: 53 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,54 @@
from item import Item
# 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 = [Item(item) for item in items]
self.n = None
self.e = None
self.w = None
self.s = None

def __str__(self):
return f"{self.__name}"

def __repr__(self):
return f"Room({self.__name})"

def assign_room(self, direction, room):
opposite_direction = {
's': 'n',
'n': 's',
'w': 'e',
'e': 'w'
}
setattr(self, direction, room)
setattr(room, opposite_direction[direction], self)

def get_room(self, direction):
return self[direction]

def get_description(self):
return self.__description

def __contains__(self, item):
for i in self.items:
if str(i) == item:
return True

def remove_item(self, item):
# slice up to index of item name
# concat the index after item
item_location = 0
for index in range(len(self.items)):
if str(self.items[index]) == item:
item_location = index
break

self.items = self.items[0:item_location] + self.items[item_location+1:]

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