lib/codebreaker_diz/matcher.rb in codebreaker_diz-0.2.5 vs lib/codebreaker_diz/matcher.rb in codebreaker_diz-0.2.6
- old
+ new
@@ -1,32 +1,36 @@
# frozen_string_literal: true
require_relative 'config'
module CodebreakerDiz
- class Matcher
+ class CodeResolver
+ attr_reader :matches
+
def initialize(secret_code, user_code)
- @secret_code = secret_code.clone
- @user_code = user_code
+ @secret_code = secret_code.clone
+ @user_code = user_code
- @exact_matches = 0
+ @exact_matches = 0
@number_matches = 0
+
+ @matches = obtain_matches
end
- def matches
- calculate_matches
+ private
+ def obtain_matches
+ calculate_matches unless @secret_code.empty?
+
EXACT_MATCH_SIGN * @exact_matches << NUMBER_MATCH_SIGN * @number_matches
end
- private
-
def calculate_matches
@secret_code.zip(@user_code).each 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
- elsif @secret_code.include? user_code_number then @number_matches += 1
+ if secret_code_number == user_code_number then @exact_matches += 1
+ else @number_matches += 1
end
@secret_code.delete_at(@secret_code.index(user_code_number))
end
end