class Game::CodeBreaker

Attributes

current_stat[RW]

Public Class Methods

new(player:, difficult:) click to toggle source
# 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

Public Instance Methods

can_use_attempts?() click to toggle source
# 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?() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/codebreaker/game/code_breaker.rb, line 25
def each_element_as(arr, value)
  return [] if arr.nil?

  arr.map { |_element| value }
end
hint() click to toggle source
# 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:) click to toggle source
# 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) click to toggle source
# File lib/codebreaker/game/code_breaker.rb, line 21
def player_win?(guess)
  guess.count(true) == CodeMaker::CODE_DIGITS_COUNT
end
position_checker(guess) click to toggle source
# 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