Class: CompareCodes

Inherits:
Object
  • Object
show all
Defined in:
lib/compare_codes.rb

Overview

take two arrays: test that input is array with 4 characters compare input provide feedback about number of color matches in guess compared to secret code provide feedback about number of position and color matches in guess compared to secret

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secret_code, evaluated_input) ⇒ CompareCodes

Returns a new instance of CompareCodes.



10
11
12
13
14
# File 'lib/compare_codes.rb', line 10

def initialize(secret_code, evaluated_input)
  @code = secret_code
  @guess = evaluated_input
  @counter = 0
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



7
8
9
# File 'lib/compare_codes.rb', line 7

def code
  @code
end

#guessObject (readonly)

Returns the value of attribute guess.



7
8
9
# File 'lib/compare_codes.rb', line 7

def guess
  @guess
end

Instance Method Details

#color_matchObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/compare_codes.rb', line 32

def color_match
  copy = @code.dup
  @guess.each do |matching_character|
    if copy.include?(matching_character)
      @counter += 1
      copy.delete_at(copy.index(matching_character))
    end
  end
  @counter
  #puts "you have #{@counter} of the colors in the correct position"
end

#match?Boolean

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/compare_codes.rb', line 18

def match?
  puts "this is code #{@code} and this is guess #{@guess}"
  @code == @guess
end

#position_color_matchObject



27
28
29
30
# File 'lib/compare_codes.rb', line 27

def position_color_match
  correct_position = @guess.each_with_index.count {|color,position| color == @code[position]}
  correct_position
end

#winner?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/compare_codes.rb', line 23

def winner?
  match?
end