Class: Matching

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game) ⇒ Matching

Returns a new instance of Matching.



4
5
6
# File 'lib/services/matching_service.rb', line 4

def initialize(game)
  @game = game
end

Instance Attribute Details

#gameObject (readonly)

Returns the value of attribute game.



2
3
4
# File 'lib/services/matching_service.rb', line 2

def game
  @game
end

Instance Method Details

#check_the_codeObject



38
39
40
41
42
# File 'lib/services/matching_service.rb', line 38

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

#create_responseObject



8
9
10
11
12
13
# File 'lib/services/matching_service.rb', line 8

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

#current_secret_number(index) ⇒ Object



30
31
32
# File 'lib/services/matching_service.rb', line 30

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

#current_user_number(index) ⇒ Object



34
35
36
# File 'lib/services/matching_service.rb', line 34

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

#exact_matchesObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/services/matching_service.rb', line 15

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

#remove_verified_number(number) ⇒ Object



44
45
46
47
# File 'lib/services/matching_service.rb', line 44

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

#rest_matchesObject



26
27
28
# File 'lib/services/matching_service.rb', line 26

def rest_matches
  @secret_code_clone & game.user_code
end