lib/codebreaker_diz/matcher.rb in codebreaker_diz-0.2.6 vs lib/codebreaker_diz/matcher.rb in codebreaker_diz-0.2.7

- old
+ new

@@ -23,17 +23,23 @@ EXACT_MATCH_SIGN * @exact_matches << NUMBER_MATCH_SIGN * @number_matches end def calculate_matches - @secret_code.zip(@user_code).each do |secret_code_number, user_code_number| + @secret_code.zip(@user_code).map do |secret_code_number, user_code_number| next unless @secret_code.include? user_code_number - if secret_code_number == user_code_number then @exact_matches += 1 - else @number_matches += 1 - end + next add_match(user_code_number, exact: true) if secret_code_number == user_code_number - @secret_code.delete_at(@secret_code.index(user_code_number)) + add_match(user_code_number) end + end + + def add_match(matched_code_number, exact: false) + @exact_matches += 1 if exact + + @number_matches += 1 unless exact + + @secret_code.delete_at(@secret_code.index(matched_code_number)) end end end