Sha256: 07bd3b96cb3eead5e34d96dec01b5800f43d0ad1b4317eb0f7a8df85df245db2

Contents?: true

Size: 900 Bytes

Versions: 4

Compression:

Stored size: 900 Bytes

Contents

# frozen_string_literal: true

require_relative 'autoload'

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

4 entries across 4 versions & 1 rubygems

Version Path
codebreaker_kirill-1.3.4 lib/codebreaker_kirill/guess_handler.rb
codebreaker_kirill-1.3.2 lib/codebreaker_kirill/guess_handler.rb
codebreaker_kirill-1.3.1 lib/codebreaker_kirill/guess_handler.rb
codebreaker_kirill-1.3.0 lib/codebreaker_kirill/guess_handler.rb