Last active
December 20, 2015 14:39
-
-
Save sambao21/6148248 to your computer and use it in GitHub Desktop.
Ruby Warrior level 8 & 9 script. Doesn't work prior to level 8 because uses look and shoot!.
This file contains hidden or 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 | |
@direction = :forward | |
@keep_retreating = false | |
@health = 20 | |
end | |
def play_turn(warrior) | |
@warrior = warrior | |
if @warrior.feel(@direction).wall? | |
flip_direction! | |
else | |
if almost_dead? || keep_retreating? | |
retreat! | |
else | |
venture_forth! | |
end | |
end | |
@health = @warrior.health | |
end | |
def venture_forth! | |
#changing the order makes a big difference. | |
#Always attack first if you can | |
#And walk last | |
if @warrior.feel(@direction).enemy? | |
@warrior.attack!(@direction) | |
elsif enemy_in_the_distance? | |
@warrior.shoot! | |
elsif @warrior.feel(@direction).captive? | |
@warrior.rescue!(@direction) | |
elsif @warrior.feel(@direction).empty? | |
@warrior.walk!(@direction) | |
end | |
end | |
def enemy_in_the_distance? | |
spaces_ahead = @warrior.look(@direction) | |
@warrior.look(@direction)[spaces_ahead.length - 1].enemy? | |
end | |
def almost_dead? | |
@warrior.health < 8 | |
end | |
def retreat! | |
if @warrior.health < @health then @warrior.walk!(opposite_direction) else @warrior.rest! end | |
@keep_retreating = fully_healed? ? false : true | |
end | |
def fully_healed? | |
@warrior.health == 20 | |
end | |
def keep_retreating? | |
@keep_retreating | |
end | |
def opposite_direction | |
@direction == :forward ? :backward : :forward | |
end | |
def flip_direction! | |
@direction = opposite_direction | |
@warrior.pivot!(@direction) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment