from random import random, choiceimport pickleHAZARD_BATS = 1HAZARD_PIT = 2NUM_ROOMS = 20#room naming codewords = open("/usr/share/dict/words", "r").read().split("\n")def nameRoom(): #Debugging feature. it's nice to have easy-to-remember room names return "The %s %s Room"%(choice(words).capitalize(), choice(words).capitalize())nameRoom()class Room(): def __init__(self): #HACKhackhack, get a unique room ID which is NOT absolutely unique #they may also be rather obscene as they are drawn from the system #dictionary. self.name = nameRoom() self.doors = [] self.hazard = 0 self.contents = [] self.inscription = "" def __str__(self): if self.doors == []: output = "No exits. " else: output = "Doors to: " + str(self.doors) + ". " if self.hazard == HAZARD_BATS: output += "Contains bats." elif self.hazard == HAZARD_PIT: output += "Contains a pit." return output def link(self, other): self.doors.append(other) other.doors.append(self) def put(self, item): self.contents.append(item)rooms = []for i in range(NUM_ROOMS): newRoom = Room() if random() < 0.1: newRoom.hazard = HAZARD_BATS elif random() < 0.1: newRoom.hazard = HAZARD_PIT rooms.append(newRoom)#Make doors#Loop over the rooms, and add each room to another random room's doors.for oneRoom in rooms: otherRoom = choice([room for room in rooms if room is not oneRoom]) oneMoreRoom = choice( [room for room in rooms if room is not oneRoom and room is not otherRoom]) oneRoom.link(otherRoom) oneMoreRoom.link(otherRoom)#Pick a random room for player to startcurrentRoom = choice(rooms)while True: print "You are in:", currentRoom.name for i in range(len(currentRoom.doors)): print "Door %s leads to:"%(i+1), currentRoom.doors[i].name if currentRoom.inscription is not "": print """There is some writing on the floor. It reads, "%s"."""%currentRoom.inscription if len(currentRoom.contents) > 1: itemline = "There is %s"%currentRoom.contents[0] for i in range(1, len(currentRoom.contents)-1): itemline += ", " + item itemline += " and %s here."%currentRoom.contents[-1] print itemline elif len(currentRoom.contents) == 1: print "There is %s here."%currentRoom.contents[0] action = raw_input("action to take: ").split(" ") if action[0].lower() == "door": if action[1].isdigit(): try: currentRoom = currentRoom.doors[int(action[1])-1] except: print "That's not a door." else: for room in currentRoom.doors: if " ".join(room.name.lower().split(" ")[1:2]).startswith( " ".join(action[1:2]).lower()): currentRoom = room elif action[0].lower() == "put": currentRoom.contents.append(" ".join(action[1:])) elif action[0].lower() == "take": currentRoom.contents.remove(" ".join(action[1:])) elif action[0].lower() == "inscribe": currentRoom.inscription = (" ".join(action[1:])) elif action[0].lower() == "save": savehandle = open(" ".join(action[1:]), "w") pickle.dump(rooms, savehandle) savehandle.close() print "Saved world to %s."%" ".join(action[1:]) elif action[0].lower() == "load": rooms = pickle.load(open(" ".join(action[1:]), "r")) currentRoom = choice(rooms) print "Loaded world from %s."%" ".join(action[1:]) print''' Best Of room names...The Amoebic Transportation Room (it'd be a good name for a band!)The Palpation Handshakes Room (very vivid mental image)The Burned Wall's Room (sounds almost planned. Might be a pub?)The Eel Brigand Room (the contents of my hovercraft have escaped!)The Snowshoes Unromantic Room (they are, aren't they)The Peninsular Scoring Room (I'd give California a 6/10.)'''