Sha256: ff6a73dcd3cb2164912e34922458491f6bb9d92c83f410138797d21e40ef3fe9

Contents?: true

Size: 982 Bytes

Versions: 2

Compression:

Stored size: 982 Bytes

Contents

# frozen_string_literal: true

require_relative 'autoload'

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

  def call
    add_pluses(@input, @code)
    add_minuses(@input, @code)
    @result
  end

  def add_pluses(input, code)
    input.each_with_index do |element, index|
      next unless element == code[index]

      @result << Settings::POSITIVE
      input[input.find_index(element)] = ''
      code[index] = ''
    end
    input.delete('')
    code.delete('')
  end

  def add_minuses(input, code)
    input.each_with_index do |element, _index|
      next unless code.include?(element)

      @result << Settings::NEGATIVE
      input[input.find_index(element)] = ''
      code[code.find_index(element)] = ''
    end
  end

  private

  def validation(input)
    Validations.validate_guess(input)
  rescue StandardError => e
    puts e.message
    nil
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
codebreaker_kirill-1.1.1 lib/codebreaker_kirill/guess_handler.rb
codebreaker_kirill-1.1.0 lib/codebreaker_kirill/guess_handler.rb