Last active
August 29, 2015 14:19
-
-
Save zuk/26d71eeab266490eb33f to your computer and use it in GitHub Desktop.
Ruby Warrior (Beginner)
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 | |
DIRS = [:forward, :backward] | |
def initialize | |
@dir = :forward | |
end | |
def play_turn(warrior) | |
@warrior = warrior | |
space = warrior.feel(@dir) | |
if space.wall? | |
change_dir! | |
warrior.walk!(@dir) | |
elsif space.captive? | |
warrior.rescue!(@dir) | |
elsif space.empty? | |
if lost_health_twice_in_a_row? | |
change_dir! | |
warrior.walk!(@dir) | |
elsif warrior.health < 20 | |
warrior.rest! | |
else | |
warrior.walk!(@dir) | |
end | |
else | |
warrior.attack!(@dir) | |
end | |
@lost_health_in_prev_turn = losing_health? | |
@prev_health = warrior.health | |
end | |
def losing_health? | |
@prev_health && @prev_health > @warrior.health | |
end | |
def lost_health_twice_in_a_row? | |
@lost_health_in_prev_turn && losing_health? | |
end | |
def change_dir! | |
@dir = @dir == :forward ? :backward : :forward | |
end | |
def random_change_dir! | |
@dir = DIRS[rand(DIRS.length)] | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment