Class: Game
- Inherits:
-
Object
- Object
- Game
- Defined in:
- lib/codebreaker/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.
-
#win ⇒ Object
readonly
Returns the value of attribute win.
Instance Method Summary collapse
- #check_number_match(guess) ⇒ Object
- #difficulty(difficulty) ⇒ 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.
9 10 11 12 13 14 |
# File 'lib/codebreaker/game.rb', line 9 def initialize(difficulty) @secret_code = create_secret_code @guess = [] @attempts_counter = 0 difficulty(difficulty) end |
Instance Attribute Details
#attempts_counter ⇒ Object (readonly)
Returns the value of attribute attempts_counter.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def attempts_counter @attempts_counter end |
#current_difficulty ⇒ Object (readonly)
Returns the value of attribute current_difficulty.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def current_difficulty @current_difficulty end |
#hinted_numbers ⇒ Object (readonly)
Returns the value of attribute hinted_numbers.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def hinted_numbers @hinted_numbers 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 |
#win ⇒ Object (readonly)
Returns the value of attribute win.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def win @win end |
Instance Method Details
#check_number_match(guess) ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/codebreaker/game.rb', line 21 def check_number_match(guess) @guess = guess @attempts_counter += 1 if @guess == @secret_code @win = true return win_string end number_match(exact_match) end |
#difficulty(difficulty) ⇒ Object
16 17 18 19 |
# File 'lib/codebreaker/game.rb', line 16 def difficulty(difficulty) @current_difficulty = difficulty.level @hinted_numbers = @secret_code.sample(@current_difficulty[:hints]) end |
#lose? ⇒ Boolean
40 41 42 |
# File 'lib/codebreaker/game.rb', line 40 def lose? @attempts_counter >= @current_difficulty[:attempts] end |
#take_hint ⇒ Object
32 33 34 |
# File 'lib/codebreaker/game.rb', line 32 def take_hint @hinted_numbers.pop end |
#win?(string) ⇒ Boolean
36 37 38 |
# File 'lib/codebreaker/game.rb', line 36 def win?(string) string == win_string end |