Sha256: 242f4071782100b1fc0b97c3cfe334f698b41453fb488ae52db9eaaa84b9eaab

Contents?: true

Size: 797 Bytes

Versions: 5

Compression:

Stored size: 797 Bytes

Contents

module Codebreaker
  #
  class Marker
    attr_accessor :match

    def initialize(guess, secret_code)
      @guess = guess
      @secret_code = secret_code
      @match = []
    end

    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

    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

    def result
      find_matches
      @match.join
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
codebreaker_web-0.1.4 lib/codebreaker/marker.rb
codebreaker_web-0.1.3 lib/codebreaker/marker.rb
codebreaker_web-0.1.2 lib/codebreaker/marker.rb
codebreaker_web-0.1.1 lib/codebreaker/marker.rb
codebreaker_web-0.1.0 lib/codebreaker/marker.rb