Sha256: bb4fc3b9032e7c52a5b4e3e6b22bdd5622bcd3ad15a4864c3e7932c14e58ace5

Contents?: true

Size: 679 Bytes

Versions: 1

Compression:

Stored size: 679 Bytes

Contents

# frozen_string_literal: true

module GuessHandler
  def respond_to_guess(game, input, code)
    result = []
    numbers = input.split('').map(&:to_i)
    return 'win' if numbers == code

    game.assess_guess(numbers, code, result)
    game.attempts[:available] -= 1
    game.attempts[:used] += 1
    game.attempts[:available].zero? ? 'loss' : result
  end

  def assess_guess(numbers, code, result)
    code_copy = code.clone
    numbers.each_with_index do |element, index|
      if element == code[index]
        result << '+'
      elsif code_copy.include?(element)
        result << '-'
        code_copy.delete_at(code_copy.find_index(element))
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
codebreaker_kirill-0.2.9 lib/codebreaker_kirill/guess_handler.rb