lib/codebreaker/guess_checker.rb in codebreaker_kub-0.2.4 vs lib/codebreaker/guess_checker.rb in codebreaker_kub-0.2.5

- old
+ new

@@ -1,31 +1,40 @@ # frozen_string_literal: true module Codebreaker - module GuessChecker + class GuessChecker + attr_accessor :inexact, :exact, :none + MINUSE = '-' PLUS = '+' - NONE = '' + 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(code, input) - raw_result = inexact(code.chars, input.chars) - exact(code.chars, input.chars, raw_result) + def check_input + raw_result = mismatched + matched(raw_result) end - def inexact(code, input) - inexact = (code & input).map { |element| [code.count(element), input.count(element)].min }.sum + private + + def mismatched + inexact = (@code & @code_input).map { |element| [@code.count(element), @code_input.count(element)].min }.sum @inexact * inexact end - def exact(code, input, result) - input.each.with_index do |element, index| - result.sub!(@inexact, @exact) if element == code[index] + 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