ruby Códigodelnivel 9,modo Intermediate de Ruby Warrior

Posted

tags:

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

以上是关于ruby Códigodelnivel 9,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