Last active
December 20, 2015 18:48
-
-
Save dwaynemac/6178341 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Direction | |
attr_accessor :current | |
def initialize(dir = nil) | |
@current = dir | |
end | |
def current | |
@current ||= :forward | |
end | |
def inverse! | |
@current = opposite | |
end | |
def opposite | |
(current == :backward)? :forward : :backward | |
end | |
end | |
class Player | |
def play_turn(warrior) | |
@warrior = warrior | |
@direction ||= Direction.new(:backward) | |
space = warrior.feel(@direction.current) | |
if space.empty? | |
if under_attack? | |
warrior.walk!(@direction.opposite) | |
else | |
if need_rest? | |
warrior.rest! | |
else | |
warrior.walk!(@direction.current) | |
end | |
end | |
else | |
if space.enemy? | |
warrior.attack!(@direction.current) | |
elsif space.captive? | |
warrior.rescue!(@direction.current) | |
elsif space.wall? | |
@direction.inverse! | |
end | |
end | |
register_life_level | |
end | |
private | |
def register_life_level | |
@previous_health = @warrior.health | |
end | |
def under_attack? | |
return false if @previous_health.nil? | |
@warrior.health < @previous_health | |
end | |
def need_rest? | |
@warrior.health < 15 | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment