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
102 changes: 62 additions & 40 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,73 @@
from room import Room
from player import Player
from item import Item
from roomInfo import roomInfo

# Declare all the rooms

room = {
'outside': Room("Outside Cave Entrance",
"North of you, the cave mount beckons"),
class Game:
def __init__(self, roomsInfo, items=None):
self.rooms = self._createRooms(roomsInfo)
self.items = items

'foyer': Room("Foyer", """Dim light filters in from the south. Dusty
passages run north and east."""),
def start(self):
while True:
print("Welcome to the game! As of right now, you can walk around rooms but that's unfortunately it!\nEnter 'q' at any prompt to quit any time!")
playerName = self._getPlayerName()
if playerName == "q":
break
else:
self.player = Player(playerName)
self._enterRoom(self.player.location)
break

def _getPlayerName(self):
playerName = input("\nPlease enter a name for your character: ")
if len(playerName) > 0:
return playerName

'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."""),
def _findRoomByID(self, id):
for key in self.rooms:
if self.rooms[key].id == id:
return self.rooms[key]

'narrow': Room("Narrow Passage", """The narrow passage bends here from west
to north. The smell of gold permeates the air."""),
def _selectRoom(self, room):
while True:
direction = input(f"\nWhich direction would you like to go? There are doorways in these directions:\n{', '.join(room.connections.keys())}: ")
if direction == "q":
break
elif direction in room.connections:
self._enterRoom(room.connections[direction])
break

'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."""),
}
def _enterRoom(self, roomID):
self.player.location = roomID
room = self._findRoomByID(roomID)
room.enter(self.player.name)
self._selectRoom(room)


# Link rooms together
def _linkRooms(self, rooms):
for key in rooms:
# Builds links for each room. rooms[key] is the class object for a room.
# The links are created from the provided links in roomInfo.
# However, the connecting room's id must be found to create the connection
links = roomInfo[key]["links"]
for direction in links:
linkedKey = links[direction]
id = rooms[linkedKey].id
rooms[key].connectRooms(id, direction)

room['outside'].n_to = room['foyer']
room['foyer'].s_to = room['outside']
room['foyer'].n_to = room['overlook']
room['foyer'].e_to = room['narrow']
room['overlook'].s_to = room['foyer']
room['narrow'].w_to = room['foyer']
room['narrow'].n_to = room['treasure']
room['treasure'].s_to = room['narrow']
def _createRooms(self, roomsInfo):
rooms = {}
for key in roomInfo:
# Iterate roomInfo and for each 'key', create a new Room class, populate 'rooms'
info = roomInfo[key]
rooms[key] = Room(info["name"], info["description"])
# _linkRooms will mutate the objects, no need to assign
self._linkRooms(rooms)
return rooms


#
# Main
#

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

# Write a loop that:
#
# * Prints the current room name
# * Prints the current description (the textwrap module might be useful here).
# * Waits for user input and decides what to do.
#
# If the user enters a cardinal direction, attempt to move to the room there.
# Print an error message if the movement isn't allowed.
#
# If the user enters "q", quit the game.

game = Game(roomInfo)
game.start()
7 changes: 7 additions & 0 deletions src/id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def setID():
id = 0
def incrementID():
nonlocal id
id += 1
return id
return incrementID
10 changes: 10 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from id import setID
getID = setID()

class Item:
def __init__(self, name, description):
self.id = f"i{getID()}"
self.name = name
self.description = description
def examine():
print(self.description)
16 changes: 16 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
# Write a class to hold player information, e.g. what room they are in
# currently.

from id import setID
getID = setID()

class Player:
def __init__(self, name, location="r1", inventory=[]):
self.id = f"p{getID()}"
self.name = name
self.location = location
self.inventory = inventory
def pickUpItem(id):
self.inventory.append(id)
def dropItem(id):
self.inventory.remove(id)
def moveToRoom(id):
self.location = id
32 changes: 31 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.

import os

from id import setID
getID = setID()

class Room:
def __init__(self, name, description="", connections=None, items=None, poi=None):
self.id = f"r{getID()}"
self.name = name
self.description = description
self.connections = connections
self.items = items
self.poi = poi
def updateAttrs(self, attr, update):
self[attr] = update
def pickUpItem(self, temName):
index = 0
for i in range(0, len(items)):
if(items[i].name is itemName):
index = i
return items.pop(index)
def enter(self, playerName):
os.system('cls' if os.name == 'nt' else 'clear')
print(f"\n{playerName} moves to the {self.name}.\n{self.description}")
def connectRooms(self, roomID, toDirection):
if self.connections is None:
self.connections = {toDirection: roomID}
else:
self.connections[toDirection] = roomID
27 changes: 27 additions & 0 deletions src/roomInfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
roomInfo = {
"outside":{
"name": "Outside Cave Entrance",
"description": "North of you, the cave mount beckons",
"links":{"north":"foyer"}
},
"foyer":{
"name": "Foyer",
"description": "Dim light filters in from the south. Dusty passages run north and east.",
"links":{"south":"outside", "north":"overlook", "east":"narrow"}
},
"overlook":{
"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.""",
"links":{"south":"foyer"}
},
"narrow":{
"name": "Narrow Passage",
"description": """The narrow passage bends here from west to north. The smell of gold permeates the air.""",
"links":{"west":"foyer", "north":"treasure"}
},
"treasure":{
"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.""",
"links":{"south":"narrow"}
}
}