Repository navigation
CS_26 Introduction Python II TL-Brian Illes #1
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
base: master
Are you sure you want to change the base?
Changes from all commits
16586bc
5fe613f
a8e32ec
ef03b09
b249f1f
f5d7dcd
a76eef4
31cc914
11b9d06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,35 @@ | ||
| from room import Room | ||
| from player import Player | ||
| from item import Item | ||
| import textwrap | ||
|
|
||
| # Declaring all items: | ||
| items = { | ||
| 'sword': Item('sword', 'magic sword for slaying loneliness'), | ||
| 'shield': Item('shield', 'normal shield for protection against humans'), | ||
| 'map': Item('map', 'for when the travelers are lost.') | ||
| } | ||
|
|
||
| # Declare all the rooms | ||
|
|
||
| room = { | ||
| 'outside': Room("Outside Cave Entrance", | ||
| "North of you, the cave mount beckons"), | ||
| "North of you, the cave mount beckons", | ||
| [items['sword'], items['map']]), | ||
|
|
||
| 'foyer': Room("Foyer", """Dim light filters in from the south. Dusty | ||
| passages run north and east."""), | ||
| passages run north and east.""", []), | ||
|
|
||
| '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.""", [items['shield']]), | ||
|
|
||
| '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.""", []), | ||
|
|
||
| '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.""", [items['map']]), | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -49,3 +60,56 @@ | |
| # Print an error message if the movement isn't allowed. | ||
| # | ||
| # If the user enters "q", quit the game. | ||
|
|
||
| # helper function that evaluates whether there is a room in user | ||
| # specified direction: | ||
| #def has_room(): | ||
|
|
||
| # Pick the name of the character in the game. | ||
| player_name = input('Before you proceed, please type in a name for your player! ') | ||
|
|
||
| # Initial room variable: | ||
| current_room = room['outside'] | ||
|
|
||
| # initializes the Player class with user inputted name and initial room (cave entrance) | ||
| character = Player(player_name, room['outside']) | ||
|
|
||
| print(f'\nCurrent room: name {current_room.name}\n description {current_room.description}\n') | ||
|
|
||
| while True: | ||
|
|
||
| current_room.show_items() | ||
|
|
||
| user_input = input('\nPlease enter a direction you want the player to move. Selections include: [n, s, e, w]! ') | ||
|
|
||
| user_input = user_input.split(' ') | ||
|
|
||
| if len(user_input) == 1: | ||
|
|
||
| if user_input[0] in ['n','s','w','e']: | ||
| character.move(user_input[0]) | ||
| current_room = character.current_room | ||
|
|
||
| elif user_input[0] in ['i', 'inventory']: | ||
| character.show_inventory() | ||
|
|
||
| elif user_input[0] == 'q': | ||
| print('Goodbye!!!') | ||
| break | ||
|
|
||
| else: | ||
| print('\nPlease use the valid inputs [n, s, e, w] to move, drop [item] and take [item], i to check inventory, or q to quit\n') | ||
|
|
||
| elif len(user_input) == 2: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice work! What could we do to make these |
||
|
|
||
| if user_input[0] == 'take': | ||
| character.pickup_item(user_input[1]) | ||
|
|
||
| elif user_input[0] == 'drop': | ||
| print(character.drop_item(user_input[1])) | ||
|
|
||
| else: | ||
| print('\nPlease use the valid inputs [n, s, e, w] to move, drop [item] and take [item], i to check inventory, or q to quit\n') | ||
|
|
||
| elif len(user_input) > 2: | ||
| print('User input exceeded input limit. Maximum input accepted 2 words') | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| class Item: | ||
| def __init__(self, name, description): | ||
|
|
||
| self.name = name | ||
| self.description = description | ||
|
|
||
| def on_take(self): | ||
| print(f'You have picked up {self.name}') | ||
|
|
||
| def __str__(self): | ||
| return str(self.__dict__) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,48 @@ | ||
| # Write a class to hold player information, e.g. what room they are in | ||
| # currently. | ||
|
|
||
| class Player: | ||
|
|
||
| def __init__(self, name, current_room): | ||
|
|
||
| self.name = name | ||
| self.current_room = current_room | ||
| self.items = [] | ||
|
|
||
| def show_inventory(self): | ||
| if len(self.items) > 0: | ||
| print(f'\n{self.name}\'s item inventory: ') | ||
| for item in self.items: | ||
| print(item) | ||
| else: | ||
| print(f'\nCharacter {self.name} needs items!') | ||
|
|
||
| def drop_item(self, item): | ||
| for item_name in self.items: | ||
| if item == item_name.name: | ||
| reclaim_item = self.items.pop(self.items.index(item_name)) | ||
| self.current_room.restock_item(reclaim_item) | ||
| return f'{reclaim_item.name} has been returned to room {self.current_room.name}' | ||
|
|
||
| return f'\nCan\'t find item {item} in inventory\n' | ||
|
|
||
| def move(self, direction): | ||
| next_room = getattr(self.current_room, f'{direction}_to') | ||
| if isinstance(next_room, str): | ||
| print(f'\n~~~~Error~~~~~\n') | ||
| print(next_room) | ||
| else: | ||
| self.current_room = next_room | ||
| print(f'\nCurrent room: name {next_room.name}\n description {next_room.description}\n') | ||
|
|
||
| def pickup_item(self, item): | ||
|
|
||
| if self.current_room.search_item(item): | ||
| acquired_item = self.current_room.lose_item(item) | ||
| acquired_item.on_take() | ||
| self.items.append(acquired_item) | ||
| else: | ||
| print(f'\nCan\'t find item {item} in room {self.current_room.name}\n') | ||
|
|
||
| def __str__(self): | ||
| return str(self.__dict__) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,39 @@ | ||
| # 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.n_to = 'Sorry there is no room north of {}\n'.format(self.name) | ||
| self.s_to = 'Sorry there is no room south of {}\n'.format(self.name) | ||
| self.e_to = 'Sorry there is no room east of {}\n'.format(self.name) | ||
| self.w_to = 'Sorry there is no room west of {}\n'.format(self.name) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool initialization here |
||
| self.items = items | ||
|
|
||
| def search_item(self, item): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice abstraction of |
||
| for item_name in self.items: | ||
| if item == item_name.name: | ||
| return True | ||
| return False | ||
|
|
||
| def show_items(self): | ||
| if len(self.items) > 0: | ||
| print(f'\nItems in {self.name}:') | ||
| # print(item_array[0]) | ||
| for item in self.items: | ||
| print(item) | ||
| else: | ||
| print(f'\nNo items in {self.name}.') | ||
|
|
||
| def restock_item(self, item): | ||
| self.items.append(item) | ||
|
|
||
| def lose_item(self, item): | ||
| for item_name in self.items: | ||
| if item == item_name.name: | ||
| return self.items.pop(self.items.index(item_name)) | ||
|
|
||
| def __str__(self): | ||
| return str(self.__dict__) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactor is on point. Far more readable :)