class Room

  attr_accessor :burrowable

  def initialize 
    @east = self
    @west = self
    @north = self
    @south = self
    @up = self
    @down = self                
    @items = Array.new          # items in the room
    @monsters = Array.new       # monsters in the room
    @description = ""           # printed on the first visit
    @short_description = ""     # printed after the second visit
    @seen = false               # has the room been visited before?
    @burrowed = false           # has the room been burrowed?
  end

  def monster(m)
    @monsters += [m]
    m.position= self  # the monster is also aware of his position
  end

  def monsters
    @monsters
  end

  def description(msg)
    @description=msg
    @short_description = msg
  end

  def short_description(msg)
    @short_description=msg
  end

  def describe
    if @seen == false
      system "cls" or system "clear"  # from stackoverflow 3170553
      puts @description
      @seen = true
      if @items.length > 0 
        puts "On the floor are some items: "
        @items.each do |i| puts " a #{i}," end
      end
      if @monsters.length > 0
        if @monsters.length == 1
          printf "A #{@monsters.first} lurks.\n"
        else
          printf "#{@monsters.first} "
          @monsters.each do |m| print ", #{m}" end # will reprint first
          puts "lurk."
        end
      end
    else
      puts @short_description
      @items.each do |i| puts i end
      @monsters.each do |m| puts m end
    end
  end
    
  def east(c) @east end
  def west(c) @west end  
  def north(c) @north end
  def south(c) @south end
  def up(c) 
    if true #c.has "ladder"
      @up 
    else 
      self 
    end 
  end
  def down(c) 
    if c.has("spade") && @burrowable==true 
      if @burrowed == false
        puts "You dig, and lo and behold, find another chamber."
        @burrowed = true
      end
      @down
    else 
      self 
    end 
  end

  # the following setter functions set up a bidirectional relationship.
  # while preventing infinite recursion
  def east=(room)
    if @east != room
      @east = room
      room.west= self
    end
  end

  def west=(room)
    if @west != room
      @west = room
      room.east = self
    end
  end
  
  def north=(room)
    if @north != room
      @north=room
      room.south = self
    end
  end
  
  def south=(room)
    if @south != room
      @south = room
      room.north = self
    end
  end
  
  def up=(room)
    if @up != room
      @up = room
      room.down = self
    end
  end

  def down=(room)
    if @down != room
      @down = room
      room.up = self
    end
  end

  def items(*optional)
    @items += optional
  end

  # will remove all instances of the item
  def remove_item_by_name(item_name)
    for i in 0..@items.length-1
      if @items[i].name == item_name
        value = @items[i]
        @items.delete(value)
        break
      end
    end
    value
  end

  def remove_item(item)
    @items = @items-[item]
  end

  def remove_monster(monster)
    @monsters = @monsters-[monster]
  end

  def attack_by(source)
    if @monsters.length > 0
      puts "#{source} attacks #{monsters[0]} using #{source.weapon}."
      monsters[0].get_hit_by(source, source.weapon.hit_points)
    else
      puts "#{source} attacks empty air."
    end
  end
end

