Class: Codebreaker::Game
- Inherits:
-
Object
- Object
- Codebreaker::Game
- Defined in:
- lib/codebreaker/game.rb
Constant Summary collapse
- DIFFICULTIES =
{ easy: { attempts: 30, hints: 3 }, medium: { attempts: 15, hints: 2 }, hard: { attempts: 10, hints: 1 } }.freeze
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
Returns the value of attribute attempts.
-
#hints ⇒ Object
readonly
Returns the value of attribute hints.
-
#secret_code ⇒ Object
readonly
Returns the value of attribute secret_code.
-
#user_code ⇒ Object
Returns the value of attribute user_code.
Instance Method Summary collapse
- #equal_codes?(user_answer) ⇒ Boolean
- #handle_guess(user_answer) ⇒ Object
-
#initialize(difficulty) ⇒ Game
constructor
A new instance of Game.
- #take_a_hint! ⇒ Object
- #valid_answer?(user_answer) ⇒ Boolean
Constructor Details
#initialize(difficulty) ⇒ Game
Returns a new instance of Game.
15 16 17 18 19 |
# File 'lib/codebreaker/game.rb', line 15 def initialize(difficulty) @secret_code = Array.new(4) { rand(1..6) } @attempts = DIFFICULTIES.dig(difficulty, :attempts) @hints = secret_code.shuffle.take(DIFFICULTIES.dig(difficulty, :hints)) end |
Instance Attribute Details
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
12 13 14 |
# File 'lib/codebreaker/game.rb', line 12 def attempts @attempts end |
#hints ⇒ Object (readonly)
Returns the value of attribute hints.
12 13 14 |
# File 'lib/codebreaker/game.rb', line 12 def hints @hints end |
#secret_code ⇒ Object (readonly)
Returns the value of attribute secret_code.
12 13 14 |
# File 'lib/codebreaker/game.rb', line 12 def secret_code @secret_code end |
#user_code ⇒ Object
Returns the value of attribute user_code.
13 14 15 |
# File 'lib/codebreaker/game.rb', line 13 def user_code @user_code end |
Instance Method Details
#equal_codes?(user_answer) ⇒ Boolean
36 37 38 |
# File 'lib/codebreaker/game.rb', line 36 def equal_codes?(user_answer) secret_code.join == user_answer end |
#handle_guess(user_answer) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/codebreaker/game.rb', line 21 def handle_guess(user_answer) @user_code = user_answer.each_char.map(&:to_i) handle_numbers @attempts -= 1 @round_result.empty? ? 'No matches' : @round_result end |
#take_a_hint! ⇒ Object
32 33 34 |
# File 'lib/codebreaker/game.rb', line 32 def take_a_hint! @hints.pop end |
#valid_answer?(user_answer) ⇒ Boolean
28 29 30 |
# File 'lib/codebreaker/game.rb', line 28 def valid_answer?(user_answer) user_answer =~ /^[1-6]{4}$/ end |