Class: CodebreakerGem::Game
- Inherits:
-
Object
- Object
- CodebreakerGem::Game
- Defined in:
- lib/app/entities/game.rb
Constant Summary collapse
- SECRET_CODE_LENGTH =
4
- ELEMENT_VALUE_RANGE =
(1..6).freeze
- DELIMITER =
''
- EXACT_MATCH =
'+'
- NUMBER_MATCH =
'-'
Instance Attribute Summary collapse
-
#difficulty ⇒ Object
readonly
Returns the value of attribute difficulty.
-
#secret_code ⇒ Object
readonly
Returns the value of attribute secret_code.
-
#total_attempts ⇒ Object
readonly
Returns the value of attribute total_attempts.
-
#total_hints ⇒ Object
readonly
Returns the value of attribute total_hints.
-
#used_attempts ⇒ Object
readonly
Returns the value of attribute used_attempts.
-
#used_hints ⇒ Object
readonly
Returns the value of attribute used_hints.
Instance Method Summary collapse
- #hints_available? ⇒ Boolean
- #increment_used_attempts ⇒ Object
-
#initialize(difficulty) ⇒ Game
constructor
A new instance of Game.
- #loss? ⇒ Boolean
- #mark_guess(guess_code) ⇒ Object
- #use_hint ⇒ Object
- #win?(guess_code) ⇒ Boolean
Constructor Details
#initialize(difficulty) ⇒ Game
Returns a new instance of Game.
14 15 16 17 18 19 20 21 22 |
# File 'lib/app/entities/game.rb', line 14 def initialize(difficulty) @difficulty = difficulty[:level] @total_attempts = difficulty[:attempts] @used_attempts = 0 @total_hints = difficulty[:hints] @used_hints = 0 @secret_code = generate @shuffled_code = @secret_code.shuffle end |
Instance Attribute Details
#difficulty ⇒ Object (readonly)
Returns the value of attribute difficulty.
5 6 7 |
# File 'lib/app/entities/game.rb', line 5 def difficulty @difficulty end |
#secret_code ⇒ Object (readonly)
Returns the value of attribute secret_code.
5 6 7 |
# File 'lib/app/entities/game.rb', line 5 def secret_code @secret_code end |
#total_attempts ⇒ Object (readonly)
Returns the value of attribute total_attempts.
5 6 7 |
# File 'lib/app/entities/game.rb', line 5 def total_attempts @total_attempts end |
#total_hints ⇒ Object (readonly)
Returns the value of attribute total_hints.
5 6 7 |
# File 'lib/app/entities/game.rb', line 5 def total_hints @total_hints end |
#used_attempts ⇒ Object (readonly)
Returns the value of attribute used_attempts.
5 6 7 |
# File 'lib/app/entities/game.rb', line 5 def used_attempts @used_attempts end |
#used_hints ⇒ Object (readonly)
Returns the value of attribute used_hints.
5 6 7 |
# File 'lib/app/entities/game.rb', line 5 def used_hints @used_hints end |
Instance Method Details
#hints_available? ⇒ Boolean
29 30 31 |
# File 'lib/app/entities/game.rb', line 29 def hints_available? @used_hints >= @total_hints end |
#increment_used_attempts ⇒ Object
41 42 43 |
# File 'lib/app/entities/game.rb', line 41 def increment_used_attempts @used_attempts += 1 end |
#loss? ⇒ Boolean
37 38 39 |
# File 'lib/app/entities/game.rb', line 37 def loss? @used_attempts == @total_attempts end |
#mark_guess(guess_code) ⇒ Object
45 46 47 48 49 |
# File 'lib/app/entities/game.rb', line 45 def mark_guess(guess_code) @converted_input = convert_to_digit_array(guess_code) @cloned_code = @secret_code.clone convert_to_string(exact_match.compact + number_match.compact) end |
#use_hint ⇒ Object
24 25 26 27 |
# File 'lib/app/entities/game.rb', line 24 def use_hint increment_used_hints @shuffled_code.shift end |
#win?(guess_code) ⇒ Boolean
33 34 35 |
# File 'lib/app/entities/game.rb', line 33 def win?(guess_code) guess_code == convert_to_string(@secret_code) end |