Created
August 18, 2012 17:33
-
-
Save gig3m/3388609 to your computer and use it in GitHub Desktop.
Player.rb file for Ruby Warrior, through level 6 beginner
This file contains hidden or 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
DEBUG=true | |
class Player | |
def initialize | |
@last_health = 20 | |
@max_health = 20 | |
@dying_health = 8 | |
@direction = :forward #initial direction | |
@retreating = 0 | |
end | |
def play_turn(warrior) | |
@warrior = warrior | |
puts "Last Health: #{@last_health}" | |
puts "Current Health: #{warrior.health}" | |
if @retreating != 0 | |
puts "Retreating: #{@retreating}" | |
end | |
if retreated? && warrior.health < 20 | |
warrior.rest! | |
elsif retreated? && warrior.health == 20 | |
@retreating = 0 | |
switch_direction | |
warrior.walk!(@direction) | |
elsif retreating? | |
warrior.walk!(@direction) | |
@retreating = @retreating - 1 | |
elsif taking_damage? | |
if dying? | |
switch_direction | |
@retreating = 3 | |
warrior.walk!(@direction) | |
elsif space? | |
warrior.walk!(@direction) | |
elsif wall? | |
turn_around | |
elsif enemy? | |
warrior.attack!(@direction) | |
elsif captive? | |
warrior.rescue!(@direction) | |
else | |
warrior.walk!(@direction) | |
end | |
else # not taking damage | |
if hurt? | |
puts "Resting" | |
warrior.rest! | |
elsif space? | |
warrior.walk!(@direction) | |
elsif captive? | |
warrior.rescue!(@direction) | |
elsif enemy? | |
warrior.attack!(@direction) | |
elsif wall? | |
turn_around | |
end | |
end | |
update_health | |
end | |
#actions | |
def space? | |
@warrior.feel(@direction).empty? | |
end | |
def enemy? | |
@warrior.feel(@direction).enemy? | |
end | |
def captive? | |
@warrior.feel(@direction).captive? | |
end | |
def wall? | |
@warrior.feel(@direction).wall? | |
end | |
#status | |
def update_health | |
@last_health = @warrior.health | |
end | |
def taking_damage? | |
@last_health > @warrior.health | |
end | |
def hurt? | |
@warrior.health < @max_health | |
end | |
def dying? | |
@warrior.health < @dying_health | |
end | |
def retreating? | |
@retreating > 0 | |
end | |
def retreated? | |
@retreating == 1 | |
end | |
#utility | |
def switch_direction | |
if @direction == :backward | |
@direction = :forward | |
elsif @direction == :forward | |
@direction = :backward | |
end | |
end | |
def turn_around | |
# if @direction == :backward | |
# @direction = :forward | |
# elsif @direction == :forward | |
# @direction = :backward | |
# end | |
@warrior.pivot! | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!