Sha256: 784535954a01068ba0f23c7a70d88c074d34c8ab08363a5ea0f418a03e1990a8

Contents?: true

Size: 674 Bytes

Versions: 1

Compression:

Stored size: 674 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

    assess_guess(code, @numbers)
    game.attempts[:used] += 1
    game.attempts[:used] >= game.attempts[:all] ? 'loss' : @result.delete_if { |value| (1..6).include?(value) }.sort
  end

  def assess_guess(code, input)
    input.each_with_index { |element, index| code[index] = '+' if element == code[index] }
    @result = code.each_with_index do |el, index|
      if input.include?(el)
        code[index] = '-'
        input.slice!(@numbers.index(el))
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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