Class: Codebreaker::Entities::Game
- Inherits:
-
Object
- Object
- Codebreaker::Entities::Game
- Defined in:
- lib/codebreaker/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.
23 24 25 |
# File 'lib/codebreaker/entities/game.rb', line 23 def attempts @attempts end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
23 24 25 |
# File 'lib/codebreaker/entities/game.rb', line 23 def code @code end |
#hints ⇒ Object (readonly)
Returns the value of attribute hints.
23 24 25 |
# File 'lib/codebreaker/entities/game.rb', line 23 def hints @hints end |
Instance Method Details
#decrease_attempts! ⇒ Object
44 45 46 |
# File 'lib/codebreaker/entities/game.rb', line 44 def decrease_attempts! @attempts -= 1 end |
#generate(difficulty) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/codebreaker/entities/game.rb', line 29 def generate(difficulty) @difficulty = difficulty @code = generate_secret_code @hints = @code.sample(difficulty[:hints]) @attempts = difficulty[:attempts] end |
#hints_spent? ⇒ Boolean
59 60 61 |
# File 'lib/codebreaker/entities/game.rb', line 59 def hints_spent? hints.empty? end |
#start_process(command) ⇒ Object
36 37 38 |
# File 'lib/codebreaker/entities/game.rb', line 36 def start_process(command) @process.secret_code_proc(code.join, command) end |
#take_a_hint! ⇒ Object
63 64 65 |
# File 'lib/codebreaker/entities/game.rb', line 63 def take_a_hint! hints.pop end |
#to_h(name) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/codebreaker/entities/game.rb', line 48 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
40 41 42 |
# File 'lib/codebreaker/entities/game.rb', line 40 def win?(guess) code.join == guess end |