Sha256: 80a97a72ab7468e97076e55d9be3e67d0db8f6619f1946b73793a3c528ae0eca

Contents?: true

Size: 830 Bytes

Versions: 2

Compression:

Stored size: 830 Bytes

Contents

class CodeMaker
  include Validation

  attr_reader :verified_code, :code

  CORRECT_ANSWER = ['+', '+', '+', '+'].freeze

  def initialize
    @code = generate_random_code
    @verified_code = []
  end

  def verify(user_code)
    return format(user_code) if validate_code(user_code)

    raise CustomErrors::CodeTypeError
  end

  def check?
    verified_code == CORRECT_ANSWER
  end

  private

  def generate_random_code
    4.times.map { rand(1..6) }
  end

  def format(user_code)
    @verified_code = CodeFormatter.call(code, code_to_array(user_code))
  end

  def code_to_array(code)
    code.to_s.chars.map(&:to_i)
  end

  def validate_code(code)
    code = code_to_array(code)
    validate_max_length(code, 4) &&
      validate_min_length(code, 4) &&
      code.all? { |number| validate_range(number, 1, 6) }
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
codebreaker-engine-0.1.0 lib/codebreaker/codemaker.rb
codebreaker-1.0.0-0.1.0 lib/codebreaker/codemaker.rb