Skip to content

Instantly share code, notes, and snippets.

@caingougou
Created July 31, 2013 08:48
Show Gist options
  • Save caingougou/6120451 to your computer and use it in GitHub Desktop.
Save caingougou/6120451 to your computer and use it in GitHub Desktop.
RubyWarrior
class Player
def initialize
@hero = nil
@health = 20
@danger = 10
@direction = :forward
end
def play_turn(warrior)
@hero = warrior
if in_battle?
if warrior.health > @danger
battle
else
escape
end
elsif warrior.health < 20
warrior.rest!
else
if warrior.feel(@direction).captive?
warrior.rescue! @direction
elsif warrior.feel(@direction).wall?
turn_around!
# warrior.walk! @direction
else
warrior.walk! @direction
end
end
@health = warrior.health
end
def in_battle?
if @hero.health < @health
return true
elsif @hero.feel(@direction).enemy?
return true
else
return false
end
end
def battle
if @hero.feel(@direction).enemy?
@hero.attack! @direction
elsif @hero.feel(@direction).captive?
@hero.rescue! @direction
elsif @hero.feel(@direction).wall?
turn_around!
@hero.walk! @direction
else
@hero.walk! @direction
end
end
def escape
# turn_around!
@hero.walk! :backward
end
def turn_around!
@hero.pivot!
# if @direction == :forward
# @direction = :backward
# else
# @direction = :forward
# end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment