Skip to content

Instantly share code, notes, and snippets.

@sambao21
Created August 3, 2013 21:46
Show Gist options
  • Save sambao21/6148099 to your computer and use it in GitHub Desktop.
Save sambao21/6148099 to your computer and use it in GitHub Desktop.
This will complete Ruby Warrior levels 3-7. Won't run on level 1 and 2 because those didn't introduce certain methods yet. Didn't think to save those 2.
class Player
def initialize
@direction = :forward
@keep_retreating = false
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!
if @warrior.feel(@direction).empty?
@warrior.walk!(@direction)
elsif @warrior.feel(@direction).enemy?
@warrior.attack!(@direction)
elsif @warrior.feel(@direction).captive?
@warrior.rescue!(@direction)
end
end
def almost_dead?
@warrior.health < 9
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