Sha256: 420e3c47dfaa8a3f8faec6dd5fae02caea8a8f8a0737fdc8487ad97e2ee7447a

Contents?: true

Size: 896 Bytes

Versions: 4

Compression:

Stored size: 896 Bytes

Contents

# frozen_string_literal: true

module Codebreaker
  class GuessChecker
    attr_accessor :inexact, :exact, :none

    MINUSE = '-'
    PLUS = '+'
    NONE = ' '

    def initialize(code, code_input)
      @code = code.chars
      @code_input = code_input.chars
    end

    def symbols(inexact = MINUSE, exact = PLUS, none = NONE)
      @inexact = inexact
      @exact = exact
      @none = none
    end

    def check_input
      raw_result = mismatched
      matched(raw_result)
    end

    private

    def mismatched
      inexact = (@code & @code_input).map { |element| [@code.count(element), @code_input.count(element)].min }.sum
      @inexact * inexact
    end

    def matched(result)
      @code_input.each.with_index do |element, index|
        result.sub!(@inexact, @exact) if element == @code[index]
      end
      return result unless result.empty?

      @none
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
codebreaker_kub-0.3.1 lib/codebreaker/guess_checker.rb
codebreaker_kub-0.3.0 lib/codebreaker/guess_checker.rb
codebreaker_kub-0.2.6 lib/codebreaker/guess_checker.rb
codebreaker_kub-0.2.5 lib/codebreaker/guess_checker.rb