Sha256: 6a3f3b283a5d05566243150288f1ac1ca76ce82c56978eadc1065e8358a7bd31

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

# Main Gem module
module CodeBrkrGameTraining
  # Class to control the difficulty of the game
  class DifficultyController
    include Validator

    GAME_DIFFICULTIES = {
      hell: { name: 'hell', attempts: 5, hints: 1 },
      medium: { name: 'medium', attempts: 10, hints: 1 },
      easy: { name: 'easy', attempts: 15, hints: 2 }
    }.freeze

    attr_reader :init_values, :actual_values

    def initialize(difficulty_value)
      @value_name = difficulty_value
      validate

      @init_values = GAME_DIFFICULTIES[difficulty_value.to_sym]
      @actual_values = @init_values.clone
    end

    def guessing_attempts_available?
      @actual_values[:attempts].positive?
    end

    def guessing_attempts_decrement!
      guessing_attempts_decrement_permissible_check
      @actual_values[:attempts] -= 1
    end

    def hints_available?
      @actual_values[:hints].positive?
    end

    def hints_decrement!
      hints_decrement_permissible_check
      @actual_values[:hints] -= 1
    end

    private

    def validate
      check_contain_hash_key(@value_name, GAME_DIFFICULTIES)
    end

    def guessing_attempts_decrement_permissible_check
      raise GameError, 'attempts_guessing_exhausted' unless guessing_attempts_available?
    end

    def hints_decrement_permissible_check
      raise GameError, 'hints_exhausted' unless hints_available?
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
code_brkr_game_training-0.9.2 lib/code_brkr_game_training/entities/difficulty_controller.rb
code_brkr_game_training-0.9.1 lib/code_brkr_game_training/entities/difficulty_controller.rb
code_brkr_game_training-0.8.5 lib/code_brkr_game_training/entities/difficulty_controller.rb
code_brkr_game_training-0.8.0 lib/code_brkr_game_training/entities/difficulty_controller.rb