Skip to content

Instantly share code, notes, and snippets.

@lopopolo
Created August 15, 2013 06:45
Show Gist options
  • Save lopopolo/6238776 to your computer and use it in GitHub Desktop.
Save lopopolo/6238776 to your computer and use it in GitHub Desktop.
hacking at the ruby warrior game.
class Player
def max_health; 20; end;
def healing_threshold; 10; end
def initialize
@healing = false
@retreating = false
@last_action = nil
@last_health = 20
end
def should_heal?(warrior)
warrior.health < healing_threshold
end
def taking_damage?(warrior)
warrior.health < @last_health
end
def retreat?(warrior)
if taking_damage?(warrior) && warrior.feel.empty?
@retreating = false
elsif (@retreating || @healing) && taking_damage?(warrior)
@retreating = true
elsif should_heal?(warrior) && !warrior.feel.enemy?
@retreating = true
else
@retreating = false
end
@retreating
end
def rest?(warrior)
if taking_damage?(warrior) && (warrior.feel.empty? || warrior.feel.enemy?)
@healing = false
elsif should_heal?(warrior) ||
(@last_action == :attack && warrior.feel.empty?)
@healing = true
elsif @healing
@healing = false if warrior.health == max_health
else
@healing = false
end
@healing
end
def free_captive?(warrior)
warrior.feel.captive?
end
def move_or_attack!(warrior)
if warrior.feel.empty?
warrior.walk!
@last_action = :walk
elsif warrior.feel.enemy?
warrior.attack!
@last_action = :attack
end
end
def play_turn(warrior)
# cool code goes here
if retreat?(warrior)
warrior.walk! :backward
@last_action = :walk
elsif rest?(warrior)
warrior.rest!
@last_action = :rest
elsif free_captive?(warrior)
warrior.rescue!
@last_action = :rescue
else
move_or_attack!(warrior)
end
@last_health = warrior.health
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment