Skip to content

Instantly share code, notes, and snippets.

@emaraschio
Last active June 1, 2017 20:52
Show Gist options
  • Save emaraschio/7d19fe2f92f0e64277c7 to your computer and use it in GitHub Desktop.
Save emaraschio/7d19fe2f92f0e64277c7 to your computer and use it in GitHub Desktop.
Ruby Warrior Game
class Player
def play_turn(warrior)
@warrior = warrior
@health ||= warrior.health
@direction ||= :forward
warrior_feel = @warrior.feel @direction
if enemy_ahead?
@warrior.shoot!
elsif warrior_feel.empty?
if should_flee?
@direction = :backward
@warrior.walk! @direction
elsif need_replenish?
@warrior.rest!
else
@warrior.walk! @direction
end
elsif warrior_feel.captive?
@warrior.rescue! @direction
elsif warrior_feel.wall?
@direction = :forward
@warrior.pivot!
else
@warrior.attack!
end
@health = @warrior.health
end
def should_flee?
!is_safe? && @warrior.health < 6
end
def need_replenish?
is_safe? && @warrior.health < 14
end
def is_safe?
@warrior.health >= @health
end
def enemy_ahead?
warrior_look = @warrior.look(@direction)
steps_to_enemy = warrior_look.index { |space| space.enemy? == true } || 5
steps_to_captive = warrior_look.index { |space| space.captive? == true } || 5
is_safe? && steps_to_enemy < steps_to_captive
end
end
emaraschio walks forward
CONGRATULATIONS! You have climbed to the top of the tower and rescued the fair maiden Ruby.
Level Score: 74
Time Bonus: 4
Clear Bonus: 16
Level Grade: A
Total Score: 484 + 94 = 578
Your average grade for this tower is: A
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment