Sha256: 754e684c1cc521c2bd1b538a60087bb81a98b71d100bd8cd4af09067b4ef3db5

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

# Main Gem module
module CodeBrkrGameTraining
  # Class for game
  class Game
    include Validator
    extend Statistics
    extend FileOperations

    GameError = Class.new(StandardError)

    attr_reader :user, :difficulty, :code

    def initialize(user, difficulty)
      @user = user
      @difficulty = difficulty
      validate

      @code = Code.new
      @hints_indexes = []
    end

    def code_guessing_attempt(digits_arr)
      @difficulty.guessing_attempts_decrement!
      result = @code.code_check(digits_arr)
      if result[:in_positions] == Code::GAME_NUMBERS[:count]
        result = { win: true }
      elsif !@difficulty.guessing_attempts_available?
        result = { loss: true, сorrect_answer: @code.to_s }
      end
      result
    end

    def game_hint
      @difficulty.hints_decrement!
      hint = @code.random_digit(@hints_indexes)
      @hints_indexes << hint[:index]
      hint[:digit]
    end

    private

    def validate
      check_type(@user, User)
      check_type(@difficulty, DifficultyController)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
code_brkr_game_training-0.8.5 lib/code_brkr_game_training/entities/game.rb
code_brkr_game_training-0.8.0 lib/code_brkr_game_training/entities/game.rb
code_brkr_game_training-0.7.5 lib/code_brkr_game_training/entities/game.rb