Sha256: 2e10882c2499b1a9336f8e14643ed47af523647fdae7f95826743f2699715618

Contents?: true

Size: 731 Bytes

Versions: 1

Compression:

Stored size: 731 Bytes

Contents

# frozen_string_literal: true

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

    assess_guess(@code_clone, @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_clone[index] = '+' if element == @code_clone[index] }
    @result = code.each_with_index do |el, index|
      if input.include?(el)
        @code_clone[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.12 lib/codebreaker_kirill/guess_handler.rb