Class: Codebreaker::Game
- Inherits:
-
Object
- Object
- Codebreaker::Game
- Includes:
- Validation
- Defined in:
- lib/entities/game.rb
Constant Summary collapse
- INCREMENT =
1- AMOUNT_DIGITS =
4- POSITIVE_DIGIT =
'+'.freeze
- NEGATIVE_DIGIT =
'-'.freeze
- DIFFICULTIES =
{ easy: { attempts: 15, hints: 2, difficulty: 'easy' }, hard: { attempts: 10, hints: 2, difficulty: 'hard' }, expert: { attempts: 5, hints: 1, difficulty: 'expert' } }.freeze
- RANGE_OF_DIGITS =
1..6.freeze
- GUESS_CODE =
{ hint: 'hint', leave: 'exit' }.freeze
Instance Attribute Summary collapse
-
#attempts_left ⇒ Object
readonly
Returns the value of attribute attempts_left.
-
#attempts_total ⇒ Object
readonly
Returns the value of attribute attempts_total.
-
#attempts_used ⇒ Object
readonly
Returns the value of attribute attempts_used.
-
#date ⇒ Object
readonly
Returns the value of attribute date.
-
#difficulty ⇒ Object
readonly
Returns the value of attribute difficulty.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#got_hints ⇒ Object
readonly
Returns the value of attribute got_hints.
-
#have_hints ⇒ Object
readonly
Returns the value of attribute have_hints.
-
#hints_total ⇒ Object
readonly
Returns the value of attribute hints_total.
-
#hints_used ⇒ Object
readonly
Returns the value of attribute hints_used.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#secret_code ⇒ Object
readonly
Returns the value of attribute secret_code.
-
#winner ⇒ Object
readonly
Returns the value of attribute winner.
Instance Method Summary collapse
- #attempt(input) ⇒ Object
- #game_options(user_difficulty:, player:) ⇒ Object
- #miss_input ⇒ Object
- #remove_instance_helpers ⇒ Object
- #valid_difficulties?(input) ⇒ Boolean
Methods included from Validation
#valid_digits?, #valid_name?, #validate_in_range?, #validate_length, #validate_match, #validate_presence?
Instance Attribute Details
#attempts_left ⇒ Object (readonly)
Returns the value of attribute attempts_left.
17 18 19 |
# File 'lib/entities/game.rb', line 17 def attempts_left @attempts_left end |
#attempts_total ⇒ Object (readonly)
Returns the value of attribute attempts_total.
17 18 19 |
# File 'lib/entities/game.rb', line 17 def attempts_total @attempts_total end |
#attempts_used ⇒ Object (readonly)
Returns the value of attribute attempts_used.
17 18 19 |
# File 'lib/entities/game.rb', line 17 def attempts_used @attempts_used end |
#date ⇒ Object (readonly)
Returns the value of attribute date.
17 18 19 |
# File 'lib/entities/game.rb', line 17 def date @date end |
#difficulty ⇒ Object (readonly)
Returns the value of attribute difficulty.
17 18 19 |
# File 'lib/entities/game.rb', line 17 def difficulty @difficulty end |
#errors ⇒ Object
Returns the value of attribute errors.
18 19 20 |
# File 'lib/entities/game.rb', line 18 def errors @errors end |
#got_hints ⇒ Object (readonly)
Returns the value of attribute got_hints.
17 18 19 |
# File 'lib/entities/game.rb', line 17 def got_hints @got_hints end |
#have_hints ⇒ Object (readonly)
Returns the value of attribute have_hints.
17 18 19 |
# File 'lib/entities/game.rb', line 17 def have_hints @have_hints end |
#hints_total ⇒ Object (readonly)
Returns the value of attribute hints_total.
17 18 19 |
# File 'lib/entities/game.rb', line 17 def hints_total @hints_total end |
#hints_used ⇒ Object (readonly)
Returns the value of attribute hints_used.
17 18 19 |
# File 'lib/entities/game.rb', line 17 def hints_used @hints_used end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
17 18 19 |
# File 'lib/entities/game.rb', line 17 def name @name end |
#secret_code ⇒ Object (readonly)
Returns the value of attribute secret_code.
17 18 19 |
# File 'lib/entities/game.rb', line 17 def secret_code @secret_code end |
#winner ⇒ Object (readonly)
Returns the value of attribute winner.
17 18 19 |
# File 'lib/entities/game.rb', line 17 def winner @winner end |
Instance Method Details
#attempt(input) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/entities/game.rb', line 28 def attempt(input) @errors = [] return use_hint if hint?(input) converted = convert_to_array(input) return guessing(converted) if valid_input?(input, AMOUNT_DIGITS) miss_input && return end |
#game_options(user_difficulty:, player:) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/entities/game.rb', line 20 def (user_difficulty:, player:) @got_hints = '' @hints_used = 0 @attempts_used = 0 @name = player.name assign_difficulty(DIFFICULTIES[user_difficulty.downcase.to_sym]) end |
#miss_input ⇒ Object
37 38 39 |
# File 'lib/entities/game.rb', line 37 def miss_input @errors << I18n.t(:when_incorrect_guess) && return end |
#remove_instance_helpers ⇒ Object
45 46 47 48 49 50 |
# File 'lib/entities/game.rb', line 45 def remove_instance_helpers remove_instance_variable(:@winner) if @winner remove_instance_variable(:@errors) if @errors remove_instance_variable(:@hints_array) if @hints_array remove_instance_variable(:@have_hints) if @have_hints end |
#valid_difficulties?(input) ⇒ Boolean
41 42 43 |
# File 'lib/entities/game.rb', line 41 def valid_difficulties?(input) DIFFICULTIES.key?(input.to_sym) end |