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

时间:2012-05-14 15:27:04

标签: ruby code-cleanup

我正在通过Ruby学习编程,我从Railscasts的Ryan Bates那里找到了令人敬畏的Rubywarrior。不幸的是,我被困在我的代码中抛出了语法错误消息(意外的$ end)。

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

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

3 个答案:

答案 0 :(得分:4)

我的猜测:

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

答案 1 :(得分:4)

该错误消息表示您在某处遗漏了end个关键字。检查您的代码,看看您的所有陈述是否都写得正确。

答案 2 :(得分:1)

使用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行对应end

之后的第一个def should_i_move?(warrior)

这也适用于早期版本的Ruby 1.9。