Skip to content

Commit 1708efc

Browse files
committed
built room class and added move_to and display methods to player
1 parent dffcffb commit 1708efc

3 files changed

Lines changed: 30 additions & 7 deletions

File tree

‎src/adv.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,16 @@
5252
# If the user enters "q", quit the game.
5353

5454
def welcome_player(player_name):
55-
welcome_msg = f'Welcome {player_name}'
55+
welcome_msg = f'Welcome {player_name}!'
5656
return welcome_msg
5757

5858
def play_game(player):
59-
player.display_info()
59+
6060
playing = True
6161

6262
while playing:
63-
action = input('Which direction will you venture? ')
63+
player.display_info()
64+
action = input('Which direction will you venture? ').lower()
6465

6566
if(action == 'q'):
6667
playing = False
@@ -73,7 +74,7 @@ def play_game(player):
7374
"w": player.move_to,
7475
}
7576
try:
76-
possible_actions[action]
77+
possible_actions[action](action)
7778
except KeyError:
7879
print('Invalid direction, please choose a valid direction')
7980

‎src/player.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,19 @@ def __init__(self, name, current_room):
66
self.name = name
77
self.current_room = current_room
88

9-
def __str__(self):
10-
return self.current_room
9+
def display_info(self):
10+
print(self.current_room)
11+
12+
def move_to(self, direction):
13+
self.direction = direction
14+
15+
possible_direction = {
16+
'n': 'n_to',
17+
's': 's_to',
18+
'e': 'e_to',
19+
'w': 'w_to'
20+
}
21+
try:
22+
self.current_room = getattr(self.current_room, possible_direction[direction])
23+
except AttributeError:
24+
print("You cannot move in that direction")

‎src/room.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
# Implement a class to hold room information. This should have name and
2-
# description attributes.
2+
# description attributes.
3+
4+
class Room:
5+
def __init__(self, name, description):
6+
self.name = name
7+
self.description = description
8+
9+
def __str__(self):
10+
return (f'{self.name} {self.description}')

0 commit comments

Comments
 (0)