Sha256: 65899b52f34394c4ca6fa14b2c76f62a01d28e9d311c37deda51a062b59a3160

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 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 = {
      easy: { name: 'easy', attempts: 15, hints: 2 },
      medium: { name: 'medium', attempts: 10, hints: 1 },
      hell: { name: 'hell', attempts: 5, hints: 1 }
    }.freeze

    attr_reader :init_values, :actual_values

    def self.difficulty_levels_order
      GAME_DIFFICULTIES
        .values
        .sort_by { |difficulty| [difficulty[:attempts], difficulty[:hints]] }
        .collect { |difficulty| difficulty[:name] }
    end

    def initialize(dfclt_val)
      @value_name = dfclt_val
      validate

      @init_values = GAME_DIFFICULTIES[dfclt_val.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

1 entries across 1 versions & 1 rubygems

Version Path
code_brkr_game_training-0.7.2 lib/code_brkr_game_training/entities/difficulty_controller.rb