Sha256: 6a8c4c43a1dede6fc5b8e2fdada93b4275b86cc699bd1df642c48b1ddf70789b

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module Codebreaker
  class Codemaker
    NON_OCCURRING_VALUE_FIRST = 'X'
    NON_OCCURRING_VALUE_SECOND = 'Y'
    NON_OCCURRING_VALUE_THIRD = 'Z'

    attr_reader :secret_code, :response

    def initialize
      @secret_code = ''
      @response = { in_plase: 0, out_of_place: 0 }
    end

    def generate_secret_code(amout = AMOUNT_OF_NUMBERS, range = RANGE_OF_NUMBERS)
      array_of_numbers = []
      amout.times { array_of_numbers << rand(range) }
      @secret_code = array_of_numbers.join.to_i
    end

    def check(guess_array, secret_code_array)
      guess_array.each_with_index do |number, index|
        next unless number == secret_code_array[index]

        @response[:in_plase] += 1
        guess_array[index] = NON_OCCURRING_VALUE_FIRST
        secret_code_array[index] = NON_OCCURRING_VALUE_SECOND
      end
      guess_array.each do |number|
        if secret_code_array.include?(number)
          @response[:out_of_place] += 1
          secret_code_array[secret_code_array.index(number)] = NON_OCCURRING_VALUE_THIRD
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
codebreaker_sn-0.1.0 lib/codebreaker/codemaker.rb