class XenosEnigma::Xenos::Base

Base class holds all the logic for correctly detecting xenos ships

Constants

SHIP_DETECTION

Attributes

ship_height[R]
ship_width[R]
xeno_signature[R]

Public Class Methods

new() click to toggle source
# File lib/xenos_enigma/xenos/base.rb, line 9
def initialize
  raise 'This is an abstract class' if instance_of?(XenosEnigma::Xenos::Base)

  @xeno_signature = self.class::SIGNATURE.split(/\n/)
  @ship_width  = @xeno_signature.first.size
  @ship_height = @xeno_signature.size
end

Public Instance Methods

analyze?(radar_partial, look_ahead_radar_data) click to toggle source
# File lib/xenos_enigma/xenos/base.rb, line 17
def analyze?(radar_partial, look_ahead_radar_data)
  return if radar_partial.size < ship_width

  hit = nil

  @xeno_signature.each_with_index do |xeno_row, xeno_y_position|
    if possible_match?(xeno_row, radar_partial)
      hit = full_scan(xeno_y_position, look_ahead_radar_data)
      break if hit
    end
  end

  hit
end

Private Instance Methods

full_scan(xeno_y_position, look_ahead_radar_data) click to toggle source
# File lib/xenos_enigma/xenos/base.rb, line 34
def full_scan(xeno_y_position, look_ahead_radar_data)
  compound_match_score = 0
  lines_scanned = 0

  (xeno_y_position..(ship_height - 1)).each do |y_scan|
    break if look_ahead_radar_data[y_scan - xeno_y_position].nil?

    compound_match_score += pattern_difference(@xeno_signature[y_scan], look_ahead_radar_data[y_scan - xeno_y_position])
    lines_scanned += 1
  end

  is_detection_cofirmed = (lines_scanned * SHIP_DETECTION[:compound_tolerance] >= compound_match_score)

  if lines_scanned >= SHIP_DETECTION[:min_segments] && is_detection_cofirmed
    XenosEnigma::Hit.new(self, xeno_y_position)
  end
end
pattern_difference(data1, data2) click to toggle source
# File lib/xenos_enigma/xenos/base.rb, line 56
def pattern_difference(data1, data2)
  DidYouMean::Levenshtein.distance(data1, data2)
end
possible_match?(xeno_row, radar_partial) click to toggle source
# File lib/xenos_enigma/xenos/base.rb, line 52
def possible_match?(xeno_row, radar_partial)
  pattern_difference(xeno_row, radar_partial) <= SHIP_DETECTION[:tolerance]
end