-
Notifications
You must be signed in to change notification settings - Fork 838
curi's code
djuretic edited this page Feb 18, 2012
·
7 revisions
Hi, I beat it (what’s out so far is 7 levels of beginner and 6 of intermediate). Here is my code at the end of intermediate if anyone wants it. I like my short play_turn method. i made no attempt to get max points.
class Player
attr_accessor :warrior, :last_health, :ticks, :retreated
Directions = [:forward, :left, :backward, :right]
RetreatDirections = [:backward, :left, :right, :forward]
MaxHealth = 20
def initialize
@last_health = MaxHealth
@ticks = 0
@retreated = false
end
def play_turn(warrior)
puts "my health is: #{warrior.health}"
self.warrior = warrior
self.ticks += 1
action = feel_around || handle_retreat || handle_attacks || handle_resting || handle_captives || handle_post_retreat_walking
walk_towards_stairs if !action
self.last_health = warrior.health
end
def handle_post_retreat_walking
return false if !self.retreated
puts "i retreated, i'll go somewhere else!"
not_again = Directions.dup
not_again.delete(self.retreated)
self.retreated = false
not_again.sort_by {rand}.each do |dir|
puts "considering: #{dir}"
if warrior.feel(dir).empty?
warrior.walk!(dir)
return true
end
end
end
def enemy_count
count = 0
Directions.each do |dir|
count += 1 if warrior.feel(dir).enemy?
end
count
end
def handle_retreat
if enemy_count > 1
RetreatDirections.each do |dir|
if warrior.feel(dir).empty?
warrior.walk!(dir)
self.retreated = dir
return true
end
end
return handle_captives
end
false
end
def handle_attacks
Directions.each do |dir|
if warrior.feel(dir).enemy?
warrior.attack!(dir)
self.fighting = true
return true
end
end
false
end
def feel_around
Directions.each do |dir|
if warrior.feel(dir).stairs? && warrior.feel(dir).empty?
warrior.walk!(dir)
return true
end
end
false
end
def walk_towards_stairs
warrior.walk!(warrior.direction_of_stairs)
true
end
def change_heading
self.heading = opposite_direction(self.heading)
end
def handle_resting
if warrior.health < MaxHealth && !taking_damage?
warrior.rest!
return true
end
end
def handle_captives
Directions.each do |dir|
if warrior.feel(dir).captive?
warrior.rescue!(dir)
return true
end
end
false
end
def taking_damage?
warrior.health < last_health
end
end