Class: Codebreaker::Game
- Inherits:
-
Object
- Object
- Codebreaker::Game
- Defined in:
- lib/codebreaker_gapdn/game.rb
Constant Summary collapse
- GUESS_PRESENCE =
'-'- GUESS_PLACE =
'+'
Instance Attribute Summary collapse
-
#attempts_counter ⇒ Object
readonly
Returns the value of attribute attempts_counter.
-
#current_difficulty ⇒ Object
readonly
Returns the value of attribute current_difficulty.
-
#hinted_numbers ⇒ Object
readonly
Returns the value of attribute hinted_numbers.
-
#secret_code ⇒ Object
readonly
Returns the value of attribute secret_code.
Instance Method Summary collapse
- #check_number_match(guess) ⇒ Object
-
#initialize(difficulty) ⇒ Game
constructor
A new instance of Game.
- #lose? ⇒ Boolean
- #take_hint ⇒ Object
- #win?(string) ⇒ Boolean
Constructor Details
#initialize(difficulty) ⇒ Game
Returns a new instance of Game.
10 11 12 13 14 15 |
# File 'lib/codebreaker_gapdn/game.rb', line 10 def initialize(difficulty) @secret_code = create_secret_code @attempts_counter = 0 @current_difficulty = difficulty.level @hinted_numbers = @secret_code.sample(@current_difficulty[:hints]) end |
Instance Attribute Details
#attempts_counter ⇒ Object (readonly)
Returns the value of attribute attempts_counter.
8 9 10 |
# File 'lib/codebreaker_gapdn/game.rb', line 8 def attempts_counter @attempts_counter end |
#current_difficulty ⇒ Object (readonly)
Returns the value of attribute current_difficulty.
8 9 10 |
# File 'lib/codebreaker_gapdn/game.rb', line 8 def current_difficulty @current_difficulty end |
#hinted_numbers ⇒ Object (readonly)
Returns the value of attribute hinted_numbers.
8 9 10 |
# File 'lib/codebreaker_gapdn/game.rb', line 8 def hinted_numbers @hinted_numbers end |
#secret_code ⇒ Object (readonly)
Returns the value of attribute secret_code.
8 9 10 |
# File 'lib/codebreaker_gapdn/game.rb', line 8 def secret_code @secret_code end |
Instance Method Details
#check_number_match(guess) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/codebreaker_gapdn/game.rb', line 17 def check_number_match(guess) @guess = guess @attempts_counter += 1 return win_string if @guess == @secret_code number_match(exact_match) end |
#lose? ⇒ Boolean
33 34 35 |
# File 'lib/codebreaker_gapdn/game.rb', line 33 def lose? @attempts_counter >= @current_difficulty[:attempts] end |
#take_hint ⇒ Object
25 26 27 |
# File 'lib/codebreaker_gapdn/game.rb', line 25 def take_hint @hinted_numbers.pop end |
#win?(string) ⇒ Boolean
29 30 31 |
# File 'lib/codebreaker_gapdn/game.rb', line 29 def win?(string) string == win_string end |