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
99 changes: 53 additions & 46 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from room import Room
from player import Player
from item import Item
# Declare all the rooms

# Declare all the rooms
room = {
'outside': Room("Outside Cave Entrance",
"North of you, the cave mount beckons", ['coffee']),
Expand All @@ -28,7 +28,6 @@
}

# Link rooms together

room['outside'].n_to = room['foyer']
room['foyer'].s_to = room['outside']
room['foyer'].n_to = room['overlook']
Expand All @@ -42,93 +41,101 @@
# Main
#

# Make a new player object that is currently in the 'outside' room.
# room names
names = ['outside', 'foyer', 'overlook', 'narrow', 'treasure']

# intro message
print("\nWelcome. May the odds forever be in your favour. \n")

player = Player(room['outside'])
print(player)
player_name = input("\n> What is your name?\n\n")

# make a new player object that is currently in the 'outside' room.
player = Player(room['outside'], [], player_name)
print("\n", player)

def grab_item(item):

# get the inventory from a room
def get_room_inventory(current_room):
try:
player.inventory.index(item)
player.inventory.remove(item)
player.inventory.append(item)
location = current_room.split(' ')[0].lower()
if location in names:
return room[location].inventory
elif current_room.split(' ')[1].lower() in names:
location = current_room.split(' ')[1].lower()
return room[location].inventory
except:
print(item, "is not here!")
print(current_room, "does not exist!")


# take the item from the room and add it to player inventory
def take_from_room(item, current_room):
try:
# room.inventory.remove(item)
# print("From take_from_room:", room.inventory)
print("Current room ==>", current_room.split(' ')[0])
location = current_room.split(' ')[0].lower()
print("Item --", item)
# print("Answer..", room.current_room.split(' ')[0])
print("Answer..", room[location].inventory)
print("Answer..", room[location].inventory.remove(item))
print("Answer..", room[location].inventory)
room[location].inventory.remove(item)
player.inventory.append(item)

except:
print(item, "is not here!")


# drop the item from player inventory into the room
def drop_item(item, current_room):
try:
location = current_room.split(' ')[0].lower()
player.inventory.remove(item)
room[location].inventory.append(item)
except:
print(item, "is not here - nothing to drop!")


# move the player into a different room
def try_direction(direction, current_room):
attribute = direction + "_to"

if hasattr(current_room, attribute):
return getattr(current_room, attribute)


# Write a loop that:
#
while True:
# * Prints the current room name
print(player.current_room.name)
# * Prints the current description (the textwrap module might be useful here).
print(player.current_room.description)
# Prints items
print(player.current_room.inventory)
# * Waits for user input and decides what to do.
# s = input("\n>").lower()[0]
s = input("\n>").lower().split(' ')
print("First s =", len(s))
s = input(
"\n>Make your move. Choose North, South, East or West or get / drop items.\n\n").lower().split(' ')
if len(s) == 1:
# grab the first character of the first word
s = s[0][0]
print("Second s =", s)
if s == "q":
print("See you next time!")
break

player.current_room = try_direction(s, player.current_room)
elif s == "i":
print("Player inventory:", player.inventory)

else:
player.current_room = try_direction(s, player.current_room)
if player.current_room is not None:
print(player.current_room)
room_inventory = get_room_inventory(player.current_room.name)
print("Inventory in the room:", room_inventory)
else:
print("\n\n--------Muerto--------\n\n")
break

elif len(s) == 2:
first_word = s[0]
print("First word=", first_word)
second_word = s[1]
if (first_word == "get" or first_word == "take"):
print("Yeahhh", first_word)
print("Second word =>", second_word)
print("Inventory", player.current_room.inventory)
if (second_word in player.current_room.inventory):
print("It's in there!!")
take_from_room(second_word, player.current_room.name)
Item.on_take(second_word)
else:
print("Nope, not here!")
print(second_word, "is not here!")
elif (first_word == 'drop'):
drop_item(second_word, player.current_room.name)
Item.on_drop(second_word)
else:
print("You must get or take!")
print("You must get, take or drop!")
continue
# if first_word in ['get', 'drop']:

else:
print("I don't understand that.")
continue

#
# 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.
6 changes: 6 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ class Item:
def __init__(self, name, description):
self.name = name
self.description = description

def on_take(self):
print("That's right!! I've taken", self)

def on_drop(self):
print("Yup! I've dropped", self)
8 changes: 4 additions & 4 deletions src/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@


class Player:
def __init__(self, current_room):
def __init__(self, current_room, inventory, player_name):
self.current_room = current_room
# self.inventory = inventory
# self.location = location
self.inventory = inventory
self.player_name = player_name

def __repr__(self):
return f"Player is in {self.current_room}"
return f"{self.player_name} is in {self.current_room}"
5 changes: 0 additions & 5 deletions src/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ def __init__(self, name, description, inventory):
self.name = name
self.description = description
self.inventory = inventory
# class Room(Item):
# def __init__(self, room_name, room_description, item_name, item_description):
# super().__init__(room_name, room_description)
# self.room_name = room_name
# self.room_description = room_description

def __repr__(self):
return f"{self.name}, {self.description}"