Skip to content

Amer Mahyoub - CSPT12 - Intro-Python-II #491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
24 changes: 24 additions & 0 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from room import Room
from player import Player
from items import Item

# Declare all the rooms

Expand All @@ -22,6 +24,7 @@
}



# Link rooms together

room['outside'].n_to = room['foyer']
Expand All @@ -36,6 +39,27 @@
#
# Main
#
directions = ['n', 's', 'e', 'w', 'q']
player = Player(room['outside'])

while True:
print(player.current_room)
print(player.current_room.description)

usr_input = input("Where would you like to go? ")

try:
while usr_input not in directions:
print("\nInput a valid direction [n, s, e, w]\n")
usr_input = input("Where would you like to go? ")

if usr_input == 'q':
break

player.move(usr_input)

except AttributeError as error:
print("\nYou can't move that way!!\n")

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

Expand Down
4 changes: 4 additions & 0 deletions src/items.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Item:
def __init__(self, name, description):
self.name = name
self.description = description
17 changes: 17 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
# Write a class to hold player information, e.g. what room they are in
# currently.

class Player:
def __init__(self, current_room, items: List[Item]=None):
self.current_room = current_room
self.items: List[Item] = items

def move(self, direction):
next_room = self.current_room.get_direction(direction)
if next_room is not None:
self.current_room = next_room
else:
print("You can't move that way!!")


def __repr__(self):
return f'Room({repr(self.current_room)})'

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

class Room:
def __init__(self, name, description, items: List[Item]=None):
self.name = name
self.description = description
self.items: List[Item] = items

def get_direction(self, direction):
if direction == 'n':
return self.n_to
elif direction == 's':
return self.s_to
elif direction == 'e':
return self.e_to
elif direction == 'w':
return self.w_to

def add_item(self, item):

def __repr__(self):
return f'Room({repr(self.name)})'