class GemCodebreaker::Game
Constants
- DIGIT_RANGE
- LENGTH_OF_SECRET_CODE
Attributes
config[R]
matcher_hash[R]
statistic[R]
Public Class Methods
new(user, level)
click to toggle source
# File lib/gem_codebreaker/classes/game.rb, line 11 def initialize(user, level) @config = GameConfig.new(user, level) @statistic = [] secrete_code end
Public Instance Methods
game_over?()
click to toggle source
# File lib/gem_codebreaker/classes/game.rb, line 32 def game_over? @config.attempts_used? && win_game? == false end
guess_start(input_code)
click to toggle source
# File lib/gem_codebreaker/classes/game.rb, line 17 def guess_start(input_code) guess_valid?(input_code) raise GemCodebreaker::NoAttemptsError if @config.attempts_used? @config.increase_user_attempts user_code = input_code.chars.map(&:to_i) code_marker = CodeMaker.new(@secrete_code, user_code) code_marker.calculate_result_code @matcher_hash = { plus: code_marker.plus_match, minus: code_marker.minus_match, empty: code_marker.empty_match } end
hint()
click to toggle source
# File lib/gem_codebreaker/classes/game.rb, line 40 def hint raise GemCodebreaker::NoHintsError if @config.hints_used? @config.increase_user_hints @secrete_code.sample end
restart_game()
click to toggle source
# File lib/gem_codebreaker/classes/game.rb, line 47 def restart_game @config.reset_user_attempts_hints @secrete_code = false secrete_code end
save_user_statistic()
click to toggle source
# File lib/gem_codebreaker/classes/game.rb, line 53 def save_user_statistic @statistic << GameStatistic.new(self) end
show_secrete_code()
click to toggle source
# File lib/gem_codebreaker/classes/game.rb, line 36 def show_secrete_code @secrete_code if win_game? || game_over? end
win_game?()
click to toggle source
# File lib/gem_codebreaker/classes/game.rb, line 28 def win_game? @matcher_hash[:plus] == LENGTH_OF_SECRET_CODE end
Private Instance Methods
guess_valid?(user_code)
click to toggle source
# File lib/gem_codebreaker/classes/game.rb, line 59 def guess_valid?(user_code) validate_class(user_code, String) raise GemCodebreaker::LengthError, I18n.t('errors.incorrect_length') if user_code.length != LENGTH_OF_SECRET_CODE unless user_code[/\A[#{DIGIT_RANGE.first}-#{DIGIT_RANGE.last}]{#{LENGTH_OF_SECRET_CODE}}\z/] raise GemCodebreaker::ValueRangeError end true end
secrete_code()
click to toggle source
# File lib/gem_codebreaker/classes/game.rb, line 69 def secrete_code @secrete_code ||= (1..LENGTH_OF_SECRET_CODE).map { rand DIGIT_RANGE } end