Class: CodeFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/codebreaker/code_formatter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(maker_code = [], user_code = []) ⇒ CodeFormatter

Returns a new instance of CodeFormatter.



7
8
9
10
11
# File 'lib/codebreaker/code_formatter.rb', line 7

def initialize(maker_code = [], user_code = [])
  @maker_code = maker_code
  @user_code = user_code
  @result = []
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



2
3
4
# File 'lib/codebreaker/code_formatter.rb', line 2

def result
  @result
end

Class Method Details

.call(*args) ⇒ Object



3
4
5
# File 'lib/codebreaker/code_formatter.rb', line 3

def self.call(*args)
  new(*args).call
end

Instance Method Details

#callObject



13
14
15
16
# File 'lib/codebreaker/code_formatter.rb', line 13

def call
  format_plus.format_minus
  @result
end

#format_minusObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/codebreaker/code_formatter.rb', line 30

def format_minus
  @user_code.each do |number|
    next unless @maker_code.include?(number)

    @result << '-'
    clear_number(@maker_code, @maker_code.index(number))
    clear_number(@user_code, @user_code.index(number))
  end
  code_compact
  self
end

#format_plusObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/codebreaker/code_formatter.rb', line 18

def format_plus
  @user_code.zip(@maker_code).each_with_index do |numbers, index|
    next unless numbers.first == numbers.last

    @result << '+'
    clear_number(@maker_code, index)
    clear_number(@user_code, index)
  end
  code_compact
  self
end