Class: Codebreaker::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
Returns the value of attribute attempts.
-
#hints ⇒ Object
readonly
Returns the value of attribute hints.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#status ⇒ Object
Returns the value of attribute status.
-
#total_attempts ⇒ Object
readonly
Returns the value of attribute total_attempts.
-
#total_hints ⇒ Object
readonly
Returns the value of attribute total_hints.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
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)
Returns the value of attribute attempts.
13 14 15 |
# File 'lib/game.rb', line 13 def attempts @attempts end |
#hints ⇒ Object (readonly)
Returns the value of attribute hints.
13 14 15 |
# File 'lib/game.rb', line 13 def hints @hints end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
13 14 15 |
# File 'lib/game.rb', line 13 def level @level end |
#status ⇒ Object
Returns the value of attribute status.
14 15 16 |
# File 'lib/game.rb', line 14 def status @status end |
#total_attempts ⇒ Object (readonly)
Returns the value of attribute total_attempts.
13 14 15 |
# File 'lib/game.rb', line 13 def total_attempts @total_attempts end |
#total_hints ⇒ Object (readonly)
Returns the value of attribute total_hints.
13 14 15 |
# File 'lib/game.rb', line 13 def total_hints @total_hints end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
13 14 15 |
# File 'lib/game.rb', line 13 def user @user end |
Instance Method Details
#add_hints_attempts(level) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/game.rb', line 31 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
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/game.rb', line 47 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
39 40 41 42 43 44 45 |
# File 'lib/game.rb', line 39 def take_hint raise I18n.t(:out_of_hints) if @hints.zero? @hints -= 1 @secret_code.digits.shuffle.pop end |