Class: Game
Constant Summary collapse
- GAME_DIFFICULTIES =
{ simple: { attempts: 15, hints: 2 }, medium: { attempts: 10, hints: 1 }, hell: { attempts: 5, hints: 1 } }.freeze
- WIN =
:win
- LOSS =
:loss
- PLUS =
'+'
- MINUS =
'-'
- NUMBER_OF_ENCRYPTED_DIGITS =
4
- VALUES_FOR_ENCRYPTED_DIGITS =
(1..6).to_a
Constants included from Storage
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
private.
-
#hints ⇒ Object
readonly
private.
-
#level ⇒ Object
readonly
private.
-
#secret_code ⇒ Object
readonly
private.
-
#status ⇒ Object
public.
-
#total_attempts ⇒ Object
readonly
private.
-
#total_hints ⇒ Object
readonly
private.
-
#user ⇒ Object
readonly
private.
Instance Method Summary collapse
- #add_hints_attempts(level) ⇒ Object
- #guess(user_variant) ⇒ Object
-
#initialize(user_name) ⇒ Game
constructor
A new instance of Game.
- #take_hint ⇒ Object
Methods included from Storage
#show_statistics, #statistics_with_raiting, #store_game
Constructor Details
Instance Attribute Details
#attempts ⇒ Object (readonly)
private
8 9 10 |
# File 'lib/game.rb', line 8 def attempts @attempts end |
#hints ⇒ Object (readonly)
private
8 9 10 |
# File 'lib/game.rb', line 8 def hints @hints end |
#level ⇒ Object (readonly)
private
8 9 10 |
# File 'lib/game.rb', line 8 def level @level end |
#secret_code ⇒ Object (readonly)
private
8 9 10 |
# File 'lib/game.rb', line 8 def secret_code @secret_code end |
#status ⇒ Object
public
11 12 13 |
# File 'lib/game.rb', line 11 def status @status end |
#total_attempts ⇒ Object (readonly)
private
8 9 10 |
# File 'lib/game.rb', line 8 def total_attempts @total_attempts end |
#total_hints ⇒ Object (readonly)
private
8 9 10 |
# File 'lib/game.rb', line 8 def total_hints @total_hints end |
#user ⇒ Object (readonly)
private
8 9 10 |
# File 'lib/game.rb', line 8 def user @user end |
Instance Method Details
#add_hints_attempts(level) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/game.rb', line 28 def add_hints_attempts(level) @level = level.to_sym @total_attempts = Game::GAME_DIFFICULTIES[@level][:attempts] @total_hints = Game::GAME_DIFFICULTIES[@level][:hints] @attempts = @total_attempts @hints = @total_hints end |
#guess(user_variant) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/game.rb', line 44 def guess(user_variant) return @status = Game::LOSS if @attempts.zero? answer = SecretCode.new(user_variant.to_i) @attempts -= 1 if answer.valid? if answer.value == @secret_code store_game return @status = Game::WIN end check_user_answer(answer.value) end |
#take_hint ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/game.rb', line 36 def take_hint raise I18n.t(:out_of_hints) if @hints.zero? @hints -= 1 @secret_code.digits.shuffle.pop end |