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
98 changes: 88 additions & 10 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
from room import Room
from player import Player
from item import Item
import textwrap

# Declare all the rooms

# knife = Item('knife', 'small knife')
# map = Item('map', 'map for navigation to room')
# sword = Item('sword', ' long sward for battle')
# bike = Item('bike', 'bike for fast navigation')
# shoe = Item('shoe', 'magic shoe')

room = {
'outside': Room("Outside Cave Entrance",
"North of you, the cave mount beckons"),
'outside': Room(name="Outside Cave Entrance",
description="North of you, the cave mount beckons", items=['knife1', 'map1']),

'foyer': Room("Foyer", """Dim light filters in from the south. Dusty
passages run north and east."""),
'foyer': Room(name="Foyer", description="""Dim light filters in from the south. Dusty
passages run north and east.""", items=['sword1', 'magic_shoe1']),

'overlook': Room("Grand Overlook", """A steep cliff appears before you, falling
'overlook': Room(name="Grand Overlook", description="""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.""", items=['power_booster', 'magic_mirror']),

'narrow': Room("Narrow Passage", """The narrow passage bends here from west
to north. The smell of gold permeates the air."""),
'narrow': Room(name="Narrow Passage", description="""The narrow passage bends here from west
to north. The smell of gold permeates the air.""", items=['map2', 'lightening_wand']),

'treasure': Room("Treasure Chamber", """You've found the long-lost treasure
'treasure': Room(name="Treasure Chamber", description="""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.""", items=['majic_key', 'sword2']),
}


Expand All @@ -38,6 +47,13 @@
#

# Make a new player object that is currently in the 'outside' room.
player = Player('John', 'outside')

# knife = Item('knife', 'small knife')
# map = Item('map', 'map for navigation to room')
# sword = Item('sword', ' long sward for battle')
# bike = Item('bike', 'bike for fast navigation')
# shoe = Item('shoe', 'magic shoe')

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

def find_key(value, my_dict):
key_list = list(my_dict.keys())
value_list = list(my_dict.values())
return key_list[value_list.index(value)]

while True:
print('\n','I am in the room called {}'.format(player.current_room), '\n')
room[player.current_room].get_items()
user_item = input('Please enter "take item1 item2" or drop item": ')
item_arr = user_item.split(' ')

action = item_arr[0]
items = item_arr[1:]
print('item_arr', items)
if item_arr[0] == 'take':
player.pickup_items(*items)
for item in items:
room[player.current_room].delete_items(item)

# text = f'I am in the room of {player.current_room}. I do not know where to go now However. I will explore around to find a room for treasure.'
# print(textwrap.wrap(text)[0])
user_input = input('Please enter the direction to go (for exit, enter "q"): ')

if user_input == 'q':
break
# print('user_input',user_input)
if user_input == 'n':
print('\n', 'going north')
if room[player.current_room].s_to != None:
player.current_room = find_key(room[player.current_room].s_to, room)
print('\n',f'now I am in {player.current_room}')
else:
print('\n', 'no room is in the direction, please choose others')

elif user_input == 's':
print('\n', 'going south')
if room[player.current_room].n_to != None:
player.current_room = find_key(room[player.current_room].n_to, room)
print('\n',f'now I am in {player.current_room}')
else:
print('\n', 'no room is in the direction, please choose others')

elif user_input == 'e':
print('\n', 'going east')
if room[player.current_room].w_to != None:
player.current_room = find_key(room[player.current_room].w_to, room)
print('\n',f'now I am in {player.current_room}')
else:
print('\n', 'no room is in the direction, please choose others')

elif user_input == 'w':
print('\n', 'going west')
if room[player.current_room].e_to != None:
player.current_room = find_key(room[player.current_room].e_to, room)
print('\n',f'now I am in {player.current_room}')
else:
print('\n', 'no room is in the direction, please choose others')
else:
print('Wrong key is entered, please try again')


5 changes: 5 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

class Item:
def __init__(self, name, description=""):
self.name = name
self.desciption = description
18 changes: 18 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
# Write a class to hold player information, e.g. what room they are in
# currently.
class Player:
def __init__(self, name, current_room, inventory=[]):
self.name = name
self.current_room = current_room
self.inventory = inventory


def __str__(self):
return f"<Player name: {self.name}, current_room: {self.current_room}, invenotry: {self.inventory}>"

def pickup_items(self, *items):
print('\nPlayer picked up items...\n')

for item in items:
self.inventory.append(item)
print(f'{item}')
print('\n')
print('self.inventory', self.inventory)
26 changes: 25 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,26 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.
from item import Item

class Room:
def __init__(self, name, description, items=[], n_to=None, e_to=None, s_to=None, w_to=None):
self.name = name
self.description = description
self.n_to = n_to
self.e_to = e_to
self.s_to = s_to
self.w_to = w_to
self.items = [Item(p) for p in items]
def __str__(self):
return f"<Room name: {self.name}, decription: {self.description}>\n"

def get_items(self):
print(f'{self.name} room has items as follows...')
for item in self.items:
print(f'{item.name}')
print('\n')

def delete_items(self, item):

self.items = [item1 for item1 in self.items if item1['name'] != item]
print(f'item deleted in the room: {item}')