Skip to content

Instantly share code, notes, and snippets.

@calthoff
Created May 5, 2020 19:56
Show Gist options
  • Select an option

  • Save calthoff/f96f3268072adbcac2d02feb0d03e0d0 to your computer and use it in GitHub Desktop.

Select an option

Save calthoff/f96f3268072adbcac2d02feb0d03e0d0 to your computer and use it in GitHub Desktop.
import random
class Room:
rooms = []
def __init__(self, name, description):
self.name = name
self.description = description
self.linked_rooms = {}
Room.rooms.append(self) # self.rooms.append
def __repr__(self):
return self.name.title()
def link_room(self, direction, linked_room):
self.linked_rooms[direction] = linked_room
def get_moves(self):
moves = []
for direction in self.linked_rooms:
moves.append(direction.upper())
print("\nMoves: " + ", ".join(moves))
def describe(self):
print("\nYou see " + self.description + ".")
class House:
def __init__(self):
self.reception_hall = Room("reception hall", "a coat rack, a grandfather clock, a collapsed staircase, and an entry table with a tray for calling cards")
self.drawing_room = Room("living room", "a fireplace, a phonograph, and a card table")
self.dining_room = Room("dining room", "a long table with ten chairs, a hutch, and a crystal chandelier")
self.kitchen = Room("kitchen", "a wood stove, a cauldron, and a basin sink")
self.bedroom = Room("bedroom", "a canopy bed, a vanity table, and a wardrobe")
self.reception_hall.link_room("EAST", self.drawing_room)
self.reception_hall.link_room("NORTH", self.dining_room)
self.drawing_room.link_room("WEST", self.reception_hall)
self.drawing_room.link_room("NORTH", self.bedroom)
self.bedroom.link_room("SOUTH", self.drawing_room)
self.bedroom.link_room("WEST", self.dining_room)
self.dining_room.link_room("SOUTH", self.reception_hall)
self.dining_room.link_room("EAST", self.bedroom)
self.dining_room.link_room("NORTH", self.kitchen)
self.kitchen.link_room("SOUTH", self.dining_room)
self.kitchen.link_room("NORTH", "Exit!")
class Person:
def __init__(self, name):
self.name = name
self.current_room = None
self.visited_rooms = []
self.winner = False
def __repr__(self):
return self.name
def move(self, direction):
room = self.current_room
self.visited_rooms.append(room)
if room.linked_rooms[direction] == "Exit!":
self.winner = True
elif direction in room.linked_rooms:
self.current_room = room.linked_rooms[direction]
if self.current_room in self.visited_rooms:
print("\nYou have returned to the " + self.current_room.name + ".")
else:
print("\nYou find yourself in a " + self.current_room.name + ".")
class Game:
def __init__(self):
self.house = House()
def play(self):
# This method is too long. Break it up into multiple methods.
play_again = True
while play_again:
name = input("Welcome to our house. What is your name, visitor? ").title()
self.player = Person(name) # define these in __init__ first
self.player.winner = False # define these in __init__ first
starting_room = random.choice(Room.rooms)
self.player.current_room = starting_room
print("\n" + self.player.name + ", you find yourself in an abandoned Victorian " + self.player.current_room.name + ". It looks every day as old as its 120+ years.")
while self.player.winner == False:
choice = input("\nWhat would you like to do?\nLOOK around the room\nMOVE to other rooms\nQUIT\n").upper()
if choice == 'LOOK':
self.player.current_room.describe()
elif choice == 'MOVE':
self.player.current_room.get_moves()
direction = input("Which way would you like to go?\n").upper()
self.player.move(direction)
if self.player.winner == True:
print("\nCongratulations! You've escaped!")
break
elif choice == 'QUIT':
print("\nGoodbye, " + self.player.name + ". We'll still be here, if you dare to return!")
break
else:
print("\nI didn't understand your instructions, " + self.player.name + ". Please select again.")
continue
choice = input("\nWould you like to play again? (Y/N) ").upper()
if choice == 'N':
play_again = False
game = Game()
game.play()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment