Skip to content

Dragonfax's Beginner Solution

Daniel Hug edited this page Apr 8, 2015 · 2 revisions
CONGRATULATIONS! You have climbed to the top of the tower and rescued the fair maiden Ruby.
Level Score: 74
Time Bonus: 0
Clear Bonus: 15
Level Grade: B
Total Score: 399 + 89 = 488
Your average grade for this tower is: B

  Level 1: S
  Level 2: A
  Level 3: S
  Level 4: A
  Level 5: D
  Level 6: F
  Level 7: A
  Level 8: F
  Level 9: B
class Player

  def under_attack?
    @warrior.health < @last_health
  end

  def health_low?
    @warrior.health < 10
  end

  def range_enemy?(direction)
    ! @warrior.look(direction).select { |space| space.enemy? }.empty?
  end

  DIRECTIONS = [:forward, :backward]

  def fight_adjacent_enemies
    enemies = DIRECTIONS.select { |d| @warrior.feel(d).enemy? }
    if ! enemies.empty?
      @warrior.attack!(enemies.first) unless @action_taken
      @action_taken = true
    end
  end

  def free_adjacent_captives      
    captives = DIRECTIONS.select {|d| @warrior.feel(d).captive? }
    if ! captives.empty?
      @warrior.rescue!(captives.first) unless @action_taken
      @action_taken = true
    end
  end

  def fight_or_flight
    if under_attack?
      if health_low? && @warrior.feel(:backward).empty?
        @warrior.walk!(:backward) unless @action_taken
        @action_taken = true
      else
        @warrior.walk! unless @action_taken
        @action_taken = true
      end
    end
  end

  def long_range_attack
    DIRECTIONS.each do |d|
      if range_enemy?(d)
        @warrior.shoot!(d) unless @action_taken
        @action_taken = true
      end
    end
  end

  def wander
    if @warrior.health < 20
      @warrior.rest! unless @action_taken
      @action_taken = true
    else
      if @warrior.feel.wall?
        @warrior.pivot! unless @action_taken
        @action_taken = true
      else
        @warrior.walk! unless @action_taken
        @action_taken = true
      end
    end
  end

  def play_turn(warrior)
    @action_taken = false
    @warrior = warrior
    @last_health = warrior.health if ! @last_health

    fight_adjacent_enemies

    free_adjacent_captives

    fight_or_flight

    long_range_attack

    wander

    @last_health = warrior.health
  end
end