kevinnio
5/6/2014 - 11:44 PM

Código del nivel 9, modo Intermediate

Código del nivel 9, modo Intermediate

class Player
  
  def play_turn(warrior)
    @units = warrior.listen
    @enemies = @units.select { |unit| unit.enemy? }
    @captives = @units.select { |unit| unit.captive? }
    @hurry_captives = @captives.select { |captive| captive.ticking? }
    
    [@hurry_captives, @enemies, @captives].each do |units|
      if not units.empty?
        act warrior, warrior.direction_of(units.first)
        break
      end
    end
    
    act warrior, warrior.direction_of_stairs if @units.empty?
  end
  
  def enemies_around(warrior, direction)
    er = []
    [:backward, :left, :right, :forward] .each do |d|
      space = warrior.feel(d)
      er.push space if space.enemy? and d != direction
    end
    return er
  end
  
  def act(w, d)
    er = enemies_around(w, d)
    if not er.empty?
      er.each do |e|
        if w.direction_of(e) != d
          w.bind! w.direction_of(e)
          break
        end
      end
    elsif w.feel(d).empty?
      if er.empty? and (w.health < 10 or 
      (w.health < 20 and @hurry_captives.empty? and not @units.empty?))
        w.rest!
      else 
        w.walk! d
      end
    elsif w.feel(d).enemy?
      look = w.look.select { |s| s.enemy?}
      captive_ahead = w.look.select {|s| s.captive?}
      if look.size >= 2 and captive_ahead.empty?
        w.detonate! d
      else
        w.attack! d
      end
    elsif w.feel(d).captive?
      w.rescue! d
    end
  end
  
end