Created
February 29, 2016 04:32
-
-
Save bingxie/2ae68d22698b22ac070f to your computer and use it in GitHub Desktop.
Ruby Warrior on bloc.io solution
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 | |
REST_HEALTH = 15 | |
FLEE_HEALTH = 7 | |
def play_turn(warrior) | |
@last_known_health ||= warrior.health | |
@direction ||= :forward | |
feel_space = warrior.feel @direction | |
if clear_shot?(warrior) | |
warrior.shoot! | |
elsif feel_space.empty? | |
if should_flee?(warrior) | |
@direction = :backward | |
warrior.walk! @direction | |
elsif should_rest?(warrior) | |
warrior.rest! | |
else | |
warrior.walk! @direction | |
end | |
elsif feel_space.captive? | |
warrior.rescue! @direction | |
elsif feel_space.wall? | |
@direction = :forward | |
warrior.pivot! | |
else | |
warrior.attack! | |
end | |
@last_known_health = warrior.health | |
end | |
private | |
def clear_shot? warrior | |
glance = warrior.look @direction | |
result = glance.find { |space| space.captive? || space.enemy? } | |
result && result.enemy? && safe?(warrior) | |
end | |
def should_flee?(warrior) | |
bad_health = warrior.health < FLEE_HEALTH | |
!safe?(warrior) && bad_health | |
end | |
def should_rest?(warrior) | |
bad_health = warrior.health < REST_HEALTH | |
safe?(warrior) && bad_health | |
end | |
def safe? warrior | |
warrior.health >= @last_known_health | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment