Last active
December 20, 2015 09:48
-
-
Save ryan-scott-dev/6110194 to your computer and use it in GitHub Desktop.
warrior.rb for https://www.bloc.io/ruby-warrior
This file contains hidden or 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 initialize | |
@direction = :forward | |
@health = 20 | |
end | |
def play_turn(warrior) | |
@warrior = warrior | |
if nothing_interesting_ahead? | |
if is_badly_wounded? and took_damage? | |
walk_backwards! | |
elsif is_wounded? and didnt_take_damage? | |
rest! | |
else | |
walk! | |
end | |
else | |
if is_captive_ahead? | |
rescue! | |
elsif is_wall_ahead? | |
pivot! | |
elsif is_enemy_ahead? | |
attack! | |
elsif didnt_take_damage? | |
shoot! | |
else | |
walk! | |
end | |
end | |
end_of_turn | |
end | |
def nothing_interesting_ahead? | |
is_empty_ahead? and no_enemies_unblocked? | |
end | |
def is_enemy_visible? | |
@warrior.look(@direction).any? {|space| space.enemy? } | |
end | |
def no_captive_visible? | |
[email protected](@direction).any? {|space| space.captive? } | |
end | |
def enemy_not_blocked? | |
is_enemy_visible? and no_captive_visible? | |
end | |
def no_enemies_unblocked? | |
!enemy_not_blocked? | |
end | |
def is_empty_ahead? | |
@warrior.feel(@direction).empty? | |
end | |
def is_enemy_ahead? | |
@warrior.feel(@direction).enemy? | |
end | |
def is_captive_ahead? | |
@warrior.feel(@direction).captive? | |
end | |
def is_wall_ahead? | |
@warrior.feel(@direction).wall? | |
end | |
def is_wounded? | |
@warrior.health < 20 | |
end | |
def is_badly_wounded? | |
@warrior.health <=10 | |
end | |
def took_damage? | |
@warrior.health < @health | |
end | |
def didnt_take_damage? | |
not took_damage? | |
end | |
def rest! | |
@warrior.rest! | |
end | |
def walk! | |
@warrior.walk!(@direction) | |
end | |
def walk_backwards! | |
@warrior.walk!(:backward) | |
end | |
def rescue! | |
@warrior.rescue!(@direction) | |
end | |
def pivot! | |
@warrior.pivot! | |
end | |
def attack! | |
@warrior.attack!(@direction) | |
end | |
def shoot! | |
@warrior.shoot!(@direction) | |
end | |
def end_of_turn | |
@health = @warrior.health | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment