Class: Codebreaker::Game
- Inherits:
-
Object
- Object
- Codebreaker::Game
- Includes:
- Constants::Shared, Validations
- Defined in:
- lib/codebreaker/game.rb
Constant Summary
Constants included from Constants::Shared
Constants::Shared::CORRECT_RANGE, Constants::Shared::LENGTH_GOOD, Constants::Shared::REGULAR_FOR_CODE, Constants::Shared::TYPE_OF_DIFFICULTY, Constants::Shared::WINNING_INDEX
Constants included from Validations
Validations::ERROR_GUESS, Validations::ERROR_NAME, Validations::MAX_LENGTH, Validations::MIN_LENGTH
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
Returns the value of attribute attempts.
-
#hints ⇒ Object
readonly
Returns the value of attribute hints.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
-
#win ⇒ Object
readonly
Returns the value of attribute win.
Class Method Summary collapse
Instance Method Summary collapse
- #give_hints ⇒ Object
-
#initialize(user:, type_of_difficulty:) ⇒ Game
constructor
A new instance of Game.
- #my_guess(guess) ⇒ Object
Methods included from Validations
Constructor Details
#initialize(user:, type_of_difficulty:) ⇒ Game
Returns a new instance of Game.
10 11 12 13 14 15 16 17 18 |
# File 'lib/codebreaker/game.rb', line 10 def initialize(user:, type_of_difficulty:) @win = false @user = user @secret_code = RandomSecretCode.call @attempts = TYPE_OF_DIFFICULTY[type_of_difficulty][:attempts] @hints = TYPE_OF_DIFFICULTY[type_of_difficulty][:hints] @secret_code_for_hints = @secret_code.shuffle @type = type_of_difficulty end |
Instance Attribute Details
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
8 9 10 |
# File 'lib/codebreaker/game.rb', line 8 def attempts @attempts end |
#hints ⇒ Object (readonly)
Returns the value of attribute hints.
8 9 10 |
# File 'lib/codebreaker/game.rb', line 8 def hints @hints end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
8 9 10 |
# File 'lib/codebreaker/game.rb', line 8 def type @type end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
8 9 10 |
# File 'lib/codebreaker/game.rb', line 8 def user @user end |
#win ⇒ Object (readonly)
Returns the value of attribute win.
8 9 10 |
# File 'lib/codebreaker/game.rb', line 8 def win @win end |
Class Method Details
.type_of_difficulty_validate?(difficulty) ⇒ Boolean
38 39 40 |
# File 'lib/codebreaker/game.rb', line 38 def self.type_of_difficulty_validate?(difficulty) TYPE_OF_DIFFICULTY.key?(difficulty.to_sym) end |
.user_validate_in_game?(user) ⇒ Boolean
34 35 36 |
# File 'lib/codebreaker/game.rb', line 34 def self.user_validate_in_game?(user) user.is_a?(User) end |
Instance Method Details
#give_hints ⇒ Object
30 31 32 |
# File 'lib/codebreaker/game.rb', line 30 def give_hints @hints.positive? ? check_hints : raise(StandardError, I18n.t(:message_for_hints)) end |
#my_guess(guess) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/codebreaker/game.rb', line 20 def my_guess(guess) validate_for_guess(guess) raise(StandardError, I18n.t(:message_for_attempts)) unless @attempts.positive? @result = GuessChecker.new(guess.chars, @secret_code).check_guess @attempts -= 1 @win = won? @result end |