class CodeFormatter

Public Class Methods

call(*args) click to toggle source
# File lib/codebreaker/code_formatter.rb, line 2
def self.call(*args)
  new(*args).call
end
new(maker_code = [], user_code = []) click to toggle source
# File lib/codebreaker/code_formatter.rb, line 6
def initialize(maker_code = [], user_code = [])
  @maker_code = maker_code
  @user_code = user_code
end

Public Instance Methods

call() click to toggle source
# File lib/codebreaker/code_formatter.rb, line 11
def call
  format(@maker_code, @user_code)
end
format(maker_code, user_code) click to toggle source
# File lib/codebreaker/code_formatter.rb, line 15
def format(maker_code, user_code)
  user_code.each_with_index.map do |char, index|
    if maker_code[index] == user_code[index]
      maker_code[index] = nil
      '+'
    else
      char_index = maker_code.index(char)
      if char_index && minus?(char_index, maker_code, user_code)
        maker_code[char_index] = nil
        '-'
      end
    end
  end.compact.sort
end

Private Instance Methods

minus?(char_index, maker_code, user_code) click to toggle source
# File lib/codebreaker/code_formatter.rb, line 32
def minus?(char_index, maker_code, user_code)
  return unless maker_code[char_index]

  maker_code[char_index] != user_code[char_index]
end