Skip to content

Instantly share code, notes, and snippets.

@hamakn
Created July 28, 2013 06:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamakn/6097625 to your computer and use it in GitHub Desktop.
Save hamakn/6097625 to your computer and use it in GitHub Desktop.
class Player
def initialize
@step = :forward
@health = 20
@max_health = 20
@last_health = 20
@touch_wall = false
end
def play_turn(warrior)
manage_health(warrior)
spaces_forward = warrior.look @step
spaces_backward = warrior.look opposite
space = spaces_forward[0]
if spaces_backward[2].enemy?
warrior.pivot!
elsif spaces_forward[2].enemy? || spaces_forward[1].enemy?
warrior.shoot! @step
elsif space.enemy?
warrior.attack! @step
elsif space.captive?
warrior.rescue! @step
elsif space.wall?
@touch_wall = true
warrior.pivot!
elsif space.stairs?
unless @touch_wall
warrior.pivot!
else
warrior.walk!
end
elsif !here_is_safety?
if @health > 10
warrior.walk! @step
else
# run away!!
warrior.walk! opposite
end
elsif health_is_full?
warrior.walk! @step
else
warrior.rest!
end
end
def opposite
@step == :forward ? :backward : :forward
end
def manage_health(warrior)
@last_health = @health
current_health = warrior.health
@max_health = 20
#@max_health = current_health if current_health > @max_health
@health = current_health
end
def here_is_safety?
@last_health <= @health
end
def health_is_full?
@max_health == @health
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment