class AlexCodebreaker::Game

Constants

STATUS

Attributes

current_comparison[R]
game_id[R]
secret_code[R]
session[R]
status[R]
used_hints[R]

Public Class Methods

new() click to toggle source
# File lib/alex_codebreaker/game.rb, line 9
def initialize
  @secret_code = AlexCodebreaker::SecretCodeGenerator.new.secret_code
  @secret_code_for_hint = @secret_code.clone.uniq
  @session = Session.new
  @used_hints = []
  @status = STATUS[:in_game]
end

Public Instance Methods

guess(user_input) click to toggle source
# File lib/alex_codebreaker/game.rb, line 23
def guess(user_input)
  return unless guess_validation(user_input)

  compare_codes(user_input)
  check_win_lose
end
hint() click to toggle source
# File lib/alex_codebreaker/game.rb, line 17
def hint
  return unless @session.check_hints

  process_hint
end

Private Instance Methods

check_win_lose() click to toggle source
# File lib/alex_codebreaker/game.rb, line 51
def check_win_lose
  checking_result = @session.check_attempts
  return @status = STATUS[:win] if @current_comparison == AlexCodebreaker::Modules::Settings::WIN_COMPARISON

  @status = STATUS[:lose] unless checking_result
end
compare_codes(user_input) click to toggle source
# File lib/alex_codebreaker/game.rb, line 41
def compare_codes(user_input)
  user_input = format_user_input(user_input)
  comparison = Comparison.new(user_input, @secret_code.clone)
  @current_comparison = comparison.response
end
format_user_input(user_input) click to toggle source
# File lib/alex_codebreaker/game.rb, line 47
def format_user_input(user_input)
  user_input.chars.map(&:to_i)
end
process_hint() click to toggle source
# File lib/alex_codebreaker/game.rb, line 32
def process_hint
  hint = if @secret_code_for_hint.one?
           @secret_code_for_hint.first
         else
           @secret_code_for_hint.delete(@secret_code_for_hint.sample)
         end
  @used_hints << hint
end