Rubywarrior 4级(清理我的代码帮助)(Rubywarrior Level 4 (clea

2019-09-17 05:16发布

我通过红宝石学习编程,我发现了真棒Rubywarrior由Railscasts瑞恩·贝茨。 不幸的是,我停留在我的代码被扔了语法错误消息($意外结束)。

我不要求的答案,我想那种出自己,但如果有人能指出哪里我的代码是从得到的错误,这将是超级。 谢谢!

class Player

  def initialize
    @maxhealth = 20
    @dying = 7
    @previoushealth = @maxhealth
    @health = warrior.health
    @warrior = warrior
  end

  def play_turn(warrior)
  # If there are no enemies, rest until health is 100%
    turn_start_check(warrior)
    actions(warrior)
    turn_end_check(warrior)
  end

  def actions(warrior)
    if @damaged_since_last_turn
      warrior.shoot!
    elsif
      @health < @maxhealth
        warrior.rest!
    else
      warrior.walk!
    end
  end

  def hurt?(warrior)
    warrior.health < 20
  end

  def healthy?(warrior)
    warrior.health = 20
  end

  def alone?(warrior)
    warrior.feel.empty?
  end

  def should_i_move?(warrior)
    if healthy? and alone?
      warrior.rest!
    else
      warrior.walk!
  end

  # Put code here for if health from previous turn is less than last term
  # If true don't rest and keep moving forward
  def turn_start_check(warrior)
    @damaged_since_last_turn = @previoushealth > warrior.health
  end

  def turn_end_check(warrior)
    @previoushealth = warrior.health
  end
end

Answer 1:

我猜:

def should_i_move?(warrior)
  if healthy? and alone?
    warrior.rest!
  else
    warrior.walk!
  end  # <<MISSING THIS ONE
end


Answer 2:

该错误消息意味着你缺少一个end的地方关键字。 检查你的代码,看看是否所有的语句都正确写入。



Answer 3:

使用Ruby 1.9.3,如果打开警告,您会收到以下警告:

-:46: warning: mismatched indentations at 'end' with 'if' at 42
-:57: warning: mismatched indentations at 'end' with 'def' at 41

然后错误

-:57: syntax error, unexpected $end, expecting keyword_end

线46对应于第一enddef should_i_move?(warrior)

这应该与早期版本的Ruby 1.9的正常工作。



文章来源: Rubywarrior Level 4 (cleaning up my code help)