Last active
December 21, 2015 10:18
-
-
Save nicusg/6290804 to your computer and use it in GitHub Desktop.
Rewritten some methods
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 | |
@stairs = false | |
@wall = false | |
@warrior | |
def play_turn(warrior) | |
@warrior = warrior | |
if damaged_from_behind | |
warrior.shoot!(:backward) | |
else | |
should_look | |
end | |
@health = warrior.health | |
end | |
def can_fight | |
@warrior.health > 15 | |
end | |
def taking_damage? | |
@warrior.health < @health | |
end | |
def injured | |
if taking_damage? | |
return @warrior.walk!(can_fight? ? :forward : :backward) | |
else | |
@warrior.rest! | |
end | |
end | |
def explore | |
if @warrior.feel.stairs? | |
@stairs = true | |
return @wall ? @warrior.walk! : @warrior.pivot! | |
end | |
if @warrior.feel.wall? | |
@wall = true | |
return @warrior.pivot! | |
end | |
@warrior.walk! | |
end | |
def first_non_empty(spaces) | |
spaces.each do |space| | |
return "enemy" if space.enemy? | |
return "captive" if space.captive? | |
end | |
"blank" | |
end | |
def damaged_from_behind | |
@warrior.health < 20 && taking_damage? && first_non_empty(@warrior.look(:backward)) == "enemy" | |
end | |
def check_health | |
@warrior.health < 20 ? injured : explore | |
end | |
def should_move | |
return @warrior.attack! if @warrior.feel.enemy? | |
return @warrior.rescue! if @warrior.feel.captive? | |
check_health | |
end | |
def should_look | |
if first_non_empty(@warrior.look) == "enemy" | |
@warrior.shoot! | |
else | |
should_move | |
end | |
end | |
end |
That's pretty much it. Sorry to just show you how I'd do it but sometimes its hard to make suggestions until I sit down and write through the code. Most of them are just syntactic sugar that ruby makes easy. Let me know if you have any questions.
Nice, that looks better.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Injured I might write something like: