Class: Codebreaker::Game
- Inherits:
-
Object
- Object
- Codebreaker::Game
- Defined in:
- lib/codebreaker/game.rb
Constant Summary collapse
- DIFFICULTIES =
{ easy: { attempts: 15, hints: 2, hardness: 1 }, medium: { attempts: 10, hints: 1, hardness: 2 }, hell: { attempts: 5, hints: 1, hardness: 3 } }.freeze
Instance Attribute Summary collapse
-
#attempts_left ⇒ Object
readonly
Returns the value of attribute attempts_left.
-
#difficulty_type ⇒ Object
readonly
Returns the value of attribute difficulty_type.
-
#player ⇒ Object
readonly
Returns the value of attribute player.
Instance Method Summary collapse
- #attempts_total ⇒ Object
- #guess_secret_code(guess) ⇒ Object
- #hints_left ⇒ Object
- #hints_total ⇒ Object
-
#initialize(player, difficulty_type) ⇒ Game
constructor
A new instance of Game.
- #lose? ⇒ Boolean
- #over? ⇒ Boolean
- #save_result ⇒ Object
- #secret_code ⇒ Object
- #take_hint ⇒ Object
- #win? ⇒ Boolean
Constructor Details
#initialize(player, difficulty_type) ⇒ Game
Returns a new instance of Game.
11 12 13 14 15 16 |
# File 'lib/codebreaker/game.rb', line 11 def initialize(player, difficulty_type) @player = player @difficulty_type = difficulty_type @attempts_left = difficulty[:attempts] @is_win = false end |
Instance Attribute Details
#attempts_left ⇒ Object (readonly)
Returns the value of attribute attempts_left.
3 4 5 |
# File 'lib/codebreaker/game.rb', line 3 def attempts_left @attempts_left end |
#difficulty_type ⇒ Object (readonly)
Returns the value of attribute difficulty_type.
3 4 5 |
# File 'lib/codebreaker/game.rb', line 3 def difficulty_type @difficulty_type end |
#player ⇒ Object (readonly)
Returns the value of attribute player.
3 4 5 |
# File 'lib/codebreaker/game.rb', line 3 def player @player end |
Instance Method Details
#attempts_total ⇒ Object
49 50 51 |
# File 'lib/codebreaker/game.rb', line 49 def attempts_total difficulty[:attempts] end |
#guess_secret_code(guess) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/codebreaker/game.rb', line 29 def guess_secret_code(guess) ensure_game_is_not_over @attempts_left -= 1 matching = SecretCodeMatcher.new(secret_code, guess).call check_win(guess) matching end |
#hints_left ⇒ Object
57 58 59 |
# File 'lib/codebreaker/game.rb', line 57 def hints_left hints.count end |
#hints_total ⇒ Object
53 54 55 |
# File 'lib/codebreaker/game.rb', line 53 def hints_total difficulty[:hints] end |
#lose? ⇒ Boolean
37 38 39 |
# File 'lib/codebreaker/game.rb', line 37 def lose? attempts_left.zero? && !win? end |
#over? ⇒ Boolean
45 46 47 |
# File 'lib/codebreaker/game.rb', line 45 def over? win? || lose? end |
#save_result ⇒ Object
61 62 63 64 65 |
# File 'lib/codebreaker/game.rb', line 61 def save_result raise Errors::GameSaveError unless win? CreateStatService.new(self).call end |
#secret_code ⇒ Object
18 19 20 |
# File 'lib/codebreaker/game.rb', line 18 def secret_code @secret_code ||= SecretCodeGenerator.new.call end |
#take_hint ⇒ Object
22 23 24 25 26 27 |
# File 'lib/codebreaker/game.rb', line 22 def take_hint ensure_game_is_not_over raise Errors::NoHintsError if hints.empty? hints.pop end |
#win? ⇒ Boolean
41 42 43 |
# File 'lib/codebreaker/game.rb', line 41 def win? @is_win end |