Created
July 28, 2013 20:12
-
-
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.
This file contains hidden or 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) | |
@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