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
49 changes: 31 additions & 18 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
from room import Room
from room import Room, Store
from player import Player
from item import Item, LightSource
import textwrap

# Declare game-wide "constants"
char_line_limit = 90
char_line_limit = 70

# Declare global functions

wrapper = textwrap.TextWrapper(width=char_line_limit, replace_whitespace=False)
def print_wrap(foo):
if isinstance(foo, str):
print(wrapper.fill(text=foo))
if not isinstance(foo, str):
bar = str(foo)
else:
print(wrapper.fill( text=str(foo) ))
bar = foo
split = bar.split("\n")
for paragraph in split:
print(wrapper.fill( text=paragraph ))

# Declare all the rooms

rooms = {
'outside': Room("Outside Cave Entrance", "North of you, the cave mount beckons", True),
'store': Store("General Store", "A jolly fat bearded man sits behind the counter. \"Yessir, commerce happens here!\" he says.", True),
'town': Room("Town Gate", "Here is the gate to a small village south of the mountain. To the west is the general store. To the east is the tavern.", True),
'outside': Room("Outside Cave Entrance", "North of you, the cave mount beckons.", True),
'foyer': Room("Foyer", """Dim light filters in from the south. Dusty passages run north and east.""", True),
'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.""", False),
'narrow': Room("Narrow Passage", """The narrow passage bends here from west to north. The smell of gold permeates the air.""", False),
Expand All @@ -27,6 +32,10 @@ def print_wrap(foo):

# Link rooms together

rooms['store'].e_to = rooms['town']
rooms['town'].w_to = rooms['store']
rooms['town'].n_to = rooms['outside']
rooms['outside'].s_to = rooms['town']
rooms['outside'].n_to = rooms['foyer']
rooms['foyer'].s_to = rooms['outside']
rooms['foyer'].n_to = rooms['overlook']
Expand All @@ -37,15 +46,15 @@ def print_wrap(foo):
rooms['treasure'].s_to = rooms['narrow']

# Populate rooms with items
rooms['foyer'].add_item(Item("Potion", "You can drink this to boost your health."))
rooms['foyer'].add_item(Item("Coins", "These can be used to buy stuff."))
rooms['foyer'].add_item(LightSource("Lamp", "This burns oil for light."))
rooms['outside'].add_item(Item("Stick", "A very crude weapon."))
rooms['narrow'].add_item(Item("Coin", "This can be used to buy stuff."))
rooms['narrow'].add_item(Item("Coin", "This can be used to buy stuff."))
rooms['narrow'].add_item(Item("Stick", "A very crude weapon."))
rooms['narrow'].add_item(Item("Coin", "This can be used to buy stuff."))
rooms['treasure'].add_item(Item("Rope", "Good for lots of things."))
rooms['foyer'].add_item(Item("Potion", "You can drink this to boost your health.", 12))
rooms['foyer'].add_item(Item("Coins", "These can be used to buy stuff.", 10))
rooms['foyer'].add_item(LightSource("Lamp", "This burns oil for light.", 3))
rooms['outside'].add_item(Item("Stick", "A very crude weapon.", 0))
rooms['narrow'].add_item(Item("Coin", "This can be used to buy stuff.", 1))
rooms['narrow'].add_item(Item("Coin", "This can be used to buy stuff.", 1))
rooms['narrow'].add_item(Item("Stick", "A very crude weapon.", 0))
rooms['narrow'].add_item(Item("Coin", "This can be used to buy stuff.", 1))
rooms['treasure'].add_item(Item("Rope", "Good for lots of things.", 5))

#
# Main
Expand Down Expand Up @@ -106,9 +115,13 @@ def print_wrap(foo):
noun = complex_command[1]

if verb == "take" or verb == "get":
print(pc.take_item( noun.capitalize() ))
if verb == "drop":
print(pc.drop_item( noun.capitalize() ))
print(pc.try_take_item( noun.capitalize() ))
elif verb == "drop":
print(pc.try_drop_item( noun.capitalize() ))
elif verb == "sell":
print(pc.try_sell_item( noun.capitalize() ))
elif verb == "buy":
print(pc.try_buy_item( noun.capitalize() ))

else:
print("Command not understood.\n")
Expand Down
7 changes: 4 additions & 3 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

class Item:
def __init__(self, name, description):
def __init__(self, name, description, value):
self.name = name
self.description = description
self.value = value

def on_take(self):
pass
Expand All @@ -12,8 +13,8 @@ def on_drop(self):


class LightSource(Item):
def __init__(self, name, description):
super().__init__(name, description)
def __init__(self, name, description, value):
super().__init__(name, description, value)

def on_drop(self):
return "It's not wise to drop your source of light!"
96 changes: 71 additions & 25 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,102 @@
from random import randint
from item import LightSource
from room import Store

# Write a class to hold player information, e.g. what room they are in
# currently.
class Player:
def __init__(self, start_room):
self.room = start_room
self.inventory = []
self.wealth = 2

def take_item(self, item_name):
available = [x for x in self.room.items if x.name == item_name]
if not available:
return "That item is not available to take.\n"
def try_take_item(self, item_name):
if not self.can_see():
return "Good luck finding that in the dark!\n"
else:
available = [x for x in self.room.items if x.name == item_name]
if not available:
return "That item is not available to take.\n"
elif isinstance(self.room, Store):
return "Stealing isn't THAT easy.\n"
else:
return self.take_item(item_name, available[0])

def take_item(self, item_name, item):
self.inventory.append(item)
self.room.remove_item(item_name)
message = item.on_take()
if message:
print(message)
return f'{item_name} successfully taken!\n'

def try_buy_item(self, item_name):
if not self.can_see():
return "Good luck finding that in the dark!\n"
elif not isinstance(self.room, Store):
return "You can't buy stuff here!\n"
else:
self.inventory.append(available[0])
self.room.remove_item(item_name)
message = available[0].on_take()
if message:
print(message)
return f'{item_name} successfully taken!\n'

def drop_item(self, item_name):
available = [x for x in self.room.items if x.name == item_name]
if not available:
return "The store doesn't have that.\n"
else:



def try_drop_item(self, item_name):
available = [x for x in self.inventory if x.name == item_name]
if not available:
return "You don't have one of those!\n"
elif isinstance(self.room, Store):
return "You can't just leave that here, but you can try to sell it.\n"
else:
self.room.add_item(available[0])
return self.drop_item(item_name, available[0])

message = available[0].on_drop()
if message:
print(message)
def drop_item(self, item_name, item):
self.room.add_item(item)

for i in range(len(self.inventory)):
if self.inventory[i].name == item_name:
self.inventory.pop(i)
break
message = item.on_drop()
if message:
print(message)

return "Done.\n"
for i in range(len(self.inventory)):
if self.inventory[i].name == item_name:
self.inventory.pop(i)
break

return "Done.\n"

def try_sell_item(self, item_name):
available = [x for x in self.inventory if x.name == item_name]
if not available:
return "You don't have one of those!\n"
elif not isinstance(self.room, Store):
return "You can't sell that here!\n"
else:
self.boost_wealth(available[0].value)
self.drop_item(item_name, available[0])
return "Thank you for your business!\n"

def display_items(self):
if len(self.inventory) == 0:
return "Items: none\n"
return f"Wealth: {self.wealth}\nItems: none"
else:
item_names = [x.name for x in self.inventory]
str_form = ", ".join(item_names)
return f'Items: {str_form}'
return f'Wealth: {self.wealth}\nItems: {str_form}'

def can_see(self):
if self.room.is_light:
return True
# for i in self.inventory if isinstance(i, LightSource):
for i in self.inventory:
if isinstance(i, LightSource):
return True
return False


def boost_wealth(self, value):
rolled_total = 0
for i in range(self.wealth):
if randint(1, 6) > 4:
rolled_total += 1
if value > rolled_total:
self.wealth += (value - rolled_total)
8 changes: 7 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ def display_items(self):
else:
item_names = [x.name for x in self.items]
str_form = ", ".join(item_names)
return f'Items: {str_form}'
return f'Items: {str_form}'


class Store(Room):
def __init__(self, name, description, naturally_lit):
super().__init__(name, description, naturally_lit)