Last active
August 29, 2015 14:22
-
-
Save britg/91ebaa74af16e8819150 to your computer and use it in GitHub Desktop.
ruby warrior level
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 | |
@health = 20 | |
@clear_behind = false | |
@wounded = false | |
end | |
def play_turn(warrior) | |
warrior_wounded?(warrior) | |
if @clear_behind | |
explore_forward(warrior) | |
else | |
explore_backward(warrior) | |
end | |
@health = warrior.health | |
end | |
def explore_backward(warrior) | |
if at_back_wall?(warrior) | |
@clear_behind = true | |
explore_forward(warrior) | |
else | |
warrior_act(warrior,:backward) | |
end | |
end | |
def explore_forward(warrior) | |
if @wounded | |
retreat(warrior) | |
else | |
warrior_act(warrior,:forward) | |
end | |
end | |
def warrior_act(warrior,direction) | |
if warrior.feel(direction).captive? | |
warrior.rescue!(direction) | |
elsif warrior.feel(direction).enemy? | |
warrior.attack!(direction) | |
else | |
warrior.walk!(direction) | |
end | |
end | |
def at_back_wall?(warrior) | |
warrior.feel(:backward).wall? | |
end | |
def being_attacked?(warrior) | |
@health > warrior.health | |
end | |
def retreat(warrior) | |
if being_attacked?(warrior) | |
warrior.walk!(:backward) | |
else | |
heal(warrior) | |
end | |
end | |
def warrior_wounded?(warrior) | |
@wounded = true if warrior.health < 10 | |
end | |
def heal(warrior) | |
if warrior.health < 20 | |
warrior.rest! | |
else | |
@wounded = false | |
end | |
end | |
end |
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 | |
@previous_health = 20 | |
end | |
def play_turn(warrior) | |
@warrior = warrior | |
if wounded? | |
handle_wounded! | |
elsif at_back_wall? | |
explore_forward! | |
else | |
explore_backward! | |
end | |
@previous_health = warrior.health | |
end | |
def handle_wounded! | |
if being_attacked? | |
retreat! | |
else | |
heal! | |
end | |
end | |
def explore_forward! | |
warrior_act(:forward) | |
end | |
def explore_backward! | |
warrior_act(:backward) | |
end | |
def warrior_act(direction) | |
if @warrior.feel(direction).captive? | |
@warrior.rescue!(direction) | |
elsif @warrior.feel(direction).enemy? | |
@warrior.attack!(direction) | |
else | |
@warrior.walk!(direction) | |
end | |
end | |
def at_back_wall? | |
@warrior.feel(:backward).wall? | |
end | |
def being_attacked? | |
@previous_health > @warrior.health | |
end | |
def wounded? | |
@warrior.health < 10 | |
end | |
def retreat! | |
@warrior.walk!(:backward) | |
end | |
def heal! | |
@warrior.rest! | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment