module AlexCodebreaker class Comparison def initialize(user_input, secret_code_for_comparison) @user_input = user_input @secret_code_for_comparison = secret_code_for_comparison @matching = { place: 0, presence: 0 } end def response place_matches presence_matches format_response end private def place_matches @user_input.each_with_index do |value, index| next unless value == @secret_code_for_comparison[index] @secret_code_for_comparison[index] = nil @user_input[index] = nil @matching[:place] += 1 end end def presence_matches @user_input.compact.each do |value| if @secret_code_for_comparison.include?(value) @matching[:presence] += 1 @secret_code_for_comparison.delete_at(@secret_code_for_comparison.index(value)) end end end def format_response AlexCodebreaker::Modules::Settings::MATCHING[:place] * @matching[:place] + AlexCodebreaker::Modules::Settings::MATCHING[:presence] * @matching[:presence] end end end