Sha256: 1e678514869fd970c4f6b6148527e759c3c0ab44d1016286dd9d6b26187a3524

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

require_relative 'autoload'
require_relative 'game'

class GuessHandler
  def respond_to_guess(game, input, code)
    Validations.validate_guess(input)
    return game.give_a_hint if input == 'hint'

    @result = []
    @code_clone = code.clone
    @numbers = input.split('').map(&:to_i)
    return 'win' if @numbers == code

    assess_guess(@code_clone, @numbers)
    attempts_handler(game)
  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

  def attempts_handler(game)
    game.user.attempts[:used] += 1
    if game.user.attempts[:used] >= game.user.attempts[:all]
      'loss'
    else
      @result.delete_if do |value|
        (1..6).include?(value)
      end.sort
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
codebreaker_kirill-0.2.20 lib/codebreaker_kirill/guess_handler.rb
codebreaker_kirill-0.2.19 lib/codebreaker_kirill/guess_handler.rb