Skip to content

Instantly share code, notes, and snippets.

@carpeliam
Created July 28, 2013 20:12
Show Gist options
  • Save carpeliam/6099991 to your computer and use it in GitHub Desktop.
Save carpeliam/6099991 to your computer and use it in GitHub Desktop.
My code for the final level at https://www.bloc.io/ruby-warrior. Could have been more efficient (pivot earlier when the closest object is a wall, don't bother healing when there are no enemies left) but I'm happy enough with it.
class Player
def play_turn(warrior)
@health ||= warrior.health
taking_damage = warrior.health < @health
objects = warrior.look
closest_enemy = objects.detect { |s| s.enemy? }
closest_captive = objects.detect { |s| s.captive? }
enemy_is_closer = closest_captive && closest_enemy && objects.index(closest_enemy) < objects.index(closest_captive)
if !taking_damage && (!closest_captive || enemy_is_closer) && closest_enemy && objects.index(closest_enemy) < 3
warrior.shoot!
else
space = warrior.feel
if space.wall?
warrior.pivot!
elsif space.captive?
warrior.rescue!
elsif space.empty?
if taking_damage
if warrior.health < 10
warrior.walk!(:backward)
else
warrior.walk!
end
elsif warrior.health < 20
warrior.rest!
else
warrior.walk!
end
elsif space.enemy?
warrior.attack!
end
@health = warrior.health
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment