lib/codebreaker_diz/matcher.rb in codebreaker_diz-0.2.1 vs lib/codebreaker_diz/matcher.rb in codebreaker_diz-0.2.2
- old
+ new
@@ -2,23 +2,34 @@
require_relative 'config'
module CodebreakerDiz
class Matcher
- class << self
- def calculate_matches(secret, user_code)
- delta = []
- result = []
+ def initialize(secret_code, user_code)
+ @secret_code = secret_code
+ @user_code = user_code
- (0...CODE_LENGTH).each do |i|
- if secret[i] == user_code[i] then result << EXACT_MATCH_SIGN
- elsif (secret - delta).include? user_code[i] then result << NUMBER_MATCH_SIGN
- end
+ @exact_matches = 0
+ @number_matches = 0
+ end
- delta << user_code[i]
+ def matches
+ calculate_matches
+
+ EXACT_MATCH_SIGN * @exact_matches << NUMBER_MATCH_SIGN * @number_matches
+ end
+
+ private
+
+ def calculate_matches
+ delta = []
+
+ @secret_code.zip(@user_code).each do |secret_code_number, user_code_number|
+ if secret_code_number == user_code_number then @exact_matches += 1
+ elsif (@secret_code - delta).include? user_code_number then @number_matches += 1
end
- result.sort!.join
+ delta << user_code_number
end
end
end
end