Created
July 31, 2013 08:51
-
-
Save johanek/6120466 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 | |
@health ||= warrior.health | |
@direction ||= :forward | |
attack || look || retreat || wall || captive || rest || walk | |
@health = warrior.health | |
end | |
def look | |
@warrior.shoot!(@direction) if @warrior.look(@direction)[2].enemy? | |
end | |
def walk | |
@warrior.walk!(@direction) | |
end | |
def attack | |
@warrior.attack! if @warrior.feel(@direction).enemy? | |
end | |
def rest | |
unless @health > @warrior.health | |
@warrior.rest! if @warrior.feel(@direction).empty? and @warrior.health < 20 | |
end | |
end | |
def captive | |
@warrior.rescue!(@direction) if @warrior.feel(@direction).captive? | |
end | |
def wall | |
if @warrior.feel(@direction).wall? | |
change_direction | |
@warrior.pivot! if @warrior.feel(:backward).empty? | |
end | |
end | |
def change_direction | |
if @direction == :backward | |
@direction = :forward | |
else | |
@direction = :backward | |
end | |
end | |
def retreat | |
if @health > @warrior.health and @warrior.feel(@direction).empty? and @warrior.health < 10 | |
@direction = :backward | |
walk | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment