Ruby Warrior 簡單版攻略


#1

遊戲在這邊

https://www.bloc.io/ruby-warrior

可以的話請先挑戰不要看我的答案,邊玩邊寫這篇,用簡單的思路完成

第一關(走路)

class Player
  def play_turn(warrior)
    warrior.walk!
  end
end

第二關(打怪x1)

class Player
  def play_turn(warrior)
    if(warrior.feel.enemy?)
      warrior.attack!
    else
      warrior.walk!
    end
  end
end

第三關(一條路很多怪)

class Player
  def play_turn(warrior)
    if(warrior.feel.enemy?)
      warrior.attack!
    else
      if(warrior.health < 20)
        warrior.rest!
      else
        warrior.walk!
      end
    end
  end
end

第四關(弓箭手)

class Player
  def play_turn(warrior)
    @health ||= warrior.health
    if(warrior.feel.enemy?)
      warrior.attack!
    elsif(@health > warrior.health)
      warrior.walk!
    elsif(warrior.health < 20)
      warrior.rest!
    else
      warrior.walk!
    end
    @health = warrior.health
  end
end

第五關(拯救)

class Player
  def play_turn(warrior)
    @health ||= warrior.health
    if(warrior.feel.enemy?)
      warrior.attack!
    elsif(@health > warrior.health)
      warrior.feel.captive? ? warrior.rescue! : warrior.walk!
    elsif(warrior.health < 20)
      warrior.rest!
    else
      warrior.feel.captive? ? warrior.rescue! : warrior.walk!
    end
    @health = warrior.health
  end
end

第六關(向後走)

class Player
  def gogogo(warrior)
    if(!@backed && !warrior.feel(:backward).wall?)
      warrior.feel(:backward).captive? ? warrior.rescue!(:backward) : warrior.walk!(:backward)
    else
      warrior.feel.captive? ? warrior.rescue! : warrior.walk!
      @backed = true
    end
  end
  def play_turn(warrior)
    @health ||= warrior.health
    if(warrior.feel.enemy?)
      warrior.attack!
      @safe = false
    elsif(@health > warrior.health)
      gogogo(warrior)
    elsif(warrior.health < 20)
      if(warrior.health < 10 && !@safe)
        @safe = true
        warrior.walk!(:backward)
      else
        warrior.rest!
      end
    else
      gogogo(warrior)
    end
    @health = warrior.health
  end
end

第七關(向後轉)

class Player
  def gogogo(warrior)
    warrior.feel.captive? ? warrior.rescue! : warrior.walk!
  end
  def play_turn(warrior)
    @health ||= warrior.health
    return warrior.pivot! if warrior.feel.wall?
    if(warrior.feel.enemy?)
      warrior.attack!
      @safe = false
    elsif(@health > warrior.health)
      gogogo(warrior)
    elsif(warrior.health < 20)
      if(warrior.health < 10 && !@safe)
        @safe = true
        warrior.walk!(:backward)
      else
        warrior.rest!
      end
    else
      gogogo(warrior)
    end
    @health = warrior.health
  end
end

第八關(巫師:look回傳的是space的array)

class Player
  def gogogo(warrior)
    warrior.feel.captive? ? warrior.rescue! : warrior.walk!
  end
  def play_turn(warrior)
    @health ||= warrior.health
    return warrior.pivot! if warrior.feel.wall?
    if(warrior.feel.enemy?)
      warrior.attack!
      @safe = false
    elsif(warrior.look.any?{|space|space.enemy?} && !warrior.look.any?{|space|space.captive?})
      warrior.shoot!
    elsif(@health > warrior.health)
      gogogo(warrior)
    elsif(warrior.health < 20)
      if(warrior.health < 10 && !@safe)
        @safe = true
        warrior.walk!(:backward)
      else
        warrior.rest!
      end
    else
      gogogo(warrior)
    end
    @health = warrior.health
  end
end

第九關(小心背後:偷懶先走一格)

class Player
  def gogogo(warrior)
    warrior.feel.captive? ? warrior.rescue! : warrior.walk!
  end
  def play_turn(warrior)
    @health ||= warrior.health
    unless @first
      @first = true
      return warrior.walk!
    end
    return warrior.pivot! if warrior.feel.wall?
    if(warrior.feel.enemy?)
      warrior.attack!
      @safe = false
    elsif(warrior.look.any?{|space|space.enemy?} && !warrior.look.any?{|space|space.captive?})
      warrior.shoot!
    elsif(@health > warrior.health)
      gogogo(warrior)
    elsif(warrior.health < 20)
      if(warrior.health < 10 && !@safe)
        @safe = true
        warrior.walk!(:backward)
      else
        warrior.rest!
      end
    else
      gogogo(warrior)
    end
    @health = warrior.health
  end
end

第10關…乾,掛掉是哪招,還是我已經破了X"DDDD
別人的教學都只寫到第九關…所以看來真的破了…

Orz"…我沒東西玩了…我一直以為它有99關之類的|||


#2

哈,好玩@@


#3
@health ||= warrior.health

這邊用到的||= 是什麼意思?


#4
@health = warrior.health unless @health

@系列變數不用宣告直接可用,而相同可用的有類似hash[:key] / @@之類的


#5
if(@health)
  @health = @health
else  
  @health = warrior.health
end

菜鳥我應該會寫成這樣,差好多 哈哈哈


#6

… Orz" … 你上面的 code 寫錯了啦 … 重來|||


#7
if(@health)
  @health
else  
   @health = warrior.health
end

安ㄋ?


#8

unless @health # 或 if !@health
  @health = warrior.health
end