Class: Codebreakergem::Game

Inherits:
Object
  • Object
show all
Includes:
Checker
Defined in:
lib/classes/game.rb

Constant Summary collapse

DIGIT_NUMBER =
4
CORRECT_PLACE_SYMBOL =
'+'
INCORRECT_PLACE_SYMBOL =
'-'
FILE =
File.expand_path(__dir__ + '/../data/data.yml')
PERMITTED_CLASSES =
[Codebreakergem::User,
Codebreakergem::Game,
Codebreakergem::Easy,
Codebreakergem::Hell,
Codebreakergem::Medium,
Symbol].freeze

Constants included from Checker

Checker::ALLOWED_LENGTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Checker

#validate_string_length

Constructor Details

#initialize(user, difficulty_title) ⇒ Game

Returns a new instance of Game.



22
23
24
25
26
# File 'lib/classes/game.rb', line 22

def initialize(user, difficulty_title)
  @user = user
  @difficulty = difficulty_factory(difficulty_title)
  @secret_code = generate_code
end

Instance Attribute Details

#difficultyObject

Returns the value of attribute difficulty.



20
21
22
# File 'lib/classes/game.rb', line 20

def difficulty
  @difficulty
end

#secret_codeObject (readonly)

Returns the value of attribute secret_code.



19
20
21
# File 'lib/classes/game.rb', line 19

def secret_code
  @secret_code
end

#statisticsObject

Returns the value of attribute statistics.



20
21
22
# File 'lib/classes/game.rb', line 20

def statistics
  @statistics
end

#userObject (readonly)

Returns the value of attribute user.



19
20
21
# File 'lib/classes/game.rb', line 19

def user
  @user
end

Instance Method Details

#saveObject



48
49
50
# File 'lib/classes/game.rb', line 48

def save
  FileWorker.add_to_file(FILE, to_h)
end

#show_hintObject



28
29
30
31
32
33
# File 'lib/classes/game.rb', line 28

def show_hint
  return I18n.t(:no_hints) unless difficulty.hints.positive?

  difficulty.hints -= 1
  secret_code[difficulty.hints]
end

#to_hObject



52
53
54
55
# File 'lib/classes/game.rb', line 52

def to_h
  source = difficulty_factory(difficulty.title)
  formater(source, source.attempts - difficulty.attempts, source.hints - difficulty.hints)
end

#try_guess(guess) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/classes/game.rb', line 35

def try_guess(guess)
  if guess == secret_code
    save
    difficulty.attempts = 0
    return I18n.t(:victory_message)
  end

  difficulty.attempts -= 1
  return I18n.t(:out_of_attempts) unless difficulty.attempts.positive?

  make_guess(guess)
end