Class: Game
- Inherits:
-
Object
- Object
- Game
- Defined in:
- lib/game.rb
Instance Attribute Summary collapse
-
#attempts ⇒ Object
Returns the value of attribute attempts.
-
#difficulty_name ⇒ Object
Returns the value of attribute difficulty_name.
-
#hints ⇒ Object
Returns the value of attribute hints.
-
#player_name ⇒ Object
Returns the value of attribute player_name.
-
#secret_code ⇒ Object
Returns the value of attribute secret_code.
-
#secret_code_for_hint ⇒ Object
readonly
Returns the value of attribute secret_code_for_hint.
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
-
#user_code ⇒ Object
Returns the value of attribute user_code.
Instance Method Summary collapse
- #generate_code ⇒ Object
-
#initialize(player_name, difficulty) ⇒ Game
constructor
A new instance of Game.
- #lost? ⇒ Boolean
- #result(response) ⇒ Object
- #run ⇒ Object
- #save_result ⇒ Object
- #use_hint ⇒ Object
- #won?(result) ⇒ Boolean
Constructor Details
#initialize(player_name, difficulty) ⇒ Game
Returns a new instance of Game.
7 8 9 10 11 12 13 14 15 |
# File 'lib/game.rb', line 7 def initialize(player_name, difficulty) @stats = Statistic.stats @player_name = player_name @difficulty_name = difficulty @attempts = DIFFICULTIES[difficulty.to_sym][:attempts] @hints = DIFFICULTIES[difficulty.to_sym][:hints] @db = DB @secret_code_for_hint = [] end |
Instance Attribute Details
#attempts ⇒ Object
Returns the value of attribute attempts.
4 5 6 |
# File 'lib/game.rb', line 4 def attempts @attempts end |
#difficulty_name ⇒ Object
Returns the value of attribute difficulty_name.
4 5 6 |
# File 'lib/game.rb', line 4 def difficulty_name @difficulty_name end |
#hints ⇒ Object
Returns the value of attribute hints.
4 5 6 |
# File 'lib/game.rb', line 4 def hints @hints end |
#player_name ⇒ Object
Returns the value of attribute player_name.
4 5 6 |
# File 'lib/game.rb', line 4 def player_name @player_name end |
#secret_code ⇒ Object
Returns the value of attribute secret_code.
4 5 6 |
# File 'lib/game.rb', line 4 def secret_code @secret_code end |
#secret_code_for_hint ⇒ Object (readonly)
Returns the value of attribute secret_code_for_hint.
5 6 7 |
# File 'lib/game.rb', line 5 def secret_code_for_hint @secret_code_for_hint end |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
5 6 7 |
# File 'lib/game.rb', line 5 def stats @stats end |
#user_code ⇒ Object
Returns the value of attribute user_code.
4 5 6 |
# File 'lib/game.rb', line 4 def user_code @user_code end |
Instance Method Details
#generate_code ⇒ Object
27 28 29 |
# File 'lib/game.rb', line 27 def generate_code Array.new(4) { rand(1..6) } end |
#lost? ⇒ Boolean
48 49 50 |
# File 'lib/game.rb', line 48 def lost? attempts.zero? end |
#result(response) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/game.rb', line 31 def result(response) @user_code = response.each_char.map(&:to_i) @attempts -= 1 return WINNING_RESULT if @secret_code == user_code Matching.new(self).create_response end |
#run ⇒ Object
22 23 24 25 |
# File 'lib/game.rb', line 22 def run @secret_code = generate_code @secret_code_for_hint = @secret_code.clone end |
#save_result ⇒ Object
39 40 41 42 |
# File 'lib/game.rb', line 39 def save_result @stats.push(Statistic.generate_stats) DbUtils.add(@db, stats) end |
#use_hint ⇒ Object
17 18 19 20 |
# File 'lib/game.rb', line 17 def use_hint @hints -= 1 secret_code_for_hint.sort_by! { rand }.pop end |
#won?(result) ⇒ Boolean
44 45 46 |
# File 'lib/game.rb', line 44 def won?(result) result == WINNING_RESULT end |