kevinnio
5/8/2014 - 12:32 AM

Código del nivel 9, modo Intermediate de Ruby Warrior

Código del nivel 9, modo Intermediate de Ruby Warrior

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