Skip to content

Instantly share code, notes, and snippets.

@kpumuk
Last active December 20, 2015 10:20
Show Gist options
  • Save kpumuk/6114872 to your computer and use it in GitHub Desktop.
Save kpumuk/6114872 to your computer and use it in GitHub Desktop.
class Player
def play_turn(warrior)
case true
# Critical thinking
when dying_under_attack?(warrior) then warrior.walk! :backward
# Normal processing
when should_pivot?(warrior) then warrior.pivot!
# Can we shoot it?
when can_shoot?(warrior) then warrior.shoot!
# Need to rest?
when should_rest?(warrior) then warrior.rest!
# Can we go closer?
when can_walk?(warrior) then warrior.walk!
# Let's rescue what could be rescued....
when can_rescue?(warrior) then warrior.rescue!
# And kill the rest
when can_hit?(warrior) then warrior.attack!
# What happened?
else raise "I don't know what to do!"
end
@health = warrior.health
end
#----- State checks -----
def dying?(warrior)
warrior.health < 7
end
def under_attack?(warrior)
@health > warrior.health
end
def dying_under_attack?(warrior)
dying?(warrior) && under_attack?(warrior)
end
def should_rest?(warrior)
return false if warrior.health == 20 || under_attack?(warrior)
# Only rest if there is an enemy somewhere and we're dying
dying?(warrior) || warrior.look.any? { |c| c.enemy? }
end
def should_pivot?(warrior)
cells = warrior.look
# Do not turn around from exit
return false if cells.any? { |c| c.stairs? }
# Do we see a wall in front of us and no things to do?
cells[0].wall? || (cells[0].empty? && (cells[1].wall? || (cells[1].empty? && cells[2].wall?)))
end
#----- Teambuilding checks -----
def can_rescue?(warrior)
warrior.feel.captive?
end
def can_hit?(warrior)
warrior.feel.enemy?
end
def can_shoot?(warrior)
cells = warrior.look
# Only shoot if the enemy is far from us.
# If he is close - come closer and hit with a sword.
cells[0].empty? && cells[1].empty? && cells[2].enemy?
end
def can_walk?(warrior)
warrior.feel.empty?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment