Last active
June 1, 2017 20:52
-
-
Save emaraschio/7d19fe2f92f0e64277c7 to your computer and use it in GitHub Desktop.
Ruby Warrior Game
This file contains 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 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 |
This file contains 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
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