Skip to content

Instantly share code, notes, and snippets.

@timoteoponce
Created October 3, 2014 19:36
Show Gist options
  • Save timoteoponce/73f548d55a93ca085f31 to your computer and use it in GitHub Desktop.
Save timoteoponce/73f548d55a93ca085f31 to your computer and use it in GitHub Desktop.
ruby warrior solution-beginner
class Player
def initialize
@direction = :forward
end
def feel
return @warrior.feel @direction
end
def look
spaces = @warrior.look @direction
spaces.each do |space|
return space if space.captive?
end
return spaces[2]
end
def walk
if feel.wall? then
change_direction
@warrior.pivot! :backward if @direction == :forward
@warrior.pivot! :forward if @direction == :backward
else
@warrior.walk! @direction
end
end
def walk_back
@warrior.walk! :forward if @direction == :backward
@warrior.walk! :backward if @direction == :forward
end
def rescue_captive
@warrior.rescue! @direction
end
def change_direction
@direction = :backward if @direction == :forward
@direction = :forward if @direction == :backward
end
def play_turn(warrior)
@warrior = warrior
if feel.enemy? then
warrior.attack! @direction
elsif look.enemy?
warrior.shoot! @direction
else
walk_or_rest
end
@health = warrior.health
end
def walk_or_rest
if @warrior.health < 14 && taking_damage then
walk_back
elsif @warrior.health < 20 && taking_damage
walk
elsif @warrior.health < 20
@warrior.rest!
elsif feel.captive?
rescue_captive
else
walk
end
end
def taking_damage
@warrior.health < @health
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment