Ruby Warrior —— 用游戏学习Ruby (附全部关卡代码)_ruby


游戏地址:

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


今天发现了这个好玩的游戏,一下午通关。

BGM很洗脑,嗯。


Level 1

# 方法调用
class Player
  def play_turn(warrior)
    warrior.walk!
  end
end


Level 2


# if-else表达式
class Player
  def play_turn(warrior)
    if warrior.feel.enemy?
      warrior.attack!
    else
      warrior.walk!
    end
  end
end


Level 3

# if-elsif-else表达式
# 注意play_turn是每回合执行一次,所以先战斗,再回满血,再前进
class Player
  def play_turn(warrior)
    if warrior.feel.enemy?
      warrior.attack!
    elsif warrior.health < 20
      warrior.rest!
    else
      warrior.walk!
    end
  end
end


Level 4

# 注意到每回合是先执行play_turn再执行enemy_turn,
# 所以先用一个字符串@lastHP来存储warrior进行一次行动后的血量,
# 若下一回合发现血量减少,则在enemy_turn warrior必定受到了伤害
class Player
  def play_turn(warrior)
    if warrior.feel.enemy?
      warrior.attack!
    elsif warrior.health < @lastHP.to_i
      warrior.walk!
    elsif warrior.health < 20
      warrior.rest!
    else
      warrior.walk!
    end
    @lastHP = warrior.health
  end
end

Level 5

# 先杀怪,再救人,在上一关的基础上加一句话即可
class Player
  def play_turn(warrior)
    if warrior.feel.enemy?
      warrior.attack!
    elsif warrior.health < @lastHP.to_i
      warrior.walk!
    elsif warrior.health < 20
      warrior.rest!
    elsif warrior.feel.captive?
      warrior.rescue!
    else
      warrior.walk!
    end
    @lastHP = warrior.health
  end
end


Level 6


# 先救人,杀一个怪后回到最左休息,然后一路向右
class Player
  def play_turn(warrior)
    if @ok
      if warrior.feel.enemy?
        warrior.attack!
        @attack=true # kill a monster
      else
        if @attack and !@fullHP and !(warrior.feel:backward).wall?
          warrior.walk!:backward # back to left to rest
        elsif (warrior.feel:backward).wall? and warrior.health < 20
          warrior.rest!
          @fullHP = true
        else
          warrior.walk!
        end
      end
    else
      if (warrior.feel:backward).captive?
        warrior.rescue!:backward
        @ok = true
      else
        warrior.walk!:backward
      end 
    end
  end
end


Level 7


#在上一关的基础上转个身即可
class Player
  def play_turn(warrior)
    if warrior.feel.wall?
      warrior.pivot!
    else
      if warrior.feel.enemy?
        warrior.attack!
        @attack=true # kill a monster
      else
        if @attack and !@fullHP and !(warrior.feel:backward).wall?
          warrior.walk!:backward # back to left to rest
        elsif (warrior.feel:backward).wall? and warrior.health < 20
          warrior.rest!
          @fullHP = true
        else
          warrior.walk!
        end
      end
    end
  end
end


Level 8


# 可以使用数组了
# 先救人,在射箭
class Player
  def play_turn(warrior)
	if @ok
	  if warrior.look[0].enemy? or warrior.look[1].enemy? or warrior.look[2].enemy?
		warrior.shoot!
      else
        warrior.walk!
	  end
	else
	  if warrior.feel.captive?
        warrior.rescue!
		@ok = true
      else
        warrior.walk!
	  end
    end
  end
end


Level 9


# 转身射怪,转身射两怪,救人,转身,救人
# 使用case-when实现
class Player
  def play_turn(warrior)
    case @cnt.to_i
    when 0
	  warrior.pivot!
	  @cnt = 1
    when 1
	  if warrior.look[0].enemy? or warrior.look[1].enemy? or warrior.look[2].enemy?
		warrior.shoot!
	  else
	    @cnt = 2
	  end
	when 2
	  warrior.pivot!
	  @cnt = 3
	when 3
	  if warrior.look[0].enemy? or warrior.look[1].enemy? or warrior.look[2].enemy?
		warrior.shoot!
	  elsif warrior.feel.captive?
	    warrior.rescue!
		@cnt = 4
	  else
	    warrior.walk!
	  end
	when 4
	  warrior.pivot!
	  @cnt = 5
	when 5
	  if warrior.feel.captive?
	    warrior.rescue!
	  else
	    warrior.walk!
	  end
	end
  end
end


貌似还有后续关卡。。。

https://github.com/ryanb/ruby-warrior

使用方法见

http://qichunren.iteye.com/blog/383938