class Codebreaker::Game
Constants
- COMPLEXITIES
- LEVELS
- LOSE
- WIN
Attributes
attempts[R]
cb_numbers[R]
cb_numbers_copy[R]
hints[R]
level[R]
name[R]
Public Class Methods
new(name, complexity)
click to toggle source
# File lib/entities/game.rb, line 14 def initialize(name, complexity) @name = name @level = complexity.to_sym generate_hints_attempts(@level) @cb_numbers = Core.new.generate_secret_code @cb_numbers_copy = @cb_numbers.clone end
Public Instance Methods
calculate_attempts()
click to toggle source
# File lib/entities/game.rb, line 47 def calculate_attempts take_level_hash[:attempts] - @attempts end
calculate_hints()
click to toggle source
# File lib/entities/game.rb, line 43 def calculate_hints take_level_hash[:hints] - @hints end
generate_hints_attempts(level)
click to toggle source
# File lib/entities/game.rb, line 22 def generate_hints_attempts(level) level_hash = take_level_hash @hints = level_hash[:hints] @attempts = level_hash[:attempts] end
guess(user_guess)
click to toggle source
# File lib/entities/game.rb, line 28 def guess(user_guess) @attempts -= 1 user_guess_array = user_guess.split('').map(&:to_i) return WIN if win? user_guess_array return LOSE if lose? Core.new.check(@cb_numbers, user_guess_array) end
hint()
click to toggle source
# File lib/entities/game.rb, line 36 def hint return nil if @hints.zero? @hints -= 1 @cb_numbers_copy.shuffle.pop end
take_level_hash()
click to toggle source
# File lib/entities/game.rb, line 52 def take_level_hash COMPLEXITIES[@level] end
Private Instance Methods
lose?()
click to toggle source
# File lib/entities/game.rb, line 62 def lose? @attempts.zero? end
save_result()
click to toggle source
# File lib/entities/game.rb, line 66 def save_result data = restore_storage data << { name: @name, attempts: calculate_attempts, hints: calculate_hints, level: @level, date: take_date } save(data) end
take_date()
click to toggle source
# File lib/entities/game.rb, line 72 def take_date Time.now.strftime("%d/%m/%Y %H:%M") end
win?(user_guess_array)
click to toggle source
# File lib/entities/game.rb, line 58 def win?(user_guess_array) save_result if user_guess_array == @cb_numbers end