Skip to content

Instantly share code, notes, and snippets.

@seeflanigan
Forked from Jimgerneer/gist:6105276
Last active December 20, 2015 09:09
Show Gist options
  • Save seeflanigan/6105484 to your computer and use it in GitHub Desktop.
Save seeflanigan/6105484 to your computer and use it in GitHub Desktop.
class Player
attr_accessor :warrior, :direction, :health, :hurt, :step
def initialize
self.direction = :backward
self.hurt = false
self.step = true
end
def play_turn(warrior)
self.warrior = warrior
self.health = warrior.health
explore(direction)
end
def explore(direction)
case
when under_attack? && health_low?
survive(direction)
when hurt?
heal
when under_attack?
attack(direction)
when captive_found?
rescue_them(direction)
when enemy_found?
attack(direction)
when wall_found?
switch_direction
when step?
step(direction)
else
ranged_attack(direction)
end
end
def step(direction)
if warrior.feel(direction).empty?
warrior.walk!(direction)
self.step = false
end
end
def switch_direction
self.direction = direction.eql?(:forward) ? :backward : direction
end
def wall_found?
warrior.feel(direction).wall?
end
def wizard_found?
warrior.look(direction)
end
def rescue_them(direction)
self.step = false
warrior.rescue!(direction)
end
def captive_found?
warrior.feel(direction).captive?
end
def enemy_found?
warrior.feel(direction).enemy?
end
def health_low?
true if warrior.health < 12
end
def hurt?
hurt == true
end
def step?
step == true
end
def under_attack?
true if warrior.health != health
end
def survive(direction)
self.health = warrior.health
self.hurt = true
direction.eql?(:forward) ? warrior.walk!(:backward) : warrior.walk!(:forward)
end
def heal
warrior.health.eql?(20) ? warrior.rest! : self.hurt = false
end
def ranged_attack(direction)
warrior.look
warrior.shoot!(direction)
self.step = true
end
def attack(direction)
warrior.attack!(direction) if enemy_found?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment