Sha256: b6b72816c1e997701dc310e48ed55c56f83f5d862514930cfae137fef8db714e

Contents?: true

Size: 871 Bytes

Versions: 3

Compression:

Stored size: 871 Bytes

Contents

# frozen_string_literal: true

module CodebreakerKirill
  class GuessHandler
    include Settings

    def initialize(input, code)
      Validations.validate_guess(input)
      @input = input.each_char.map(&:to_i)
      @code = code.clone
      @result = []
    end

    def call
      check_same_indexes
      check_different_indexes
      @result
    end

    def check_same_indexes
      @input.each_index do |index|
        next unless @input[index] == @code[index]

        @result << Settings::POSITIVE_RESPONSE
        @input[index], @code[index] = nil
      end
    end

    def check_different_indexes
      @input.each_with_index do |value, _index|
        next unless !value.nil? && @code.include?(value)

        @result << Settings::NEGATIVE_RESPONSE
        @input[@input.find_index(value)], @code[@code.find_index(value)] = nil
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
codebreaker_kirill-2.0.0 lib/codebreaker_kirill/guess_handler.rb
codebreaker_kirill-1.3.7 lib/codebreaker_kirill/guess_handler.rb
codebreaker_kirill-1.3.6 lib/codebreaker_kirill/guess_handler.rb