Created
October 18, 2013 05:24
-
-
Save yasulab/7036854 to your computer and use it in GitHub Desktop.
One of answers to Ruby Warrior. RubyWarrior
https://www.bloc.io/ruby-warrior/
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) | |
if @health.nil? | |
# initialize if needed | |
end | |
if warrior.feel.empty? | |
if warrior.health < 20 | |
if @health <= warrior.health # not taking damage | |
warrior.look.each.with_index do |s,i| | |
if s.empty? | |
if i+1 < 3 | |
next | |
else | |
warrior.walk! | |
end | |
elsif s.enemy? | |
warrior.shoot! | |
break | |
else | |
warrior.rest! | |
break | |
end | |
end | |
else # taking damage | |
if warrior.health < 10 # dameged enough to escape | |
warrior.walk! :backward | |
else | |
warrior.walk! :forward | |
end | |
end | |
else # enough to act | |
spaces = warrior.look | |
spaces.each.with_index do |s,i| | |
if s.stairs? | |
warrior.walk! | |
break | |
elsif s.empty? | |
if i+1 < 3 | |
next | |
else | |
warrior.walk! | |
end | |
elsif s.captive? | |
warrior.walk! | |
break | |
elsif s.enemy? | |
warrior.shoot! | |
break | |
else | |
warrior.walk! | |
break | |
end | |
end | |
end | |
elsif warrior.feel.captive? | |
warrior.rescue! | |
elsif warrior.feel.wall? | |
warrior.pivot! | |
else | |
warrior.attack! | |
end | |
@health = warrior.health | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment