Class: CodebreakerRuban::Game
- Inherits:
-
Object
- Object
- CodebreakerRuban::Game
- Includes:
- Validation
- Defined in:
- lib/app/entities/game.rb
Constant Summary collapse
- LENGTH_GUESS =
4
- RANGE_SECRET_CODE =
(1..6).freeze
- DIFFICULTY =
{ easy: { hints_total: 2, attempts_total: 15, difficulty: 'easy' }, medium: { hints_total: 1, attempts_total: 10, difficulty: 'medium' }, hell: { hints_total: 1, attempts_total: 5, difficulty: 'hell' } }
Instance Attribute Summary collapse
-
#attempts_total ⇒ Object
readonly
Returns the value of attribute attempts_total.
-
#attempts_used ⇒ Object
Returns the value of attribute attempts_used.
-
#datetime ⇒ Object
readonly
Returns the value of attribute datetime.
-
#difficulty ⇒ Object
readonly
Returns the value of attribute difficulty.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#guess_code ⇒ Object
Returns the value of attribute guess_code.
-
#hints_showed ⇒ Object
readonly
Returns the value of attribute hints_showed.
-
#hints_total ⇒ Object
readonly
Returns the value of attribute hints_total.
-
#hints_used ⇒ Object
Returns the value of attribute hints_used.
-
#secret_code ⇒ Object
readonly
Returns the value of attribute secret_code.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #check_attemps ⇒ Object
- #check_user_input(input) ⇒ Object
- #hint_use ⇒ Object
-
#initialize(difficulty, user) ⇒ Game
constructor
A new instance of Game.
- #to_h ⇒ Object
- #validate_guess_input(input) ⇒ Object
Methods included from Validation
#check_number, #size, #validate_range_lenght
Constructor Details
#initialize(difficulty, user) ⇒ Game
Returns a new instance of Game.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/app/entities/game.rb', line 17 def initialize(difficulty, user) @user = user @difficulty = difficulty[:difficulty] @secret_code = generator_secret_code @attempts_total = difficulty[:attempts_total] @hints_total = difficulty[:hints_total] @hint = secret_code.clone.shuffle @hints_used = 0 @attempts_used = 0 @hints_showed = [] @datetime = Time.now @guess_code = nil @errors = [] end |
Instance Attribute Details
#attempts_total ⇒ Object (readonly)
Returns the value of attribute attempts_total.
6 7 8 |
# File 'lib/app/entities/game.rb', line 6 def attempts_total @attempts_total end |
#attempts_used ⇒ Object
Returns the value of attribute attempts_used.
7 8 9 |
# File 'lib/app/entities/game.rb', line 7 def attempts_used @attempts_used end |
#datetime ⇒ Object (readonly)
Returns the value of attribute datetime.
6 7 8 |
# File 'lib/app/entities/game.rb', line 6 def datetime @datetime end |
#difficulty ⇒ Object (readonly)
Returns the value of attribute difficulty.
6 7 8 |
# File 'lib/app/entities/game.rb', line 6 def difficulty @difficulty end |
#errors ⇒ Object
Returns the value of attribute errors.
7 8 9 |
# File 'lib/app/entities/game.rb', line 7 def errors @errors end |
#guess_code ⇒ Object
Returns the value of attribute guess_code.
7 8 9 |
# File 'lib/app/entities/game.rb', line 7 def guess_code @guess_code end |
#hints_showed ⇒ Object (readonly)
Returns the value of attribute hints_showed.
6 7 8 |
# File 'lib/app/entities/game.rb', line 6 def hints_showed @hints_showed end |
#hints_total ⇒ Object (readonly)
Returns the value of attribute hints_total.
6 7 8 |
# File 'lib/app/entities/game.rb', line 6 def hints_total @hints_total end |
#hints_used ⇒ Object
Returns the value of attribute hints_used.
7 8 9 |
# File 'lib/app/entities/game.rb', line 7 def hints_used @hints_used end |
#secret_code ⇒ Object (readonly)
Returns the value of attribute secret_code.
6 7 8 |
# File 'lib/app/entities/game.rb', line 6 def secret_code @secret_code end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
6 7 8 |
# File 'lib/app/entities/game.rb', line 6 def user @user end |
Instance Method Details
#check_attemps ⇒ Object
38 39 40 41 |
# File 'lib/app/entities/game.rb', line 38 def check_attemps @attempts_used += 1 @attempts_used != @attempts_total end |
#check_user_input(input) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/app/entities/game.rb', line 43 def check_user_input(input) input = transform_in_array(input) result = [] secret_arr = secret_code.clone user_arr = input.clone check_plus(user_arr, secret_arr, result).compact! check_minus(user_arr, secret_arr, result) [result].join('') end |
#hint_use ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/app/entities/game.rb', line 53 def hint_use if @hints_used < @hints_total @hints_used += 1 @hints_showed << @hint.pop else clear_errors errors << Message.error_guess_code end end |
#to_h ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/app/entities/game.rb', line 63 def to_h { name: @user.name.capitalize, difficulty: @difficulty, attempts_used: @attempts_used, attempts_total: @attempts_total, hints_total: @hints_total, hints_used: @hints_used } end |
#validate_guess_input(input) ⇒ Object
32 33 34 35 36 |
# File 'lib/app/entities/game.rb', line 32 def validate_guess_input(input) clear_errors return errors << Message.error_guess_code unless validate_range_lenght(input, LENGTH_GUESS) return errors << Message.error_guess_code unless check_number(transform_in_array(input), RANGE_SECRET_CODE) end |