Class: Codebreaker::Game
- Inherits:
-
Object
- Object
- Codebreaker::Game
- Defined in:
- lib/codebreaker/game.rb
Constant Summary collapse
- CODE_LENGTH =
4- CODE_RANGE =
(1..6).freeze
Instance Attribute Summary collapse
-
#all_attempts ⇒ Object
Returns the value of attribute all_attempts.
-
#all_hints ⇒ Object
Returns the value of attribute all_hints.
-
#attempts_left ⇒ Object
Returns the value of attribute attempts_left.
-
#code ⇒ Object
Returns the value of attribute code.
-
#difficulties ⇒ Object
Returns the value of attribute difficulties.
-
#difficulty ⇒ Object
Returns the value of attribute difficulty.
-
#hints_code ⇒ Object
readonly
Returns the value of attribute hints_code.
-
#hints_left ⇒ Object
Returns the value of attribute hints_left.
-
#input_code ⇒ Object
Returns the value of attribute input_code.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #attempts_left? ⇒ Boolean
- #game_option(name, difficulty) ⇒ Object
- #hint ⇒ Object
- #hints_left? ⇒ Boolean
-
#initialize(name, difficulty) ⇒ Game
constructor
A new instance of Game.
- #input_operation(input_code) ⇒ Object
- #save(filename = 'stats') ⇒ Object
- #win? ⇒ Boolean
Constructor Details
Instance Attribute Details
#all_attempts ⇒ Object
Returns the value of attribute all_attempts.
5 6 7 |
# File 'lib/codebreaker/game.rb', line 5 def all_attempts @all_attempts end |
#all_hints ⇒ Object
Returns the value of attribute all_hints.
5 6 7 |
# File 'lib/codebreaker/game.rb', line 5 def all_hints @all_hints end |
#attempts_left ⇒ Object
Returns the value of attribute attempts_left.
5 6 7 |
# File 'lib/codebreaker/game.rb', line 5 def attempts_left @attempts_left end |
#code ⇒ Object
Returns the value of attribute code.
5 6 7 |
# File 'lib/codebreaker/game.rb', line 5 def code @code end |
#difficulties ⇒ Object
Returns the value of attribute difficulties.
5 6 7 |
# File 'lib/codebreaker/game.rb', line 5 def difficulties @difficulties end |
#difficulty ⇒ Object
Returns the value of attribute difficulty.
5 6 7 |
# File 'lib/codebreaker/game.rb', line 5 def difficulty @difficulty end |
#hints_code ⇒ Object (readonly)
Returns the value of attribute hints_code.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def hints_code @hints_code end |
#hints_left ⇒ Object
Returns the value of attribute hints_left.
5 6 7 |
# File 'lib/codebreaker/game.rb', line 5 def hints_left @hints_left end |
#input_code ⇒ Object
Returns the value of attribute input_code.
5 6 7 |
# File 'lib/codebreaker/game.rb', line 5 def input_code @input_code end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/codebreaker/game.rb', line 5 def name @name end |
Instance Method Details
#attempts_left? ⇒ Boolean
47 48 49 |
# File 'lib/codebreaker/game.rb', line 47 def attempts_left? attempts_left.positive? end |
#game_option(name, difficulty) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/codebreaker/game.rb', line 19 def game_option(name, difficulty) @name = name @difficulty = difficulty.to_sym @all_attempts = difficulty_option[:attempts] @all_hints = difficulty_option[:hints] @attempts_left = difficulty_option[:attempts] @hints_left = difficulty_option[:hints] end |
#hint ⇒ Object
28 29 30 31 32 33 |
# File 'lib/codebreaker/game.rb', line 28 def hint return unless hints_left? @hints_left -= 1 generate_hint end |
#hints_left? ⇒ Boolean
35 36 37 |
# File 'lib/codebreaker/game.rb', line 35 def hints_left? hints_left.positive? end |
#input_operation(input_code) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/codebreaker/game.rb', line 39 def input_operation(input_code) @input_code = input_code return unless attempts_left? @attempts_left -= 1 check_input(@code, @input_code) end |
#save(filename = 'stats') ⇒ Object
55 56 57 |
# File 'lib/codebreaker/game.rb', line 55 def save(filename = 'stats') Codebreaker::Loader.save(to_h, filename) end |
#win? ⇒ Boolean
51 52 53 |
# File 'lib/codebreaker/game.rb', line 51 def win? input_code == code end |