ruby Códigodelnivel 9,modo Intermediate

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Códigodelnivel 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

以上是关于ruby Códigodelnivel 9,modo Intermediate的主要内容,如果未能解决你的问题,请参考以下文章

ruby Códigodelnivel 9,modo Intermediate de Ruby Warrior

ruby Códigodelnivel 7,modo Intermediate de Ruby Warrior

ruby Códigodelnivel 6,modo Intermediate de Ruby Warrior

ruby Códigodelnivel 5,modo Intermediate de Ruby Warrior

ruby Códigodelnivel 4,modo Intermediate de Ruby Warrior

ruby Códigodelnivel 3,modo Intermediate de RubyWarrior