class CodeFormatter

Attributes

result[R]

Public Class Methods

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

Public Instance Methods

call() click to toggle source
# File lib/codebreaker/code_formatter.rb, line 13
def call
  format_plus.format_minus
  @result
end
format_minus() click to toggle source
# File lib/codebreaker/code_formatter.rb, line 30
def format_minus
  @user_code.each do |number|
    next unless @maker_code.include?(number)

    @result << '-'
    clear_number(@maker_code, @maker_code.index(number))
    clear_number(@user_code, @user_code.index(number))
  end
  code_compact
  self
end
format_plus() click to toggle source
# File lib/codebreaker/code_formatter.rb, line 18
def format_plus
  @user_code.zip(@maker_code).each_with_index do |numbers, index|
    next unless numbers.first == numbers.last

    @result << '+'
    clear_number(@maker_code, index)
    clear_number(@user_code, index)
  end
  code_compact
  self
end

Private Instance Methods

clear_number(array, index) click to toggle source
# File lib/codebreaker/code_formatter.rb, line 49
def clear_number(array, index)
  array[index] = nil
end
code_compact() click to toggle source
# File lib/codebreaker/code_formatter.rb, line 44
def code_compact
  @user_code.compact!
  @maker_code.compact!
end