Sha256: 9a3ed4f4979a0183eb3eff4d05080df48c97b0a4e4c4a784ef08f9ae6bf24718

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

# 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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
codebreaker_vv-0.2.0 lib/codebreaker.rb