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
213 changes: 208 additions & 5 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
import random
from player import Player
from room import Room
from item import Item

# Declare all the rooms

item = {
'sword': Item('sword', 'a used sword, a worn down blade that has seen many battles'),

'ruby': Item('ruby', 'a ruby that inspires greed and fortune, looks to be worth plenty of gold'),

'dung': Item('dung', 'a pile of dung, not sure why you would want to pick this up yet your curiosity beckons'),

'torch': Item('torch', 'a torch, looks to still have some life left in it')
}


def generate_item():
return random.choice(list(item.values()))


room = {
'outside': Room("Outside Cave Entrance",
"North of you, the cave mount beckons"),
"North of you, the cave mount beckons", [generate_item(), generate_item()]),

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

'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""", [generate_item()]),

'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""", [generate_item()]),

'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""", [generate_item()]),
}


Expand All @@ -39,6 +57,191 @@

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


print('welcome to adventureland 3.0!')

player_name = input("what is your name?: ")

location = room['outside']

inventory = []

player = Player(player_name, location, inventory)


print(f"you are {player_name}, the barbarian {location}")

print("what would you like to do next?")


def choose_action():
return int(input("[1] go north [2] go south [3] go east [4] go west [5] pick up item [6] open inventory [9] Quit\n"))


action = choose_action()

inventory_action = 0


def open_inventory():
print('inventory: ')
player.open_inventory()
global action
action = int(
input("[1] go north [2] go south [3] go east [4] go west [7] drop item [9] Quit\n"))


def drop(item, current_room):
for i in range(0, len(player.items)):
if item == player.items[i].item_name:
global room
room[current_room].items.append(player.items[i])


def next_step():
print(location)
print('what would you like to do next?')
global action
action = choose_action()


def wrong_way():
print('that direction is blocked, try again')
global action
action = choose_action()


while not action == 9:
if location == room['outside']:
if action == 1:
location = room['outside'].n_to
next_step()
elif action == 5:
pickup = input('what item would you like to pick up?')

player.pickup_item(room['outside'].items, pickup)
print(f'picked up {pickup}')
room['outside'].remove_item(pickup)

action = choose_action()
elif action == 6:
open_inventory()

elif action == 7:
discard = input('what item would you like to drop?')
drop(discard, 'outside')
player.drop_item(discard)
print(f'you dropped a {discard} into the room')
action = choose_action()

else:
wrong_way()

elif location == room['foyer']:

if action == 2:
location = room['foyer'].s_to
next_step()

elif action == 3:
location = room['foyer'].e_to
next_step()

elif action == 1:
location = room['foyer'].n_to
next_step()

elif action == 5:
pickup = input('what item would you like to pick up?')

player.pickup_item(room['foyer'].items, pickup)
print(f'picked up {pickup}')
room['foyer'].remove_item(pickup)
action = choose_action()

elif action == 6:
open_inventory()

elif action == 7:
discard = input('what item would you like to drop?')
player.drop_item(discard)
action = choose_action()

else:
wrong_way()

elif location == room['overlook']:
if action == 2:
location = room['overlook'].s_to
next_step()

elif action == 5:
pickup = input('what item would you like to pick up?')

player.pickup_item(room['overlook'].items, pickup)
print(f'picked up {pickup}')
room['overlook'].remove_item(pickup)

action = choose_action()

elif action == 6:
open_inventory()

elif action == 7:
discard = input('what item would you like to drop?')
player.drop_item(discard)
action = choose_action()

else:
wrong_way()

elif location == room['narrow']:
if action == 4:
location = room['narrow'].w_to
next_step()
elif action == 1:
location = room['narrow'].n_to
next_step()

elif action == 5:
pickup = input('what item would you like to pick up?')

player.pickup_item(room['narrow'].items, pickup)
print(f'picked up {pickup}')
room['narrow'].remove_item(pickup)

elif action == 6:
open_inventory()

elif action == 7:
discard = input('what item would you like to drop?')
player.drop_item(discard)
action = choose_action()

else:
wrong_way()

elif location == room['treasure']:
if action == 2:
location = room['treasure'].s_to
next_step()
elif action == 7:
discard = input('what item would you like to drop?')
player.drop_item(discard)
action = choose_action()
elif action == 5:
pickup = input('what item would you like to pick up?')

player.pickup_item(room['treasure'].items, pickup)
print(f'picked up {pickup}')
room['treasure'].remove_item(pickup)

elif action == 6:
open_inventory()
else:
wrong_way()


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

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

class Player:
def __init__(self, name, current_room, items):
self.name = name
self.current_room = current_room
self.items = items

def pickup_item(self, item_list, item):
for i in range(0, len(item_list)):
if item_list[i].item_name == item:
self.items.append(item_list[i])

def drop_item(self, item):
for i in self.items:
if i.item_name == item:
self.items.remove(i)

def open_inventory(self):
for i in self.items:
print(i)

def __str__(self):
return f"{self.name} is at {self.current_room}"
34 changes: 33 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,34 @@
# 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 = items

def add_item(self, i):
self.items.append(i)

def remove_item(self, item):
if len(self.items) > 1:
for i in range(0, len(self.items)):
if self.items[i].item_name == item:
self.items.remove(self.items[i])
break
else:
for i in range(0, len(self.items)):
if self.items[i].item_name == item:
self.items.remove(self.items[i])

def __repr__(self):
def all_items():
new_list = []
if len(self.items) > 0:
for i in range(len(self.items)):
new_list.append(self.items[i].description)
return ', and also '.join(new_list)
else:
return 'nothing of interest'
return f"you find yourself at the {self.name}, {self.description}, in it lies {all_items()}"