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
@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