Skip to content

Instantly share code, notes, and snippets.

@borodean
Created March 12, 2014 03:23
Show Gist options
  • Select an option

  • Save borodean/9500232 to your computer and use it in GitHub Desktop.

Select an option

Save borodean/9500232 to your computer and use it in GitHub Desktop.
RubyWarrior
class Player
def initialize
@max_health = 20
@health = @max_health
@direction = :backward
@has_retreated = false
@is_berserk = false
end
def play_turn(warrior)
# Shorthands
feel = warrior.feel(@direction)
# Stats
is_taking_damage = (warrior.health < @health)
is_wounded = (warrior.health < @max_health)
# Memoise
@health = warrior.health
# Rescue
if feel.captive?
return warrior.rescue!(@direction)
end
# Hit a wall
if feel.wall?
@direction = (@direction == :forward) ? :backward : :forward
return play_turn(warrior)
end
# Attack
if feel.enemy?
return warrior.attack!(@direction)
end
# Exit
if feel.stairs?
return warrior.walk!(@direction)
end
# Healing
if is_wounded and not @is_berserk
if is_taking_damage
@has_retreated = true
return warrior.walk!(:backward)
end
return warrior.rest!
else
@is_berserk = @has_retreated
end
# Walk
return warrior.walk!(@direction)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment