Sha256: 578dea51f2402276094fd33b128ca083bec599fc076b06f61f2bad3a1ea32c94

Contents?: true

Size: 781 Bytes

Versions: 1

Compression:

Stored size: 781 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)
    @numbers.each_with_index do |element, index|
      if element == code[index]
        code[index] = '+'
        input[@numbers.index(element)] = '' end
    end

    @result = code.each_with_index do |el, index|
      if @numbers.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.13 lib/codebreaker_kirill/guess_handler.rb