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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode
*.pyc
__pycache__
__pycache__
.idea
59 changes: 57 additions & 2 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from room import Room

from player import Player
from item import Item
import random
import textwrap
import pdb
# Declare all the rooms

room = {
Expand All @@ -21,6 +25,8 @@
earlier adventurers. The only exit is to the south."""),
}

rooms = list(room.keys())


# Link rooms together

Expand All @@ -38,7 +44,6 @@
#

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

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

def print_error():
print("\n \n Illegal Move! Room doesn't exist. Try again. \n \n")

items = [Item("sword"), Item("axe"), Item("coins"), Item("ladder"), Item("emerald"), Item("ruby"), Item("bow"), Item("book")]
player_items = random.sample(items, 2)
p = Player(room['outside'], player_items)
items = list(set(items).difference(set(player_items)))


while items:
item = random.choice(items)
random_room = room[random.choice(rooms)]
items.remove(item)
random_room.add(item)



while True:
print(p.current_room)
user_input = input("What direction do you want to go? ")

if len(user_input.split(' ')) > 1:
user_input_list = user_input.split(' ')
command = user_input_list[0]
item = user_input_list[1].lower()
if command == "take" or command == "get":
p.take(item)
elif command == "drop":
p.drop(item)

user_input = user_input.lower()[0]
direction = f'{user_input}_to'
if user_input == 'q':
break
elif user_input == 'n' and hasattr(p.current_room, direction):
p.current_room = p.current_room.n_to

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MUCH clearer than before!

elif user_input == 's' and hasattr(p.current_room, direction):
p.current_room = p.current_room.s_to
elif user_input == 'e' and hasattr(p.current_room, direction):
p.current_room = p.current_room.e_to
elif user_input == 'w' and hasattr(p.current_room, direction):
p.current_room = p.current_room.w_to
elif user_input == 'i' or user_input == 'inventory':
if p.items: item_names = [p.item.name for p.item in p.items]
if p.items: print(f" \n {', '.join(item_names)} \n")
elif user_input == 't' or user_input == 'g' or user_input == 'd':
pass
else:
print_error()
12 changes: 12 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Item:
def __init__(self, name, description=""):
self.name = name
self.description = description


def on_take(self):
print(f'{self.name} Taken.')


def on_drop(self):
print(f'{self.name} Dropped.')
31 changes: 31 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,33 @@
# Write a class to hold player information, e.g. what room they are in
# currently.
class Player:

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


def print_items(self):
print(self.items)


def drop(self, item_name):
try:
item = [self.item for self.item in self.items if item_name == self.item.name][0]
except:
print(f"Player does not have {item_name}")
return
self.items.remove(item)
item.on_drop()
self.current_room.add(item)


def take(self, item_name):
try:
item = [self.current_room.item for self.current_room.item in self.current_room.items if item_name == self.current_room.item.name][0]
except:
print(f"Item {item_name} does not exist in room")
return
self.current_room.remove(item)
self.items.append(item)
item.on_take()
24 changes: 23 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.

class Room:

def __init__(self, name, description):
self.name = name
self.description = description
self.items = []

def __str__(self):
item_names = [item.name for item in self.items]
items_list_str = ', '.join(item_names) if self.items else ""
return f"{self.name}, {self.description} \n{items_list_str}"

def add(self, item):
self.items.append(item)


def remove(self, item):
if item in self.items:
self.items.remove(item)
else:
print(f"Item does not exist in {self.name}")