Skip to content

Instantly share code, notes, and snippets.

@kmayer
Created August 27, 2013 20:37
Show Gist options
  • Select an option

  • Save kmayer/6358840 to your computer and use it in GitHub Desktop.

Select an option

Save kmayer/6358840 to your computer and use it in GitHub Desktop.
rubywarrior (Level 6) with Ryan Spore
Command = Struct.new(:method, :direction)
class Command
def initialize(m, *args)
@method = m
@direction = args
end
def perform(warrior)
warrior.send(@method, *@direction)
end
end
class Player
attr_reader :warrior
attr_accessor :health
def initialize
@direction = :backward
@health = 20
end
def play_turn(warrior)
@warrior = warrior
tactics = [retreat, rescue_captive, attack, rest, walk].compact
tactics.first.perform(warrior)
change_direction
@health = warrior.health
end
def retreat
Command.new("walk!", opposite) if attacked? && sick?
end
def opposite
{:forward => :backward, :backward => :forward}[@direction]
end
def sick?
warrior.health < 8
end
def rescue_captive
Command.new("rescue!", @direction) if warrior.feel(@direction).captive?
end
def attack
Command.new("attack!", :forward) if warrior.feel(:forward).enemy?
end
def walk
Command.new("walk!", @direction)
end
def rest
Command.new("rest!") if warrior.health < 20 && !attacked?
end
def change_direction
@direction = :forward if warrior.feel(:backward).wall?
end
def attacked?
@health > warrior.health
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment