Skip to content

Instantly share code, notes, and snippets.

@timsegraves
Created August 19, 2013 14:21
Show Gist options
  • Save timsegraves/6269686 to your computer and use it in GitHub Desktop.
Save timsegraves/6269686 to your computer and use it in GitHub Desktop.
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
@timsegraves
Copy link
Author

I don't see in here if you're doing anything with the @stairs instance var other than setting it. Maybe that comes later?

@timsegraves
Copy link
Author

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.

@nicusg
Copy link

nicusg commented Aug 21, 2013

I use the @stairs and @Wall instance variables to make sure I've explored all spaces so there is no captive remaining.

@nicusg
Copy link

nicusg commented Aug 21, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment