Sha256: 0a852f674dda33593c68d1965f15d5fb781cfc5549419b4cc9f2534d3f19ae8f

Contents?: true

Size: 1.74 KB

Versions: 2

Compression:

Stored size: 1.74 KB

Contents

require_relative 'src/autoloader'

class Codebreaker
  attr_reader :secret_code, :attempts, :hints

  def initialize
    @data_base_manager = DataBaseManager.new
  end

  def start_game(difficulty, player_name)
    @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
    @data_base_manager.save_game_results([@player_name, @difficulty,
                                          @total_attempts, attempts_used,
                                          @total_hints, hints_used])
  end

  def load_game_results
    @data_base_manager.load_game_results
  end

  private

  def init_difficulty_stats(difficulty)
    @total_attempts, @total_hints = case difficulty
                                    when 0 then [15, 2]
                                    when 1 then [10, 1]
                                    else [5, 1]
                                    end
  end

  def generate_secret_code
    @secret_code = Factory.new_secret_code
    @verifier = CodeVerifier.new(@secret_code)
    @code_for_hints = @secret_code.clone
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
codebreaker_vv-2.0.0 lib/codebreaker.rb
codebreaker_vv-1.0.0 lib/codebreaker.rb