Class: Codebreaker
- Inherits:
-
Object
- Object
- Codebreaker
- Defined in:
- lib/codebreaker.rb,
lib/codebreaker/version.rb
Constant Summary collapse
- VERSION =
'2.0.0'.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.
Instance Method Summary collapse
- #check_code_and_give_result(user_code) ⇒ Object
- #give_hint ⇒ Object
-
#initialize ⇒ Codebreaker
constructor
A new instance of Codebreaker.
- #load_game_results ⇒ Object
- #save_game_result ⇒ Object
- #start_game(difficulty, player_name) ⇒ Object
Constructor Details
#initialize ⇒ Codebreaker
Returns a new instance of Codebreaker.
6 7 8 |
# File 'lib/codebreaker.rb', line 6 def initialize @data_base_manager = DataBaseManager.new end |
Instance Attribute Details
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
4 5 6 |
# File 'lib/codebreaker.rb', line 4 def attempts @attempts end |
#hints ⇒ Object (readonly)
Returns the value of attribute hints.
4 5 6 |
# File 'lib/codebreaker.rb', line 4 def hints @hints end |
#secret_code ⇒ Object (readonly)
Returns the value of attribute secret_code.
4 5 6 |
# File 'lib/codebreaker.rb', line 4 def secret_code @secret_code end |
Instance Method Details
#check_code_and_give_result(user_code) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/codebreaker.rb', line 19 def check_code_and_give_result(user_code) answer = @verifier.verify_user_code(user_code) @attempts -= 1 if answer == '++++' "#{answer} #{Locale::PLAYER_WIN}" elsif @attempts <= 0 "#{answer} #{Locale::PLAYER_LOSE}" else answer end end |
#give_hint ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/codebreaker.rb', line 32 def give_hint if @hints <= 0 Locale::NO_MORE_HINTS_AVAILABLE else @hints -= 1 hint = @code_for_hints.sample @code_for_hints.delete_at(@code_for_hints.index(hint)) hint end end |
#load_game_results ⇒ Object
51 52 53 |
# File 'lib/codebreaker.rb', line 51 def load_game_results @data_base_manager.load_game_results end |
#save_game_result ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/codebreaker.rb', line 43 def save_game_result attempts_used = @total_attempts - @attempts hints_used = @total_hints - @hints @data_base_manager.save_game_results([@player_name, @difficulty, @total_attempts, attempts_used, @total_hints, hints_used]) end |
#start_game(difficulty, player_name) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/codebreaker.rb', line 10 def start_game(difficulty, player_name) @difficulty = difficulty @player_name = player_name init_difficulty_stats(difficulty) @hints = @total_hints @attempts = @total_attempts generate_secret_code end |