# require_relative 'codebreaker/version' require_relative 'src/autoloader' module Codebreaker class Error < StandardError; end class << self attr_reader :secret_code, :attempts, :hints def start_game(difficulty, player_name) @total_attempts = 0 @total_hints = 1 @difficulty = difficulty @player_name = player_name init_difficulty_stats(difficulty) @hints = @total_hints @attempts = @total_attempts generate_secret_code end def check_code_and_give_result(user_code) answer = @verifier.verify_user_code(user_code) @attempts -= 1 if answer == '++++' "#{answer} #{Locale::PLAYER_WIN}" elsif @attempts <= 0 "#{answer} #{Locale::PLAYER_LOSE}" else answer end end def give_hint if @hints <= 0 Locale::NO_MORE_HINTS_AVAILABLE else @hints -= 1 hint = @code_for_hints.sample @code_for_hints.delete_at(@code_for_hints.index(hint)) hint end end def save_game_result attempts_used = @total_attempts - @attempts hints_used = @total_hints - @hints DataBaseManager.instance.save_game_results([@player_name, @difficulty, @total_attempts, attempts_used, @total_hints, hints_used]) end def load_game_results DataBaseManager.instance.load_game_results end private def init_difficulty_stats(difficulty) case difficulty when 0 @total_attempts = 15 @total_hints = 2 when 1 @total_attempts = 10 else @total_attempts = 5 end end def generate_secret_code @secret_code = Factory.new.new_secret_code @verifier = CodeVerifier.new(@secret_code) @code_for_hints = @secret_code.clone end end end