Last active
January 29, 2016 03:30
-
-
Save jnf/6198927 to your computer and use it in GitHub Desktop.
My code for level 9 of Ruby Warrior (https://www.bloc.io/ruby-warrior#/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Player | |
def play_turn(warrior) | |
@prev_health ||= 20 | |
@warrior = warrior | |
@space = @warrior.feel | |
feel_it_out | |
@prev_health = warrior.health | |
end | |
protected | |
def feel_it_out | |
return about_face if @space.wall? | |
return be_a_hero if @space.captive? | |
return strike if @space.enemy? | |
return ddos if @warrior.look[2].enemy? | |
return reconsider if @warrior.health < @prev_health | |
return sleep_or_move if @space.empty? | |
end | |
def sleep_or_move | |
(@warrior.health < 20) ? sleep_it_off : move | |
end | |
def sleep_it_off | |
@warrior.rest! | |
end | |
def strike | |
@warrior.attack! | |
end | |
def ddos | |
@warrior.shoot! | |
end | |
def move(direction = :forward) | |
@warrior.walk! direction | |
end | |
def be_a_hero | |
@warrior.rescue! | |
end | |
def about_face | |
@warrior.pivot! | |
end | |
def reconsider | |
move(@warrior.health < 10 ? :backward : :forward) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice! Thanks for sharing. I was completely stumped