Class: Codebreaker::Game
- Inherits:
-
Object
- Object
- Codebreaker::Game
- Includes:
- Constants
- Defined in:
- lib/codebreaker/game.rb
Constant Summary
Constants included from Constants
Constants::DIFFICULTS, Constants::MINUS, Constants::PLUS, Constants::WIN_RESULT
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
Returns the value of attribute attempts.
-
#difficult ⇒ Object
readonly
Returns the value of attribute difficult.
-
#hints ⇒ Object
readonly
Returns the value of attribute hints.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
Instance Method Summary collapse
- #check_number(guess, secret_code = @secret) ⇒ Object
- #decrement_attempts ⇒ Object
-
#initialize(name, difficult) ⇒ Game
constructor
A new instance of Game.
- #use_hint ⇒ Object
- #used_attempts ⇒ Object
- #used_hints ⇒ Object
- #win?(matches) ⇒ Boolean
Constructor Details
#initialize(name, difficult) ⇒ Game
Returns a new instance of Game.
9 10 11 12 13 14 15 16 17 |
# File 'lib/codebreaker/game.rb', line 9 def initialize(name, difficult) @name = name @difficult = difficult @attempts = get_attempts(difficult) @hints = get_hints(difficult) generate_secret @secret_chars = @secret.chars.shuffle 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 |
#difficult ⇒ Object (readonly)
Returns the value of attribute difficult.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def difficult @difficult 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 |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def secret @secret end |
Instance Method Details
#check_number(guess, secret_code = @secret) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/codebreaker/game.rb', line 44 def check_number(guess, secret_code = @secret) secret = secret_code.chars input = guess.chars grouped = group_arrays(secret, input) plus = grouped[true].size return result(plus) if plus == 4 minus = get_minuses_count(grouped) result(plus, minus) end |
#decrement_attempts ⇒ Object
19 20 21 22 23 |
# File 'lib/codebreaker/game.rb', line 19 def decrement_attempts return false if @attempts.zero? @attempts -= 1 end |
#use_hint ⇒ Object
25 26 27 28 29 30 |
# File 'lib/codebreaker/game.rb', line 25 def use_hint return false if @hints.zero? @hints -= 1 @secret_chars.pop end |
#used_attempts ⇒ Object
36 37 38 |
# File 'lib/codebreaker/game.rb', line 36 def used_attempts get_attempts(@difficult) - @attempts end |
#used_hints ⇒ Object
40 41 42 |
# File 'lib/codebreaker/game.rb', line 40 def used_hints get_hints(@difficult) - @hints end |
#win?(matches) ⇒ Boolean
32 33 34 |
# File 'lib/codebreaker/game.rb', line 32 def win?(matches) matches == WIN_RESULT end |