Skip to content

Rulibutien's code Beginner

Théo Liberman edited this page Nov 25, 2018 · 1 revision
# Total Score: 544 + 106 = 650
# Your average grade for this tower is: S
#
# Level 1: S
# Level 2: S
# Level 3: S
# Level 4: S
# Level 5: S
# Level 6: S
# Level 7: S
# Level 8: S
# Level 9: S

class Player
  attr_accessor :warrior

  def play_turn(warrior)
    @warrior = warrior
    move unless save or attack
  end

  def move
    if wall_ahead? and not enemy_ahead?
      warrior.pivot!
    elsif captive_ahead?(:backward) and not enemy_ahead?(:backward)
      warrior.walk!(:backward)
    else
      warrior.walk!
    end
  end

  def save
    for direction in [:backward, :forward]
      if warrior.feel(direction).captive?
        warrior.rescue!(direction)
        return true
      end
    end
    false
  end

  def attack
    for direction in [:backward, :forward]
      if warrior.feel(direction).enemy?
        warrior.attack!(direction)
        return true
      end
      unless captive_ahead?(direction) or can_approach?(direction)
        warrior.shoot!(direction)
        return true
      end
    end
    false
  end

  def can_approach?(direction)
    for space in warrior.look(direction)
      if space.enemy?
        return false if 'w a'.include?(space.unit.character)
        thick_factor = space.unit.character == 'S' ? 2 : 1 # Thick Sludge are twice stronger
        return space.unit.health <= space.unit.max_health - 3 ** thick_factor # Damage by an arrow = 3
      end
    end
    true
  end

  def enemy_ahead?(direction = :forward)
    for space in warrior.look(direction)
      return true if space.enemy?
    end
    false
  end

  def captive_ahead?(direction = :forward)
    for space in warrior.look(direction)
      return true if space.captive?
    end
    false
  end

  def wall_ahead?(direction = :forward)
    for space in warrior.look(direction)
      return false if space.stairs?
      return true if space.wall?
    end
    false
  end

end