Skip to content

Instantly share code, notes, and snippets.

@bingxie
Created February 29, 2016 04:32
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 bingxie/2ae68d22698b22ac070f to your computer and use it in GitHub Desktop.
Save bingxie/2ae68d22698b22ac070f to your computer and use it in GitHub Desktop.
Ruby Warrior on bloc.io solution
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