Class: Codebreaker::Marker
- Inherits:
-
Object
- Object
- Codebreaker::Marker
- Defined in:
- lib/codebreaker/marker.rb
Instance Attribute Summary collapse
-
#match ⇒ Object
Returns the value of attribute match.
Instance Method Summary collapse
- #find_matches ⇒ Object
-
#initialize(guess, secret_code) ⇒ Marker
constructor
A new instance of Marker.
- #mark_minusses(number_matches) ⇒ Object
- #result ⇒ Object
Constructor Details
#initialize(guess, secret_code) ⇒ Marker
Returns a new instance of Marker.
6 7 8 9 10 |
# File 'lib/codebreaker/marker.rb', line 6 def initialize(guess, secret_code) @guess = guess @secret_code = secret_code @match = [] end |
Instance Attribute Details
#match ⇒ Object
Returns the value of attribute match.
4 5 6 |
# File 'lib/codebreaker/marker.rb', line 4 def match @match end |
Instance Method Details
#find_matches ⇒ Object
12 13 14 15 16 17 |
# File 'lib/codebreaker/marker.rb', line 12 def find_matches compared_guess = @guess.zip @secret_code exact_matches, number_matches = compared_guess.partition { |arr| arr[0] == arr[1] } @match << '+' * exact_matches.size mark_minusses(number_matches) unless number_matches.size.zero? end |
#mark_minusses(number_matches) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/codebreaker/marker.rb', line 19 def mark_minusses(number_matches) secret_code, guess = number_matches.transpose guess.each do |number| next unless secret_code.include? number @match << '-' secret_code[secret_code.find_index(number)] = nil end end |
#result ⇒ Object
28 29 30 31 |
# File 'lib/codebreaker/marker.rb', line 28 def result find_matches @match.join end |