Class: Codebreaker::Game
- Inherits:
-
Object
- Object
- Codebreaker::Game
- Includes:
- Storage, Validation
- Defined in:
- lib/codebreaker/game.rb
Constant Summary collapse
- DIFFICULTY =
{ hell: { attempts: 5, hint: 1, mode: 'hell' }, medium: { attempts: 10, hint: 1, mode: 'medium' }, easy: { attempts: 15, hint: 2, mode: 'easy' } }.freeze
Constants included from Storage
Storage::FILE_NAME, Storage::PATH
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
Returns the value of attribute attempts.
-
#difficulty ⇒ Object
readonly
Returns the value of attribute difficulty.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#hint_keeper ⇒ Object
readonly
Returns the value of attribute hint_keeper.
-
#hints ⇒ Object
readonly
Returns the value of attribute hints.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#play ⇒ Object
readonly
Returns the value of attribute play.
-
#player ⇒ Object
readonly
Returns the value of attribute player.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#secret_code ⇒ Object
readonly
Returns the value of attribute secret_code.
-
#storage ⇒ Object
readonly
Returns the value of attribute storage.
Instance Method Summary collapse
- #data_to_save(name) ⇒ Object
- #guess(guess) ⇒ Object
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #lost? ⇒ Boolean
- #set(user_input) ⇒ Object
- #take_hint! ⇒ Object
- #to_yaml(name) ⇒ Object
- #use_attempt! ⇒ Object
- #won?(guess) ⇒ Boolean
Methods included from Storage
#load_database, #save, #sort_player
Methods included from Validation
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
16 17 18 19 |
# File 'lib/codebreaker/game.rb', line 16 def initialize @secret_code = 4.times.map { Random.rand(1..6) } @errors = [] end |
Instance Attribute Details
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def attempts @attempts end |
#difficulty ⇒ Object (readonly)
Returns the value of attribute difficulty.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def difficulty @difficulty end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def errors @errors end |
#hint_keeper ⇒ Object (readonly)
Returns the value of attribute hint_keeper.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def hint_keeper @hint_keeper end |
#hints ⇒ Object (readonly)
Returns the value of attribute hints.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def hints @hints end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def name @name end |
#play ⇒ Object (readonly)
Returns the value of attribute play.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def play @play end |
#player ⇒ Object (readonly)
Returns the value of attribute player.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def player @player end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def result @result end |
#secret_code ⇒ Object (readonly)
Returns the value of attribute secret_code.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def secret_code @secret_code end |
#storage ⇒ Object (readonly)
Returns the value of attribute storage.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def storage @storage end |
Instance Method Details
#data_to_save(name) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/codebreaker/game.rb', line 46 def data_to_save(name) { name: name, difficulty: difficulty[:mode], attempts_total: difficulty[:attempts], attempts_used: difficulty[:attempts] - attempts, hints_total: difficulty[:hint], hints_used: difficulty[:hint] - hint_keeper.size } end |
#guess(guess) ⇒ Object
61 62 63 |
# File 'lib/codebreaker/game.rb', line 61 def guess(guess) '+' * exact_match_count(guess) + '-' * number_match_count(guess) end |
#lost? ⇒ Boolean
38 39 40 |
# File 'lib/codebreaker/game.rb', line 38 def lost? attempts.zero? end |
#set(user_input) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/codebreaker/game.rb', line 21 def set(user_input) return unless DIFFICULTY.key?(user_input.to_sym) @difficulty = DIFFICULTY.dig(user_input.to_sym) @hints = @difficulty.dig(:hint) @attempts = @difficulty.dig(:attempts) @hint_keeper = @secret_code.sample(hints) end |
#take_hint! ⇒ Object
30 31 32 |
# File 'lib/codebreaker/game.rb', line 30 def take_hint! hint_keeper.pop end |
#to_yaml(name) ⇒ Object
42 43 44 |
# File 'lib/codebreaker/game.rb', line 42 def to_yaml(name) data_to_save(name) end |
#use_attempt! ⇒ Object
57 58 59 |
# File 'lib/codebreaker/game.rb', line 57 def use_attempt! @attempts -= 1 end |
#won?(guess) ⇒ Boolean
34 35 36 |
# File 'lib/codebreaker/game.rb', line 34 def won?(guess) secret_code.join == guess end |