Skip to content

Commit fe015cc

Browse files
LambdaLambda
authored andcommitted
create room and player class
1 parent 47394db commit fe015cc

2 files changed

Lines changed: 46 additions & 5 deletions

File tree

‎src/player.py‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,40 @@
11
# Write a class to hold player information, e.g. what room they are in
22
# currently.
3+
# create a player item and some items to pick up
4+
# then create a room and add the player to the room
5+
6+
# import rooms for composition fields
7+
from room import Room
8+
9+
310
class Player:
4-
def __init__(self, name, age, location):
11+
def __init__(self, name, age, current_room):
512
self.name = name
613
self.age = age
7-
self.location = location
14+
self.current_room = current_room
15+
16+
# methods
17+
18+
# getters
19+
def get_name(self):
20+
return self.name
21+
22+
# setters
23+
def set_name(self, name):
24+
self.name = name
25+
26+
def __str__(self):
27+
output = ""
28+
output += self.name + "\n" + "age" + self.age
29+
i = 1
30+
for c in self.current_room:
31+
output += " " + str(i) + ". " + c.name + "\n"
32+
i += 1
33+
34+
# add an exit message
35+
output += " " + str(i) + ". Exit"
36+
return output
37+
38+
39+
s = Player('Player 1', 'Eko', [Room('Big Apple', 'NYC', 'n'), Room(
40+
'Gold Coast', 'Ghana', 's'), Room('Safari', 'SA', 'e'), Room('Naija', 'Nigeria', 'w')])

‎src/room.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
# Implement a class to hold room information. This should have name and
22
# description attributes.
3-
3+
# Create a Room class with a name and a contains[] and a method add_item
4+
# Create an Item class with a name and a contains[] that can print contents and pick up item
5+
from player import Player
46
class Room:
57
# constructor
6-
def __init__(self, name, description):
8+
def __init__(self, name, description, directions):
79
self.name = name
810
self.description = description
9-
# methods
11+
self.directions = directions
12+
# methods
13+
def __str__(self):
14+
return f"In room {self.name}.\n{self.description} it is located in the {self.directions}"
15+
16+
def __repr__(self):
17+
return f"{self.name}, {self.description}, {self.directions}"

0 commit comments

Comments
 (0)