Skip to content

Instantly share code, notes, and snippets.

@yock
Forked from robtarr/warrior.rb
Last active August 29, 2015 13:57
Show Gist options
  • Save yock/9687023 to your computer and use it in GitHub Desktop.
Save yock/9687023 to your computer and use it in GitHub Desktop.
class Player
def initialize
@last_health = 20
end
def play_turn(warrior)
possible_actions = actions.select { |action| action.can? @last_health }
possible_actions.first.go!
@last_health = warrior.health
end
def actions
[
MoveAction.new warrior,
HealthAction.new warrior,
AttackAction.new warrior,
RescueAction.new warrior,
RetreatAction.new warrior
]
end
end
class WarriorAction
def initialize warrior
@warrior = warrior
end
end
class RetreatAction < WarriorAction
def can? last_health = nil
@warrior.health < last_health and @warrior.health < 10 and @warrior.feel.empty?
end
def go!
@warrior.pivot!
end
end
class HealthAction < WarriorAction
def can? last_health = nil
@warrior.health >= last_health and @warrior.feel.empty? and @warrior.health < 20
end
def go!
@warrior.rest!
end
end
class BowAction < WarriorAction
def can? last_health = nil
spaces = @warrior.look
!spaces[0].captive? and !spaces[1].captive? and spaces[2].enemy?
end
def go!
@warrior.shoot!
end
end
class SwordAction < WarriorAction
def can? last_health = nil
@warrior.feel.enemy?
end
def go!
@warrior.attack!
end
end
class AttackAction < WarriorAction
def initialize warrior
super warrior
@sword = SwordAction.new warrior
@bow = BowAction.new warrior
end
def can? last_health = nil
@sword.can? or @bow.can?
end
def go!
if @sword.can?
@sword.go!
else
@bow.go!
end
end
end
class RescueAction < WarriorAction
def can? last_health = nil
@warrior.feel.captive?
end
def go!
@warrior.rescue!
end
end
class MoveAction < WarriorAction
def can? last_health = nil
@warrior.feel.empty? or @warrior.feel.wall?
end
def go!
if @warrior.feel.wall?
@warrior.pivot!
else
@warrior.walk!
end
end
def hitWall?
@warrior.feel.wall?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment