class Game
Attributes
attempts[RW]
difficulty_name[RW]
hints[RW]
player_name[RW]
secret_code[RW]
secret_code_for_hint[R]
stats[R]
user_code[RW]
Public Class Methods
new(player_name, difficulty)
click to toggle source
# 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
Public Instance Methods
generate_code()
click to toggle source
# File lib/game.rb, line 27 def generate_code Array.new(4) { rand(1..6) } end
lost?()
click to toggle source
# File lib/game.rb, line 48 def lost? attempts.zero? end
result(response)
click to toggle source
# 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()
click to toggle source
# File lib/game.rb, line 22 def run @secret_code = generate_code @secret_code_for_hint = @secret_code.clone end
save_result()
click to toggle source
# File lib/game.rb, line 39 def save_result @stats.push(Statistic.generate_stats) DbUtils.add(@db, stats) end
use_hint()
click to toggle source
# File lib/game.rb, line 17 def use_hint @hints -= 1 secret_code_for_hint.sort_by! { rand }.pop end
won?(result)
click to toggle source
# File lib/game.rb, line 44 def won?(result) result == WINNING_RESULT end