Class: CodeBrkrGameTraining::Game

Inherits:
Object
  • Object
show all
Extended by:
FileOperations, Statistics
Includes:
Validator
Defined in:
lib/code_brkr_game_training/entities/game.rb

Overview

Class for game

Constant Summary collapse

GameError =
Class.new(StandardError)

Constants included from Statistics

Statistics::SAVE_DIRECTORY, Statistics::SAVE_FILE

Constants included from Validator

Validator::DataValidError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Statistics

full_statistics, save_game_results!

Methods included from FileOperations

load_from_file, save_to_file

Methods included from Validator

#check_contain_hash_key, #check_length, #check_type

Constructor Details

#initialize(user, difficulty) ⇒ Game

Returns a new instance of Game.



15
16
17
18
19
20
21
22
# File 'lib/code_brkr_game_training/entities/game.rb', line 15

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

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

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



13
14
15
# File 'lib/code_brkr_game_training/entities/game.rb', line 13

def code
  @code
end

#difficultyObject (readonly)

Returns the value of attribute difficulty.



13
14
15
# File 'lib/code_brkr_game_training/entities/game.rb', line 13

def difficulty
  @difficulty
end

#userObject (readonly)

Returns the value of attribute user.



13
14
15
# File 'lib/code_brkr_game_training/entities/game.rb', line 13

def user
  @user
end

Instance Method Details

#code_guessing_attempt(digits_arr) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/code_brkr_game_training/entities/game.rb', line 24

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, 

#game_hintObject



35
36
37
38
39
40
# File 'lib/code_brkr_game_training/entities/game.rb', line 35

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