Last active
December 20, 2015 13:59
-
-
Save andypalmer/6143025 to your computer and use it in GitHub Desktop.
Slightly complicated solution to level 5 of RubyWarrior: https://www.bloc.io/ruby-warrior
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 Safe | |
def next_move_for(player) | |
return Attack.new if player.is_under_attack? | |
self | |
end | |
def act(warrior) | |
return warrior.rest! if warrior.health < 15 | |
return warrior.attack! if warrior.feel.enemy? | |
return warrior.rescue! if warrior.feel.captive? | |
warrior.walk! | |
end | |
end | |
class Attack | |
def initialize | |
end | |
def next_move_for(player) | |
return Safe.new unless player.is_under_attack? | |
self | |
end | |
def act(warrior) | |
return warrior.attack! if warrior.feel.enemy? | |
warrior.walk! | |
end | |
end | |
class Player | |
def play_turn(warrior) | |
@decision ||= Safe.new | |
@health ||= warrior.health | |
@warrior = warrior | |
@decision = @decision.next_move_for(self) | |
@decision.act(warrior) | |
@health = warrior.health | |
end | |
def is_under_attack? | |
@warrior.health < @health | |
end | |
end | |
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 play_turn(warrior) | |
@direction ||= :backward | |
@direction = :forward if warrior.feel(@direction).wall? | |
@health ||= warrior.health | |
@warrior = warrior | |
action ||= lambda { warrior.rescue! @direction } if we_find_a_captive | |
action ||= lambda { warrior.attack! @direction } if there_is_an_enemy | |
action ||= lambda { warrior.walk! :backward } if we_need_to_retreat | |
action ||= lambda { warrior.rest! } if we_need_to_rest | |
action ||= lambda { warrior.walk! @direction } | |
action.call | |
@health = warrior.health | |
end | |
def we_find_a_captive | |
@warrior.feel(@direction).captive? | |
end | |
def there_is_an_enemy | |
@warrior.feel(@direction).enemy? | |
end | |
def we_need_to_rest | |
@warrior.health < 15 && !we_are_taking_hits | |
end | |
def we_need_to_retreat | |
@warrior.health < 6 && we_are_taking_hits | |
end | |
def we_are_taking_hits | |
@warrior.health < @health | |
end | |
end | |
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 play_turn(warrior) | |
return warrior.pivot! if warrior.feel.wall? | |
@health ||= warrior.health | |
@warrior = warrior | |
action ||= lambda { warrior.rescue! } if we_find_a_captive | |
action ||= lambda { warrior.attack! } if there_is_an_enemy | |
action ||= lambda { warrior.walk! :backward } if we_need_to_retreat | |
action ||= lambda { warrior.rest! } if we_need_to_rest | |
action ||= lambda { warrior.walk! } | |
action.call | |
@health = warrior.health | |
end | |
def we_find_a_captive | |
@warrior.feel.captive? | |
end | |
def there_is_an_enemy | |
@warrior.feel.enemy? | |
end | |
def we_need_to_rest | |
@warrior.health < 15 && !we_are_taking_hits | |
end | |
def we_need_to_retreat | |
@warrior.health < 6 && we_are_taking_hits | |
end | |
def we_are_taking_hits | |
@warrior.health < @health | |
end | |
end | |
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 play_turn(warrior) | |
return warrior.pivot! if warrior.feel.wall? | |
@health ||= warrior.health | |
@warrior = warrior | |
action ||= lambda { warrior.shoot! } if there_is_an_enemy | |
action ||= lambda { warrior.rescue! } if we_find_a_captive | |
#action ||= lambda { warrior.attack! } if there_is_an_enemy | |
action ||= lambda { warrior.walk! :backward } if we_need_to_retreat | |
action ||= lambda { warrior.rest! } if we_need_to_rest | |
action ||= lambda { warrior.walk! } | |
action.call | |
@health = warrior.health | |
end | |
def we_find_a_captive | |
@warrior.feel.captive? | |
end | |
def there_is_an_enemy | |
occupied_spaces = @warrior.look.select {|x| !x.empty?} | |
return false if occupied_spaces.empty? | |
return occupied_spaces.first.enemy? | |
end | |
def we_need_to_rest | |
@warrior.health < 15 && !we_are_taking_hits | |
end | |
def we_need_to_retreat | |
@warrior.health < 6 && we_are_taking_hits | |
end | |
def we_are_taking_hits | |
@warrior.health < @health | |
end | |
end | |
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 play_turn(warrior) | |
if @first.nil? | |
@first=false | |
return warrior.walk! | |
end | |
return warrior.pivot! if warrior.feel.wall? | |
@health ||= warrior.health | |
@warrior = warrior | |
action ||= lambda { warrior.attack!} if warrior.feel.enemy? | |
action ||= lambda { warrior.shoot! } if there_is_an_enemy | |
action ||= lambda { warrior.rescue! } if we_find_a_captive | |
action ||= lambda { warrior.walk! :backward } if we_need_to_retreat | |
action ||= lambda { warrior.rest! } if we_need_to_rest | |
action ||= lambda { warrior.walk! } | |
action.call | |
@health = warrior.health | |
end | |
def we_find_a_captive | |
@warrior.feel.captive? | |
end | |
def there_is_an_enemy | |
occupied_spaces = @warrior.look.select {|x| !x.empty?} | |
return false if occupied_spaces.empty? | |
return occupied_spaces.first.enemy? | |
end | |
def we_need_to_rest | |
@warrior.health < 15 && !we_are_taking_hits | |
end | |
def we_need_to_retreat | |
@warrior.health < 6 && we_are_taking_hits | |
end | |
def we_are_taking_hits | |
@warrior.health < @health | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment