Last active
December 20, 2015 22:29
-
-
Save ninjapanzer/6205619 to your computer and use it in GitHub Desktop.
Ruby Warrior Level 9 Works for levels 8 and 9 - Does a pass to see if the warrior is between enemies an then goes to work
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Player | |
def init(warrior) | |
@warrior = warrior | |
@low_side ||= 10 | |
@current_direction ||= :forward | |
@backsteps ||= 0 | |
@skip ||= false | |
@turn_count ||= 0 | |
@health ||= 20 | |
@surrounded ||= false | |
end | |
def play_turn(warrior) | |
init(warrior) | |
if @turn_count < 1 | |
front = @warrior.look(@current_direction).detect{|space| space.enemy?} | |
change_direction | |
back = @warrior.look(@current_direction).detect{|space| space.enemy?} | |
change_direction | |
@warrior.pivot! | |
skip_actions! | |
@surrounded = front && back | |
end | |
@turn_count += 1 | |
hit_wall? | |
surrounded? | |
do_stuff! unless @skip | |
@skip = false | |
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 | |
elsif @current_direction == :backward | |
@current_direction = :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