Skip to content

Instantly share code, notes, and snippets.

@robtarr robtarr/warrior.rb
Last active Aug 29, 2015

Embed
What would you like to do?
class Player
def initialize
@last_health = 20
end
def play_turn(warrior)
move = MoveAction.new warrior
health = HealthAction.new warrior
attack = AttackAction.new warrior
resque = RescueAction.new warrior
retreat = RetreatAction.new warrior
for action in [retreat, attack, health, resque, move]
if action.can? @last_health
action.go!
@last_health = warrior.health
@last_health = @last_health
break
end
end
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
You can’t perform that action at this time.