Sha256: 93c765b0fbc49ff370aa931979e8e5fde5cc7e8a6f0fa262a596ebc0911123d7

Contents?: true

Size: 847 Bytes

Versions: 9

Compression:

Stored size: 847 Bytes

Contents

module Codebreaker
  module Entities
    class Controller
      GUESSED_NUMBER_SYMBOL = '+'.freeze
      UNEXPECTED_NUMBER_SYMBOL = '-'.freeze

      attr_reader :guess, :code, :result

      def secret_code_handler(code, guess)
        @code = code.split('')
        @guess = guess.split('')
        guessed_numbers.join + guessed_numbers_error_posit.join
      end

      private

      def guessed_numbers
        code.map.with_index do |_key, index|
          next unless code[index] == guess[index]

          @guess[index], @code[index] = nil
          GUESSED_NUMBER_SYMBOL
        end
      end

      def guessed_numbers_error_posit
        guess.compact.map do |number|
          next unless @code.include?(number)

          @code.delete_at(code.index(number))
          UNEXPECTED_NUMBER_SYMBOL
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
codebreaker_artuomka-0.1.8 lib/codebreaker/entities/controller.rb
codebreaker_artuomka-0.1.7 lib/codebreaker/entities/controller.rb
codebreaker_artuomka-0.1.6 lib/codebreaker/entities/controller.rb
codebreaker_artuomka-0.1.5 lib/codebreaker/entities/controller.rb
codebreaker_artuomka-0.1.4 lib/codebreaker/entities/controller.rb
codebreaker_artuomka-0.1.3 lib/codebreaker/entities/controller.rb
codebreaker_artuomka-0.1.2 lib/codebreaker/entities/controller.rb
codebreaker_artuomka-0.1.1 lib/codebreaker/entities/controller.rb
codebreaker_artuomka-0.1.0 lib/codebreaker/entities/controller.rb