Sha256: 201a73550ef2cefaf0a98c6b0f139300ed1c675c0f7a4c6e2e9b951565495224

Contents?: true

Size: 1.06 KB

Versions: 9

Compression:

Stored size: 1.06 KB

Contents

# 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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
codebreaker-mikhail-0.2.4 lib/codebreaker/matrix.rb
codebreaker-mikhail-0.2.3 lib/codebreaker/matrix.rb
codebreaker-mikhail-0.2.2 lib/codebreaker/matrix.rb
codebreaker-mikhail-0.2.1 lib/codebreaker/matrix.rb
codebreaker-mikhail-0.2.0 lib/codebreaker/matrix.rb
codebreaker-mikhail-0.1.5 lib/codebreaker/matrix.rb
codebreaker-mikhail-0.1.4 lib/codebreaker/matrix.rb
codebreaker-mikhail-0.1.3 lib/codebreaker/matrix.rb
codebreaker-mikhail-0.1.2 lib/codebreaker/matrix.rb