Skip to content

Instantly share code, notes, and snippets.

@briancicutti
Created July 2, 2012 19:29
Show Gist options
  • Save briancicutti/3035155 to your computer and use it in GitHub Desktop.
Save briancicutti/3035155 to your computer and use it in GitHub Desktop.
Ruby Warrior Beginner Level 4 Solution
class Player
def play_turn(warrior)
if warrior.feel.enemy?
warrior.attack!
else
if warrior.health < 20 && !taking_damage?(warrior)
warrior.rest!
else
warrior.walk!
end
end
@health = warrior.health
end
def taking_damage?(warrior)
warrior.health < @health
end
end
@marcamillion
Copy link

So I am a bit confused with something. This is my solution: https://gist.github.com/marcamillion/0f0677f012e82fc75b77

For the most part it works, but when I move to level 5, it gives me a Fixnum can't be compared to nil error. That is because on the first iteration through, @health hasn't been set before taking_damage has been run the first time.

However, when I run yours, I never get that issue - even though both of us declare @health at the same place and in the same way.

What am I missing?

Btw, I fixed it by doing this at the top of my play_turn method:

if @health.nil?
   @health = warrior.health
end

I could have just created an 'initialized health` method also, but I just did it like this.

Thoughts?

@jastuccio
Copy link

I found some code here:
https://github.com/foogoof/ruby-warrior-solutions/blob/master/rubywarrior/level-4-beginner/player.rb

inserting this line into my code got it working for me
@last_known_health = warrior.health unless @last_known_health

class Player
  def play_turn(warrior)
    @health = warrior.health unless @health

    #2 options when the space is empty
    if warrior.feel.empty? 
      #option 1 rest if health < 20 and we are not under attack
      if warrior.health < 20 && warrior.health >= @health
        warrior.rest!
     #otherwise walk
      else warrior.walk! 
      end
    #if the space is not empty we attack  
    else warrior.attack! 
    end

    @health = warrior.health
  end #play_turn
end #class

@sdotson01
Copy link

@jastuccio's code worked for me

@DVORAKIwasIhere
Copy link

I found some code here:
https://github.com/foogoof/ruby-warrior-solutions/blob/master/rubywarrior/level-4-beginner/player.rb

inserting this line into my code got it working for me
@last_known_health = warrior.health unless @last_known_health

class Player
  def play_turn(warrior)
    @health = warrior.health unless @health

    #2 options when the space is empty
    if warrior.feel.empty? 
      #option 1 rest if health < 20 and we are not under attack
      if warrior.health < 20 && warrior.health >= @health
        warrior.rest!
     #otherwise walk
      else warrior.walk! 
      end
    #if the space is not empty we attack  
    else warrior.attack! 
    end

    @health = warrior.health
  end #play_turn
end #class

@health = warrior.health unless @health // useless part of code, maybe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment