Class: Codebreakergem::Game
- Inherits:
-
Object
- Object
- Codebreakergem::Game
- Includes:
- Checker
- Defined in:
- lib/classes/game.rb
Constant Summary collapse
- DIGIT_NUMBER =
4
- CORRECT_PLACE_SYMBOL =
'+'
- INCORRECT_PLACE_SYMBOL =
'-'
- FILE =
File.(__dir__ + '/../data/data.yml')
- PERMITTED_CLASSES =
[Codebreakergem::User, Codebreakergem::Game, Codebreakergem::Easy, Codebreakergem::Hell, Codebreakergem::Medium, Symbol].freeze
Constants included from Checker
Instance Attribute Summary collapse
-
#difficulty ⇒ Object
Returns the value of attribute difficulty.
-
#secret_code ⇒ Object
readonly
Returns the value of attribute secret_code.
-
#statistics ⇒ Object
Returns the value of attribute statistics.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#initialize(user, difficulty_title) ⇒ Game
constructor
A new instance of Game.
- #save ⇒ Object
- #show_hint ⇒ Object
- #to_h ⇒ Object
- #try_guess(guess) ⇒ Object
Methods included from Checker
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
#difficulty ⇒ Object
Returns the value of attribute difficulty.
20 21 22 |
# File 'lib/classes/game.rb', line 20 def difficulty @difficulty end |
#secret_code ⇒ Object (readonly)
Returns the value of attribute secret_code.
19 20 21 |
# File 'lib/classes/game.rb', line 19 def secret_code @secret_code end |
#statistics ⇒ Object
Returns the value of attribute statistics.
20 21 22 |
# File 'lib/classes/game.rb', line 20 def statistics @statistics end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
19 20 21 |
# File 'lib/classes/game.rb', line 19 def user @user end |
Instance Method Details
#save ⇒ Object
48 49 50 |
# File 'lib/classes/game.rb', line 48 def save FileWorker.add_to_file(FILE, to_h) end |
#show_hint ⇒ Object
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_h ⇒ Object
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 |