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
-
#made_guesses ⇒ Object
readonly
Returns the value of attribute made_guesses.
-
#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 14 |
# File 'lib/codebreaker/game.rb', line 8 def initialize @secret_code = generate @shuffled_secret_code = @secret_code.shuffle @used_attempts = 0 @used_hints = 0 @made_guesses = {} end |
Instance Attribute Details
#made_guesses ⇒ Object (readonly)
Returns the value of attribute made_guesses.
6 7 8 |
# File 'lib/codebreaker/game.rb', line 6 def made_guesses @made_guesses end |
#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
31 32 33 |
# File 'lib/codebreaker/game.rb', line 31 def available_attempts ATTEMPTS - @used_attempts end |
#available_hints ⇒ Object
35 36 37 |
# File 'lib/codebreaker/game.rb', line 35 def available_hints HINTS - @used_hints end |
#hint ⇒ Object
16 17 18 19 |
# File 'lib/codebreaker/game.rb', line 16 def hint @used_hints += 1 @shuffled_secret_code.pop end |
#make_guess(user_code) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/codebreaker/game.rb', line 21 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 result = exact_matches + number_matches @made_guesses[user_code] = result result end |