Class: CompareCodes
- Inherits:
-
Object
- Object
- CompareCodes
- 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
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#guess ⇒ Object
readonly
Returns the value of attribute guess.
Instance Method Summary collapse
- #color_match ⇒ Object
-
#initialize(secret_code, evaluated_input) ⇒ CompareCodes
constructor
A new instance of CompareCodes.
- #match? ⇒ Boolean
- #position_color_match ⇒ Object
- #winner? ⇒ Boolean
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
#code ⇒ Object (readonly)
Returns the value of attribute code.
7 8 9 |
# File 'lib/compare_codes.rb', line 7 def code @code end |
#guess ⇒ Object (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_match ⇒ Object
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
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_match ⇒ Object
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
23 24 25 |
# File 'lib/compare_codes.rb', line 23 def winner? match? end |