Created
September 26, 2013 11:19
-
-
Save joshuapaling/6712817 to your computer and use it in GitHub Desktop.
ruby warrior beginner level 6
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 initialize | |
@max_health = 20 | |
@prev_health = @max_health | |
@rescued_behind = false | |
@half_health = @max_health / 2 | |
end | |
def play_turn(warrior) | |
@warrior = warrior | |
if !@rescued_behind | |
rescue_captive_behind | |
else | |
if @warrior.feel.empty? | |
handle_empty_space_ahead | |
else | |
# handle not empty space ahead | |
if @warrior.feel.captive? | |
@warrior.rescue! | |
else | |
@warrior.attack! | |
end | |
end | |
end | |
@prev_health = @warrior.health | |
end | |
def handle_empty_space_ahead | |
if max_health? | |
@warrior.walk! | |
elsif taking_damage? | |
if below_half_health? | |
@warrior.walk! :backward | |
else | |
@warrior.walk! | |
end | |
else | |
@warrior.rest! | |
end | |
end | |
def rescue_captive_behind | |
if @warrior.feel(:backward).captive? | |
@warrior.rescue! :backward | |
@rescued_behind = true | |
else | |
if @rescued_behind | |
@warrior.walk! | |
else | |
@warrior.walk! :backward | |
end | |
end | |
end | |
def max_health? | |
@warrior.health == @max_health | |
end | |
def below_half_health? | |
@warrior.health < @half_health | |
end | |
def taking_damage? | |
@warrior.health < @prev_health | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment