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
81 changes: 76 additions & 5 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
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"),
"North of you, the cave mount beckons",Item('bow')),

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

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

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

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


Expand All @@ -39,6 +41,10 @@

# Make a new player object that is currently in the 'outside' room.

player_name = input("What is your name: ")
player_room = room["outside"]
player = Player(player_name, player_room)
print(player)
# Write a loop that:
#
# * Prints the current room name
Expand All @@ -49,3 +55,68 @@
# Print an error message if the movement isn't allowed.
#
# If the user enters "q", quit the game.
while True:
current_room = player.current_room
player_inventory = [x.name for x in player.inventory] if len(player.inventory) else "Empty Bag"
room_inventory = [ x.name for x in current_room.item] if len( current_room.item) else "Empty Room"



print(f'You are located at {current_room.name} and {current_room.description}')
print(f"You have found a {room_inventory}\n")
print(f"If you would like to take an item, type: take item_name\n")
print(f"If you would like to drop an item, type: drop item_name\n")

print(f'{player_name} has {len(player.inventory)} item(s) in there bag\n')
print(f'Bag: {player.inventory}')
print("Choose next path.\n")

enter = input('Enter n, s , e, w and q to quit the game: ')

action = enter[0:4]
item = enter[5: ]

if enter == 'q':
print(f'Thanks for playing, {player_name}. Till next time!')
if enter == 'n':
if hasattr(current_room, 'n_to'):
print("going North")
player.current_room = current_room.n_to
else:
print(f'{player_name} enter another location, you hit a invisible wall ')
elif enter == 's':
if hasattr(current_room, 's_to'):
print(f'{player_name} you are going South my friend')
player.current_room = current_room.s_to
else:
print(f'{player_name} enter another location, you hit a invisible wall ')
elif enter == 'e':
if hasattr(current_room, 'e_to'):
print(f'{player_name} you are moving East!')
player.current_room = current_room.e_to
else:
print(f'{player_name} enter another location, you hit a invisible wall ')
elif enter == 'w':
if hasattr(current_room, 'w_to'):
print(f'{player_name} going out West')
player.current_room = current_room.w_to
print(f'{player_name} enter another location, you hit a invisible wall ')

elif action == "drop":
item_name_list = [i.name for i in player_inventory]
item_index = item_name_list.index(item)
current_room.add_item(player.inventory[item_index])
player.drop(item_index)

elif action == "take" :
item_name_list = [i.name for i in current_room.item]
item_index = item_name_list.index(item)
player.take(current_room.item[item_index])
current_room.delete_item(item_index)


else:
print('Please enter a valid location')
break


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



class Player:
def __init__(self, name, current_room):
self.name = name
self.current_room = current_room
self.inventory = []
def __str__(self):
return f'{self.name} {self.current_room}'

def take(self, item):
self.inventory.append(item)

def drop(self, item):
del self.inventory[item]
18 changes: 17 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.
from item import Item

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

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

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

def delete_item(self, item):
del self.item[item]