import random

worldmap = {}

class Room (object):
    def __init__(self, name, description):
        global worldmap
        self.name = name
        self.description = description
        worldmap[self.name] = self
        self.doors = {}
        self.items = []
        self.visits = 0
        
    def visit(self, player):
        print(self.name)
        self.visits += 1
        if self.visits == 1:
            self.describe()
        
    def describe(self):
        print(self.description)
        for d in self.doors:
            print("You can go %s" % d)
        for i in self.items:
            r = random.randint(0,100)
            if i.visibility > r:
                print("There is a %s here" % i.name)
     
    def take(self, itemname):
        for idx in range(len(self.items)):
            if self.items[idx].name == itemname:
                item = self.items[idx]
                del self.items[idx]
                return item
        return None

    def makeDoor(self, named, to):
        global worldmap
        self.doors[named] = to
        
    def addItem(self, item):
        self.items.append(item)
     
class RoomOfDying (Room):
    def visit(self, player):
        Room.visit(self, player)
        player.health = 0

class Player (object):
    def __init__(self, name):
        self.name = name
        self.health = 100
        self.inventory = []
        
    def take(self, item):
        self.inventory.append(item)
       
class Item (object):
    def __init__(self, name, visibility=100, weight=1):
        self.name = name
        self.weight = weight
        self.visibility = visibility

class Armor (Item):
    def __init__(self, name, visible=100, weight=1, strength=10):
        Item.__init__(self, name, visible, weight)
        self.strength = strength
        
def addDoor(frm, named, to):
    global worldmap
    worldmap.get(frm).makeDoor(named, to)
    
def placeItem(roomname, item):
    global worldmap
    worldmap.get(roomname).addItem(item)
 
def setupWorld(): 
    ring = Item("ring of power")
    
    Room("start", "You are in a room")
    Room("path", "Winding path")
    placeItem("path", ring)
    
    Room("brook", "Babbling Brook")
 
    RoomOfDying("whirlpool", "You have drowned")
    
    addDoor('start', 'north', 'path')
    addDoor('path', 'north', 'brook')
    addDoor('path', 'south', 'start')
    addDoor('brook', 'south', 'path')
    addDoor('brook', 'swimming', 'whirlpool')

def rpg():
    setupWorld()
    
    r = worldmap.get('start')
    p = Player(raw_input("Your name? "))
    print
    
    r.visit(p)
    while p.health > 0:
        action = raw_input("Now What? ")
        if action == 'describe' or action == 'd':
            r.describe()
        elif action == 'quit' or action == 'q':
            print("OKBYE")
            return
        elif action == 'inventory' or action == 'i':
            print("You're carrying:")
            for i in p.inventory:
                print ("    %s" % i.name)
        elif action.startswith("take "):
            itemname = action[5:]
            item = r.take(itemname)
            if not item:
                print("I don't see a %s here" % itemname)
            else:
                p.take(item)
                print ("%s taken" % itemname)
            
        elif action in r.doors:
            r = worldmap.get(r.doors[action])
        else:
            print("I don't know how to do that.") 
            
        if not r:
            print("That room doesn't exist.  Goodbye.")
            return
            
        r.visit(p)  #XXX new parameter
        
        

if __name__ == '__main__':
    rpg()