Skip to content

Instantly share code, notes, and snippets.

@gig3m
Created August 18, 2012 17:33
Show Gist options
  • Save gig3m/3388609 to your computer and use it in GitHub Desktop.
Save gig3m/3388609 to your computer and use it in GitHub Desktop.
Player.rb file for Ruby Warrior, through level 6 beginner
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
@Edwin30
Copy link

Edwin30 commented Mar 11, 2016

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment