Skip to content

Instantly share code, notes, and snippets.

@zuk
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zuk/26d71eeab266490eb33f to your computer and use it in GitHub Desktop.
Save zuk/26d71eeab266490eb33f to your computer and use it in GitHub Desktop.
Ruby Warrior (Beginner)
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