Skip to content

Instantly share code, notes, and snippets.

@ninjapanzer
Created August 12, 2013 13:07
Show Gist options
  • Save ninjapanzer/6210657 to your computer and use it in GitHub Desktop.
Save ninjapanzer/6210657 to your computer and use it in GitHub Desktop.
respond_to? ruby warrior rewrite
class Player
def init(warrior)
@warrior = warrior
@low_side ||= 10
@current_direction ||= :forward
@backsteps ||= 0
@skip ||= false
@turn_count ||= 0
@health ||= 20
@surrounded ||= false
unless @warrior.respond_to? :pivot!
class << @warrior
def pivot!
change_direction
end
end
end
unless @warrior.respond_to? :look
class << @warrior
def look(direction)
[]
end
end
end
end
def play_turn(warrior)
init(warrior)
sit_rep
surrounded?
do_stuff! unless @skip
@skip = false
end
def sit_rep
if @turn_count < 1
front = ranged_enemy? change_direction
back = ranged_enemy? change_direction
skip_actions!
@surrounded = front && back
@warrior.pivot! if @surrounded
@turn_count += 1
end
hit_wall?
end
def ranged_enemy?(direction)
@warrior.look(@current_direction).detect{|space| space.enemy?}
end
def surrounded?
if @surrounded && @warrior.feel.stairs?
@warrior.pivot!
#change_direction
skip_actions!
@low_side = 20
@surrounded = false
end
end
def skip_actions!
@skip = true
end
def change_direction
if @current_direction == :forward
@current_direction = :backward
:backward
elsif @current_direction == :backward
@current_direction = :forward
:forward
end
end
def do_stuff!
if @warrior.health < @low_side && @warrior.feel(@current_direction).empty?
@warrior.rest!
@low_side = 20
elsif @warrior.feel(@current_direction).enemy?
@warrior.attack!(@current_direction)
elsif @warrior.feel(@current_direction).captive?
@warrior.rescue!(@current_direction)
elsif @warrior.look(@current_direction).detect{|item| item.captive?}
@warrior.walk!
elsif @warrior.look(@current_direction)[2].enemy?
@warrior.shoot!
else
@warrior.walk!(@current_direction)
@low_side = 10
@backsteps = 0
end
@health = @warrior.health
end
def backstep
change_direction
@warrior.walk!(@current_direction)
change_direction
@backsteps += 1
end
def hit_wall?
if @warrior.feel(@current_direction).wall?
@warrior.pivot!
skip_actions!
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment