Class: CodebreakerVk::Game
- Inherits:
-
Object
- Object
- CodebreakerVk::Game
- Includes:
- Output
- Defined in:
- lib/codebreaker_vk/game.rb
Constant Summary collapse
- SECRET_CODE_LENGTH =
4
- RANGE_START =
1
- RANGE_END =
6
- NOT_YET =
'-'
- GOT_IT =
'+'
- DIFFICULTY_LEVEL =
{ easy: { attempts: 15, hints: 3 }, medium: { attempts: 10, hints: 2 }, hell: { attempts: 5, hints: 1 } }.freeze
Instance Attribute Summary collapse
-
#attempts ⇒ Object
Returns the value of attribute attempts.
-
#attempts_total ⇒ Object
Returns the value of attribute attempts_total.
-
#difficulty ⇒ Object
Returns the value of attribute difficulty.
-
#hints ⇒ Object
Returns the value of attribute hints.
-
#hints_total ⇒ Object
Returns the value of attribute hints_total.
-
#name ⇒ Object
Returns the value of attribute name.
-
#secret ⇒ Object
Returns the value of attribute secret.
Instance Method Summary collapse
- #check(number) ⇒ Object
-
#initialize(name:, difficulty:) ⇒ Game
constructor
A new instance of Game.
- #make_number(numbers = RANGE_END) ⇒ Object
- #use_hint ⇒ Object
- #win? ⇒ Boolean
Methods included from Output
#rules, #start_info, #summary_info
Constructor Details
#initialize(name:, difficulty:) ⇒ Game
Returns a new instance of Game.
19 20 21 22 23 24 25 26 |
# File 'lib/codebreaker_vk/game.rb', line 19 def initialize(name:, difficulty:) @name = name @difficulty = difficulty @attempts = DIFFICULTY_LEVEL[difficulty][:attempts] @hints = DIFFICULTY_LEVEL[difficulty][:hints] @secret = make_number @unused_hints = @secret.chars end |
Instance Attribute Details
#attempts ⇒ Object
Returns the value of attribute attempts.
17 18 19 |
# File 'lib/codebreaker_vk/game.rb', line 17 def attempts @attempts end |
#attempts_total ⇒ Object
Returns the value of attribute attempts_total.
17 18 19 |
# File 'lib/codebreaker_vk/game.rb', line 17 def attempts_total @attempts_total end |
#difficulty ⇒ Object
Returns the value of attribute difficulty.
17 18 19 |
# File 'lib/codebreaker_vk/game.rb', line 17 def difficulty @difficulty end |
#hints ⇒ Object
Returns the value of attribute hints.
17 18 19 |
# File 'lib/codebreaker_vk/game.rb', line 17 def hints @hints end |
#hints_total ⇒ Object
Returns the value of attribute hints_total.
17 18 19 |
# File 'lib/codebreaker_vk/game.rb', line 17 def hints_total @hints_total end |
#name ⇒ Object
Returns the value of attribute name.
17 18 19 |
# File 'lib/codebreaker_vk/game.rb', line 17 def name @name end |
#secret ⇒ Object
Returns the value of attribute secret.
17 18 19 |
# File 'lib/codebreaker_vk/game.rb', line 17 def secret @secret end |
Instance Method Details
#check(number) ⇒ Object
32 33 34 35 |
# File 'lib/codebreaker_vk/game.rb', line 32 def check(number) @attempts -= 1 @last_result = check_numbers(@secret.chars, number.chars) end |
#make_number(numbers = RANGE_END) ⇒ Object
28 29 30 |
# File 'lib/codebreaker_vk/game.rb', line 28 def make_number(numbers = RANGE_END) (1..SECRET_CODE_LENGTH).map { rand(RANGE_START..numbers) }.join end |
#use_hint ⇒ Object
41 42 43 44 45 46 |
# File 'lib/codebreaker_vk/game.rb', line 41 def use_hint return I18n.t(:no_hints) unless @hints.positive? @hints -= 1 hint(@unused_hints) end |
#win? ⇒ Boolean
37 38 39 |
# File 'lib/codebreaker_vk/game.rb', line 37 def win? @last_result == GOT_IT * SECRET_CODE_LENGTH end |