# frozen_string_literal: true module Codebreaker class MatrixGenerator def initialize(input, secret_code) @input = input @secret_code = secret_code end def call input, text_code, extra_char = check_in_same_position(@input) check_in_different_position(input, text_code, extra_char) end private def select_code(input) (0...4).select { |index| input[index] == @secret_code[index].to_s } end def check_in_same_position(input) text_code = "" extra_char = "" select_code(input).reverse_each do |index| text_code += "+" extra_char += input.slice(index) end [input, text_code, extra_char] end def check_in_different_position(input, text_code = "", extra_char = "") input.each_char do |item| if include_number?(item, extra_char) text_code += "-" extra_char += item end end text_code end def include_number?(number, char) @secret_code.include?(number.to_i) && !char.include?(number) end end end