Sha256: bae4daf2254a05c8c698b9de64462a4b5aea2e8810c552953040daf4e83337c4

Contents?: true

Size: 1.09 KB

Versions: 10

Compression:

Stored size: 1.09 KB

Contents

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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
alex_codebreaker-0.2.3 lib/alex_codebreaker/comparison.rb
alex_codebreaker-0.2.2 lib/alex_codebreaker/comparison.rb
alex_codebreaker-0.2.1 lib/alex_codebreaker/comparison.rb
alex_codebreaker-0.2.0 lib/alex_codebreaker/comparison.rb
alex_codebreaker-0.1.13 lib/alex_codebreaker/comparison.rb
alex_codebreaker-0.1.12 lib/alex_codebreaker/comparison.rb
alex_codebreaker-0.1.11 lib/alex_codebreaker/comparison.rb
alex_codebreaker-0.1.10 lib/alex_codebreaker/comparison.rb
alex_codebreaker-0.1.9 lib/alex_codebreaker/comparison.rb
alex_codebreaker-0.1.8 lib/alex_codebreaker/comparison.rb