Sha256: 65c8330f570739b8cc9bd28e61ce5ba9c81235c4f42f795a6013d31b0655ce7d

Contents?: true

Size: 911 Bytes

Versions: 4

Compression:

Stored size: 911 Bytes

Contents

class CodeVerifier
  def initialize(secret_code)
    @secret_code = secret_code
    @code = []
    @user_code = []
  end

  def verify_user_code(user_code_)
    @code = @secret_code.clone
    @user_code = user_code_
    "#{check_matches}#{check_includes}"
  end

  private

  def check_matches
    answer = ''
    @user_code.to_enum.with_index.reverse_each do |_, index|
      next unless @code[index] == @user_code[index]

      answer += '+'
      @user_code.delete_at(index)
      @code.delete_at(index)
    end
    answer
  end

  def check_includes
    answer = ''
    @user_code.to_enum.with_index.reverse_each do |element, user_element_index|
      next unless @code.include?(element)

      answer += '-'
      @user_code.delete_at(user_element_index)
      code_element = @code.find { |new_element| new_element == element }
      @code.delete_at(@code.index(code_element))
    end
    answer
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
codebreaker_vv-2.0.0 lib/src/code_verifier.rb
codebreaker_vv-1.0.0 lib/src/code_verifier.rb
codebreaker_vv-0.2.0 lib/src/code_verifier.rb
codebreaker_vv-0.1.0 lib/src/code_verifier.rb