Class: CodebreakerKirill::GuessHandler

Inherits:
Object
  • Object
show all
Includes:
Settings
Defined in:
lib/codebreaker_kirill/guess_handler.rb

Constant Summary

Constants included from Settings

Settings::CODE_LENGTH, Settings::DIFFICULTY, Settings::GUESS_LENGTH, Settings::NAME_MAX_LENGTH, Settings::NAME_MIN_LENGTH, Settings::NEGATIVE_RESPONSE, Settings::POSITIVE_RESPONSE, Settings::RANDOM_RANGE

Instance Method Summary collapse

Constructor Details

#initialize(input, code) ⇒ GuessHandler

Returns a new instance of GuessHandler.



7
8
9
10
11
12
# File 'lib/codebreaker_kirill/guess_handler.rb', line 7

def initialize(input, code)
  Validations.validate_guess(input)
  @input = input.each_char.map(&:to_i)
  @code = code.clone
  @result = []
end

Instance Method Details

#callObject



14
15
16
17
18
# File 'lib/codebreaker_kirill/guess_handler.rb', line 14

def call
  check_same_indexes
  check_different_indexes
  @result
end

#check_different_indexesObject



29
30
31
32
33
34
35
36
# File 'lib/codebreaker_kirill/guess_handler.rb', line 29

def check_different_indexes
  @input.each_with_index do |value, _index|
    next unless !value.nil? && @code.include?(value)

    @result << Settings::NEGATIVE_RESPONSE
    @input[@input.find_index(value)], @code[@code.find_index(value)] = nil
  end
end

#check_same_indexesObject



20
21
22
23
24
25
26
27
# File 'lib/codebreaker_kirill/guess_handler.rb', line 20

def check_same_indexes
  @input.each_index do |index|
    next unless @input[index] == @code[index]

    @result << Settings::POSITIVE_RESPONSE
    @input[index], @code[index] = nil
  end
end