Class: Game::CodeBreaker

Inherits:
Object
  • Object
show all
Includes:
Validation
Defined in:
lib/codebreaker/game/code_breaker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(player:, difficult:) ⇒ CodeBreaker

Returns a new instance of CodeBreaker.



7
8
9
10
11
12
# File 'lib/codebreaker/game/code_breaker.rb', line 7

def initialize(player:, difficult:)
  @current_stat = Statistic::StatisticRow.new(player: player, difficult_init: difficult)
  @code = Game::CodeMaker.new.code
  @hint = difficult.hint
  @hint.generate_hints(@code)
end

Instance Attribute Details

#current_statObject

Returns the value of attribute current_stat.



5
6
7
# File 'lib/codebreaker/game/code_breaker.rb', line 5

def current_stat
  @current_stat
end

Instance Method Details

#can_use_attempts?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/codebreaker/game/code_breaker.rb', line 49

def can_use_attempts?
  @current_stat.init_attempts_count > @current_stat.used_attempts_count
end

#can_use_hints?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/codebreaker/game/code_breaker.rb', line 53

def can_use_hints?
  @current_stat.init_hints_count > @current_stat.used_hints_count
end

#check_guess(guess) ⇒ Object



31
32
33
34
35
# File 'lib/codebreaker/game/code_breaker.rb', line 31

def check_guess(guess)
  position_result = position_checker(guess)
  include_result = digits_checker(guess, position_result)
  each_element_as(position_result, true) + each_element_as(include_result, false)
end

#digits_checker(guess, uncheck_digits) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/codebreaker/game/code_breaker.rb', line 37

def digits_checker(guess, uncheck_digits)
  result = []
  guess.map do |digit|
    result.push(digit) if !uncheck_digits.include?(digit) && @code.include?(digit) && !result.include?(digit)
  end
  result.compact
end

#each_element_as(arr, value) ⇒ Object



25
26
27
28
29
# File 'lib/codebreaker/game/code_breaker.rb', line 25

def each_element_as(arr, value)
  return [] if arr.nil?

  arr.map { |_element| value }
end

#hintObject



57
58
59
60
# File 'lib/codebreaker/game/code_breaker.rb', line 57

def hint
  @current_stat.used_hints_count += 1 if can_use_hints?
  @hint.hint
end

#my_guess(input_value:) ⇒ Object



14
15
16
17
18
19
# File 'lib/codebreaker/game/code_breaker.rb', line 14

def my_guess(input_value:)
  Validation::Guess.validation(input_value: input_value)
  input_value = input_value.split('').map(&:to_i)
  @current_stat.used_attempts_count += 1 if can_use_attempts?
  check_guess(input_value)
end

#player_win?(guess) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/codebreaker/game/code_breaker.rb', line 21

def player_win?(guess)
  guess.count(true) == CodeMaker::CODE_DIGITS_COUNT
end

#position_checker(guess) ⇒ Object



45
46
47
# File 'lib/codebreaker/game/code_breaker.rb', line 45

def position_checker(guess)
  @code.zip(guess).map { |code_digit, guess_digit| guess_digit if code_digit == guess_digit }.compact
end