Class: Codebreaker::Game
- Inherits:
-
Object
- Object
- Codebreaker::Game
- Defined in:
- lib/codebreaker/game.rb
Constant Summary collapse
- ATTEMPTS =
10- HINTS =
4
Instance Attribute Summary collapse
-
#used_attempts ⇒ Object
readonly
Returns the value of attribute used_attempts.
-
#used_hints ⇒ Object
readonly
Returns the value of attribute used_hints.
Instance Method Summary collapse
- #available_attempts ⇒ Object
- #available_hints ⇒ Object
- #hint ⇒ Object
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #make_guess(user_code) ⇒ Object
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
8 9 10 11 12 13 |
# File 'lib/codebreaker/game.rb', line 8 def initialize @secret_code = generate @shuffled_secret_code = @secret_code.shuffle @used_attempts = 0 @used_hints = 0 end |
Instance Attribute Details
#used_attempts ⇒ Object (readonly)
Returns the value of attribute used_attempts.
6 7 8 |
# File 'lib/codebreaker/game.rb', line 6 def used_attempts @used_attempts end |
#used_hints ⇒ Object (readonly)
Returns the value of attribute used_hints.
6 7 8 |
# File 'lib/codebreaker/game.rb', line 6 def used_hints @used_hints end |
Instance Method Details
#available_attempts ⇒ Object
28 29 30 |
# File 'lib/codebreaker/game.rb', line 28 def available_attempts ATTEMPTS - @game.used_attempts end |
#available_hints ⇒ Object
32 33 34 |
# File 'lib/codebreaker/game.rb', line 32 def available_hints HINTS - @game.used_hints end |
#hint ⇒ Object
15 16 17 18 |
# File 'lib/codebreaker/game.rb', line 15 def hint @used_hints += 1 @shuffled_secret_code.pop end |
#make_guess(user_code) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/codebreaker/game.rb', line 20 def make_guess(user_code) return unless code_valid?(user_code) @used_attempts += 1 @user_code = user_code.chars.map(&:to_i) return '++++' if @user_code == @secret_code exact_matches + number_matches end |