Skip to content

Instantly share code, notes, and snippets.

@mattsnyder
Created March 6, 2014 01:56
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/9380681 to your computer and use it in GitHub Desktop.
Save mattsnyder/9380681 to your computer and use it in GitHub Desktop.
Ruby Warrior Level 6
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment