Created
March 11, 2017 06:53
-
-
Save benzittlau/15e0f1c9e64eef2d416d8bd4345baec1 to your computer and use it in GitHub Desktop.
Ruby warrior intermediate player implementation (Level 4)
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 | |
DIRECTIONS = [:left, :forward, :right, :backward] | |
def play_turn(warrior) | |
@warrior = warrior | |
bind_enemies! || | |
attack_enemies! || | |
heal! || | |
unbind_enemy! || | |
walk! | |
end | |
def bind_enemies! | |
if enemy_count > 1 | |
@warrior.bind!(enemy_directions.first) | |
return true | |
else | |
return false | |
end | |
end | |
def attack_enemies! | |
if enemy_count == 1 | |
@warrior.attack!(enemy_directions.first) | |
return true | |
else | |
return false | |
end | |
end | |
def heal! | |
if @warrior.health < 20 | |
@warrior.rest! | |
return true | |
else | |
return false | |
end | |
end | |
def unbind_enemy! | |
if next_space.captive? | |
@warrior.rescue!(next_direction) | |
return true | |
else | |
return false | |
end | |
end | |
def walk! | |
@warrior.walk!(next_direction) | |
return true | |
end | |
def next_direction | |
@warrior.direction_of_stairs | |
end | |
def next_space | |
@warrior.feel(next_direction) | |
end | |
def enemy_count | |
enemy_directions.count | |
end | |
def enemy_directions | |
DIRECTIONS.select{ |dir| @warrior.feel(dir).enemy? } | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment