Created
December 18, 2016 13:21
-
-
Save sato-s/771b36fef877a5cec57dd321f7118105 to your computer and use it in GitHub Desktop.
ruby warrior player.rb
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 | |
DIRECTIONS = [:forward, :right, :backward, :left] | |
def play_turn(warrior) | |
@warrior = warrior | |
if feel_enemy? then | |
warrior.attack! enemy | |
elsif injured? then | |
warrior.rest! | |
else | |
warrior.walk! empty | |
end | |
end | |
def injured? | |
@warrior.health < 16 ? true :false | |
end | |
def enemy | |
DIRECTIONS.each do |d| | |
return d if @warrior.feel(d).enemy? | |
end | |
end | |
def empty | |
DIRECTIONS.each do |d| | |
return d if @warrior.feel(d).empty? | |
end | |
end | |
def feel_enemy? | |
spaces.any?{|s| s.enemy?} | |
end | |
def spaces | |
DIRECTIONS.map{|d| @warrior.feel d} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment