Created
March 6, 2014 02:21
-
-
Save mattsnyder/9381017 to your computer and use it in GitHub Desktop.
Ruby Warrior Level 8 (kills captive)(works on 9 too)
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 Strategy | |
def initialize(warrior, player) | |
@warrior = warrior | |
@player = player | |
end | |
def run | |
false | |
end | |
def player | |
@player | |
end | |
def warrior | |
@warrior | |
end | |
end | |
class HandToHandCombat < Strategy | |
def run | |
if player.losing_life? && player.close_to_death? | |
return false | |
elsif warrior.feel(:backward).enemy? | |
player.remember(:monster_backward) | |
warrior.attack!(:backward) | |
elsif warrior.feel.enemy? | |
player.remember(:monster_forward) | |
warrior.attack! | |
else | |
return false | |
end | |
end | |
end | |
class RangedCombat < Strategy | |
def run | |
if player.losing_life? && player.close_to_death? | |
return false | |
elsif warrior.look(:backward).map{|s|s.enemy?}.include?(true) | |
player.remember(:monster_backward) | |
warrior.shoot!(:backward) | |
elsif warrior.look.map{|s| s.enemy?}.include?(true) | |
player.remember(:monster_forward) | |
warrior.shoot! | |
else | |
return false | |
end | |
end | |
end | |
class Helpful < Strategy | |
def run | |
if warrior.feel(:backward).captive? | |
warrior.rescue!(:backward) | |
elsif warrior.feel.captive? | |
warrior.rescue! | |
else | |
return false | |
end | |
end | |
end | |
class Cowardly < Strategy | |
def run | |
if warrior.health < 20 && player.losing_life? | |
if player.recall(:monster_forward) | |
warrior.walk!(:backward) | |
else | |
warrior.walk! | |
end | |
else | |
return false | |
end | |
end | |
end | |
class Bravery < Strategy | |
def run | |
if player.losing_life? and not player.close_to_death? | |
if player.recall(:monster_forward) | |
warrior.walk! | |
else | |
warrior.walk!(:backward) | |
end | |
else | |
return false | |
end | |
end | |
end | |
class Curious < Strategy | |
def left_wall_undiscovered? | |
! player.recall(:wall_found_backward) | |
end | |
def run | |
if left_wall_undiscovered? && warrior.feel(:backward).empty? | |
warrior.walk!(:backward) | |
elsif warrior.feel.empty? | |
player.remember(:wall_found_backward) | |
warrior.walk! | |
else | |
return false | |
end | |
end | |
end | |
class Restful < Strategy | |
def safe_place_to_rest? | |
! player.losing_life? | |
end | |
def run | |
if warrior.health < 20 && safe_place_to_rest? | |
warrior.rest! | |
else | |
return false | |
end | |
end | |
end | |
class Player | |
def strategies | |
[RangedCombat, HandToHandCombat, Helpful, Restful, Bravery, Cowardly, Curious] | |
end | |
def memories | |
@memories ||= [] | |
end | |
def remember(memo) | |
memories << memo | |
end | |
def recall(memo) | |
memories.include? memo | |
end | |
def monitor_health(warrior) | |
@health_history ||= [20,20] | |
@health_history << warrior.health | |
end | |
def close_to_death? | |
@health_history.last < 10 | |
end | |
def losing_life? | |
@health_history[-1] < @health_history[-2] | |
end | |
def play_turn(warrior) | |
monitor_health(warrior) | |
strategies.each do |strategy| | |
return if strategy.new(warrior, self).run | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment