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
54 changes: 54 additions & 0 deletions notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
player = Player("Brady", room['outside'])
current_room = player.current_room

print(current_room)

valid_directions = ["n", "s", "e", "w"]

while True:
# Wait for user input
cmd = input("-> ")
# Parse user inputs (n, s, e, w, q)
if cmd in valid_directions:
# If input is valid, move the player and loop
player.travel(cmd)
elif cmd == "q":
print("Goodbye!")
exit()
else:
print("I did not recognize that command")


# Write a class to hold player information, e.g. what room they are in
# currently.
class Player:
def __init__(self, name, starting_room):
self.name = name
self.current_room = starting_room
def travel(self, direction):
# Check if there's a valid room in the direction
if getattr(self.current_room, f"{direction}_to") is not None:
# If so, update current_room to new room and print description
self.current_room = getattr(self.current_room, f"{direction}_to")
print(self.current_room)
else:
# Else print an error message
print("Sorry! there's no room here.", "\n")

# Implement a class to hold room information. This should have name and
# description attributes.

class Room:
def __init__(self, title, description):
self.title = title
self.description = description
self.n_to = None
self.s_to = None
self.e_to = None
self.w_to = None
def __str__(self):
str = f"""
\n----------------------------------
\n{self.title}
\n {self.description}\n"""
return str
94 changes: 94 additions & 0 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from room import Room
from player import Player
from item import Item


# Declare all the rooms

Expand Down Expand Up @@ -37,8 +40,12 @@
# Main
#


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


# print(player.room.title)

# Write a loop that:
#
# * Prints the current room name
Expand All @@ -49,3 +56,90 @@
# Print an error message if the movement isn't allowed.
#
# If the user enters "q", quit the game.

player = Player("Sarah", room['outside'])

rock = Item("Rock")
pencil = Item("Pencil")


room['outside'].items.append(rock)
player.items.append(pencil)



current_room = player.current_room

print(current_room)

valid_directions = ["n", "s", "e", "w"]


while True:
# Wait for user input
cmd = input("-> ")
wordList = cmd.split(' ')
# Parse user inputs (n, s, e, w, q)
if cmd in valid_directions:
# If input is valid, move the player and loop
player.travel(cmd)
elif cmd == "i":
player.print_inventory()
elif cmd == "q":
print("Goodbye!")
exit()
elif wordList[0] == "get":
if len(current_room.items):
for item in current_room.items:
if item.name.lower() == wordList[1].lower():
player.pickup_item(item)
current_room.items.remove(item)
else:
print(f"We don't have any {wordList[1]}")
elif wordList[0] == "drop":
if len(player.items):
for item in player.items:
if item.name.lower() == wordList[1].lower():
player.drop_item(item)
current_room.items.append(item)
print(f"You have dropped {item.name}")
else:
print(f"We don't have any {wordList[1]}")
else:
print("I did not recognize that command")


# print("Please choose a room. Enter n, s, e, w, or q to quit.")

# while True:
# print(f'You are in the {player.room.title}. {player.room.description}')

# if player.room.title == "Treasure Chamber":
# print("You found the treasure!")
# break
# player_choice = input("What room do you want to visit? ")


# if player_choice == "n":
# if player.room.n_to:
# player.room = player.room.n_to
# else:
# print("Nothing that way. Choose another direction.")
# elif player_choice == "s":
# if player.room.s_to:
# player.room = player.room.s_to
# else:
# print("Nothing that way. Choose another direction.")
# elif player_choice == "e":
# if player.room.e_to:
# player.room = player.room.e_to
# else:
# print("Nothing that way. Choose another direction.")
# elif player_choice == "w":
# if player.room.w_to:
# player.room = player.room.w_to
# else:
# print("Nothing that way. Choose another direction.")
# elif player_choice == "q":
# print("Game over!")
# break
4 changes: 4 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Item:
def __init__(self, name):
self.name = name

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

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

def travel(self, direction):
# Check if there's a valid room in the direction
if getattr(self.current_room, f"{direction}_to") is not None:
# If so, update current_room to new room and print description
self.current_room = getattr(self.current_room, f"{direction}_to")
print(self.current_room)
else:
# Else print an error message
print("Sorry! there's no room here.", "\n")

def pickup_item(self, item):
if item:
self.items.append(item)
print(f"You have added {item.name}")
else:
print("Item not found. Please choose valid item.")

def drop_item(self, item):
self.items.remove(item)


def _get_item_string(self):
if len(self.items) > 0:
return "\n" + ", ".join([item for item in self.items]) + "\n"
else:
return ""

def print_inventory(self):
print(f"Your strength is: {self.strength}\n")
if len(self.items) > 0:
print("You are carrying:\n " + ", ".join([item.name for item in self.items]) + "\n")


36 changes: 35 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,36 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.

class Room:
def __init__(self, title, description):
self.title = title
self.description = description
self.n_to = None
self.s_to = None
self.e_to = None
self.w_to = None
self.items = []

def __str__(self):
str = f"""
\n----------------------------------
\n{self.title}
\n {self.description}\n
\n{self._get_item_string()}\n
\n{self._get_exits_string()}\n"""
return str

def _get_item_string(self):
return "Room contains:" + ", ".join([item.name for item in self.items])

def _get_exits_string(self):
exits = []
if self.n_to is not None:
exits.append('n')
if self.s_to is not None:
exits.append('s')
if self.e_to is not None:
exits.append('e')
if self.w_to is not None:
exits.append('w')
return "Exits:" + ", ".join(exits)