Created
July 29, 2013 15:42
-
-
Save Jimgerneer/6105276 to your computer and use it in GitHub Desktop.
Ruby Warrior
This file contains 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 Player | |
def play_turn(warrior) | |
@warrior = warrior | |
@direction ||= :backward | |
@health ||= warrior.health | |
@hurt ||= false | |
@step ||= true | |
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) | |
@step = false | |
end | |
end | |
def switch_direction | |
if @direction == :forward | |
@direction = :backward | |
else | |
@direction = :forward | |
end | |
end | |
def wall_found? | |
@warrior.feel(@direction).wall? | |
end | |
def wizard_found? | |
@warrior.look(@direction) | |
end | |
def rescue_them(direction) | |
@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) | |
@health = @warrior.health | |
@hurt = true | |
if direction == :forward | |
@warrior.walk!(:backward) | |
else | |
@warrior.walk!(:forward) | |
end | |
end | |
def heal | |
unless @warrior.health == 20 | |
@warrior.rest! | |
else | |
@hurt = false | |
end | |
end | |
def ranged_attack(direction) | |
@warrior.look | |
@warrior.shoot!(direction) | |
@step = true | |
end | |
def attack(direction) | |
if enemy_found? | |
@warrior.attack!(direction) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment