Class: Game
- Inherits:
-
Object
- Object
- Game
- Defined in:
- lib/app/entities/game.rb
Constant Summary collapse
- DIGITS_COUNT =
4
- DIFFICULTIES =
{ easy: { attempts: 15, hints: 2 }, medium: { attempts: 10, hints: 1 }, hell: { attempts: 5, hints: 1 } }.freeze
- RANGE =
(1..6).freeze
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
Returns the value of attribute attempts.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#hints ⇒ Object
readonly
Returns the value of attribute hints.
Instance Method Summary collapse
- #decrease_attempts! ⇒ Object
- #generate(difficulty) ⇒ Object
- #hints_spent? ⇒ Boolean
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #start_process(command) ⇒ Object
- #take_a_hint! ⇒ Object
- #to_h(name) ⇒ Object
- #win?(guess) ⇒ Boolean
Constructor Details
Instance Attribute Details
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
21 22 23 |
# File 'lib/app/entities/game.rb', line 21 def attempts @attempts end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
21 22 23 |
# File 'lib/app/entities/game.rb', line 21 def code @code end |
#hints ⇒ Object (readonly)
Returns the value of attribute hints.
21 22 23 |
# File 'lib/app/entities/game.rb', line 21 def hints @hints end |
Instance Method Details
#decrease_attempts! ⇒ Object
42 43 44 |
# File 'lib/app/entities/game.rb', line 42 def decrease_attempts! @attempts -= 1 end |
#generate(difficulty) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/app/entities/game.rb', line 27 def generate(difficulty) @difficulty = difficulty @code = generate_secret_code @hints = @code.sample(difficulty[:hints]) @attempts = difficulty[:attempts] end |
#hints_spent? ⇒ Boolean
57 58 59 |
# File 'lib/app/entities/game.rb', line 57 def hints_spent? hints.empty? end |
#start_process(command) ⇒ Object
34 35 36 |
# File 'lib/app/entities/game.rb', line 34 def start_process(command) @process.secret_code_proc(code.join, command) end |
#take_a_hint! ⇒ Object
61 62 63 |
# File 'lib/app/entities/game.rb', line 61 def take_a_hint! hints.pop end |
#to_h(name) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/app/entities/game.rb', line 46 def to_h(name) { name: name, difficulty: DIFFICULTIES.key(@difficulty), all_attempts: @difficulty[:attempts], all_hints: @difficulty[:hints], attempts_used: @difficulty[:attempts] - @attempts, hints_used: @difficulty[:hints] - @hints.length } end |
#win?(guess) ⇒ Boolean
38 39 40 |
# File 'lib/app/entities/game.rb', line 38 def win?(guess) code.join == guess end |