Skip to content

Instantly share code, notes, and snippets.

@mattsnyder
Created March 6, 2014 13:17
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 mattsnyder/9389451 to your computer and use it in GitHub Desktop.
Save mattsnyder/9389451 to your computer and use it in GitHub Desktop.
Ruby Warrior Solutions Levels 6-9
class Strategy
def initialize(warrior, player)
@warrior = warrior
@player = player
end
def remember(memo)
player.remember memo
end
def recall(memo)
player.recall memo
end
def run
false
end
def player
@player
end
def warrior
@warrior
end
end
class Defensive < Strategy
def run
if warrior.feel(:backward).enemy?
warrior.attack!(:backward)
elsif warrior.feel.enemy?
warrior.attack!
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?
warrior.walk!(:backward)
else
return false
end
end
end
class Bravery < Strategy
def run
if player.losing_life? && warrior.health > 10
warrior.walk!
else
return false
end
end
end
class Curious < Strategy
def left_wall_undiscovered?
! recall(:wall_found)
end
def run
if left_wall_undiscovered? && warrior.feel(:backward).empty?
warrior.walk!(:backward)
elsif warrior.feel.empty?
remember(:wall_found)
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
[Defensive, 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 ||= []
@health_history << warrior.health
end
def losing_life?
return false if @health_history.size < 2
@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
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 Agressive < 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 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
[Agressive, 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
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
@mattsnyder
Copy link
Author

Level 8 and 9 solutions get the warrior to the stairs alive, however it does not rescue all captives. Would really like to make use of the look action to create a map of the level and make smarter decisions about what to do on a given turn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment