Created
August 19, 2013 14:21
-
-
Save timsegraves/6269686 to your computer and use it in GitHub Desktop.
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 | |
def play_turn(warrior) | |
if damaged_from_behind(warrior) | |
warrior.shoot!(:backward) | |
else | |
if first_non_empty(warrior.look) == "enemy" | |
warrior.shoot! | |
else | |
if warrior.feel.enemy? | |
warrior.attack! | |
elsif warrior.feel.captive? | |
warrior.rescue! | |
else | |
if warrior.health < 20 | |
injured(warrior) | |
else | |
explore(warrior) | |
end | |
end | |
end | |
end | |
@health = warrior.health | |
end | |
def can_fight?(warrior) | |
warrior.health > 15 | |
end | |
def taking_damage?(warrior) | |
warrior.health < @health | |
end | |
def injured(warrior) | |
if taking_damage?(warrior) | |
if can_fight?(warrior) | |
warrior.walk! | |
else | |
warrior.walk!(:backward) | |
end | |
else | |
warrior.rest! | |
end | |
end | |
def explore(warrior) | |
if warrior.feel.stairs? | |
@stairs = true | |
if @wall | |
warrior.walk! | |
else | |
warrior.pivot! | |
end | |
elsif warrior.feel.wall? | |
@wall = true | |
warrior.pivot! | |
else | |
warrior.walk! | |
end | |
end | |
def first_non_empty(spaces) | |
first_non_empty = "blank" | |
spaces.each do |space| | |
if space.enemy? | |
return "enemy" | |
elsif space.captive? | |
return "captive" | |
end | |
end | |
return first_non_empty | |
end | |
def damaged_from_behind(warrior) | |
warrior.health < 20 && taking_damage?(warrior) && first_non_empty(warrior.look(:backward)) == "enemy" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The main other thing I'd note is usually when I write something that ends up with a bunch of if, else, elsif blocks in it I usually try to go back and refactor it into some smaller methods. Like in the play_turn method I could see having helper methods you could call for should_shoot, should_attack, and check_health each of which would only have a single if statement. Not a big deal but might make for a little cleaner code.