ruby Códigodelnivel 6,modo Intermediate de Ruby Warrior

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Códigodelnivel 6,modo Intermediate de Ruby Warrior相关的知识,希望对你有一定的参考价值。

class Player
  def play_turn(warrior)
    @units, @enemies, @captives = warrior.listen, warrior.listen.select {|s| s.enemy?}, warrior.listen.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.size > 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 < 20 and @hurry_captives.empty? and not @units.empty?
        warrior.rest!
      elsif warrior.feel(direction).empty?
        warrior.walk! direction
      else
        warrior.feel(direction).captive? ? warrior.rescue!(direction) : warrior.attack!(direction)
      end
    end
  end
end

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

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