Sha256: 08ca3618d86f86bd29c886aaecaae9771472de6b3fb81902a490bbb6b6873541

Contents?: true

Size: 1.04 KB

Versions: 10

Compression:

Stored size: 1.04 KB

Contents

class Matching
  attr_reader :game

  def initialize(game)
    @game = game
  end

  def create_response
    @pluses = ''
    @minuses = ''
    check_the_code
    "#{@pluses}#{@minuses}"
  end

  def exact_matches
    exact_matches = []
    game.secret_code.each_index do |index|
      if current_secret_number(index) == current_user_number(index)
        exact_matches.push(current_secret_number(index))
        remove_verified_number(current_user_number(index))
      end
    end
    exact_matches
  end

  def rest_matches
    @secret_code_clone & game.user_code
  end

  def current_secret_number(index)
    game.secret_code.at(index)
  end

  def current_user_number(index)
    game.user_code.at(index)
  end

  def check_the_code
    @secret_code_clone = game.secret_code.clone
    exact_matches.length.times { @pluses += '+' }
    rest_matches.compact.length.times { @minuses += '-' }
  end

  def remove_verified_number(number)
    game.user_code[game.user_code.index(number)] = nil
    @secret_code_clone[@secret_code_clone.index(number)] = nil
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
codebreaker_paratskiy-0.2.4 lib/services/matching_service.rb
codebreaker_paratskiy-0.2.3 lib/services/matching_service.rb
codebreaker_paratskiy-0.2.2 lib/services/matching_service.rb
codebreaker_paratskiy-0.2.1 lib/services/matching_service.rb
codebreaker_paratskiy-0.2.0 lib/services/matching_service.rb
codebreaker_paratskiy-0.1.9 lib/services/matching_service.rb
codebreaker_paratskiy-0.1.8 lib/services/matching_service.rb
codebreaker_paratskiy-0.1.7 lib/services/matching_service.rb
codebreaker_paratskiy-0.1.6 lib/services/matching_service.rb
codebreaker_paratskiy-0.1.5 lib/services/matching_service.rb