Skip to content

Rulibutien's code Intermediate

Théo Liberman edited this page Nov 25, 2018 · 1 revision
# Total Score: 797 + 184 = 981
# 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

class Player
  attr_accessor :warrior, :directions

  def play_turn(warrior)
    @warrior = warrior
    @directions = [:backward, :left, :right, :forward]
    move unless rest or bind or detonate or go_to_ticking or attack or save or kill_bind
  end

  def move
    if warrior.listen.any?
      if warrior.feel(warrior.direction_of(warrior.listen.first)).stairs?
        for direction in directions
          if warrior.feel(direction).empty? and not warrior.feel(direction).wall?
            warrior.walk!(direction)
            break
          end
        end
      else
        warrior.walk!(warrior.direction_of(warrior.listen.first))
      end
    else
      warrior.walk!(warrior.direction_of_stairs)
    end
  end

  def rest
    if enemy_remaining? and not ticking_captive? and not enemy_around? and low_health?
      warrior.rest!
      return true
    end
    false
  end

  def bind
    if many_enemies_around?
      for direction in directions
        if warrior.feel(direction).enemy?
          warrior.bind!(direction)
          return true
        end
      end
    end
    false
  end

  def detonate
    if warrior.look[1].enemy? and warrior.look[1].unit.health < 5
      warrior.health > 5 ? warrior.detonate! : warrior.rest!
      return true
    end
    false
  end

  def go_to_ticking
    for space in warrior.listen
      if space.ticking?
        if warrior.feel(warrior.direction_of(space)).empty?
          warrior.walk!(warrior.direction_of(space))
          return true
        end
      end
    end
    false
  end

  def attack
    for direction in @directions
      if warrior.feel(direction).enemy?
        warrior.look(direction)[1].enemy? ? warrior.detonate!(direction) : warrior.attack!(direction)
        return true
      end
    end
    false
  end

  def save
    for direction in directions
      if is_captive?(warrior.feel(direction))
        warrior.rescue!(direction)
        return true
      end
    end
    false
  end

  def kill_bind
    for direction in directions
      if is_bind?(warrior.feel(direction))
        warrior.attack!(direction)
        return true
      end
    end
    false
  end

  def is_captive?(space)
    space.captive? and space.unit.character == 'C'
  end

  def is_bind?(space)
    space.captive? and space.unit.character != 'C'
  end

  def low_health?
    threshold = warrior.listen.first.unit.character == 'S' ? 14 : 10
    warrior.health < threshold
  end

  def many_enemies_around?
    count = 0
    for direction in directions
      count += 1 if warrior.feel(direction).enemy?
    end
    count > 1
  end

  def enemy_around?
    for direction in directions
      return true if warrior.feel(direction).enemy?
    end
    false
  end

  def enemy_remaining?
    for space in warrior.listen
      unless space.empty?
        return true if 'S s'.include?(space.unit.character)
      end
    end
    false
  end

  def ticking_captive?
    for space in warrior.listen
      return true if space.ticking?
    end
    false
  end

end