class Codebreaker::Guess

Attributes

errors[R]
value[R]

Public Class Methods

decorate(value, strict_match = '+', soft_match = '-') click to toggle source
# File lib/codebreaker/guess.rb, line 17
def self.decorate(value, strict_match = '+', soft_match = '-')
  value.gsub(Codebreaker::Game::EXACT_MATCH_SIGN, strict_match)
       .gsub(Codebreaker::Game::NOT_EXACT_MATCH_SIGN, soft_match)
end
new(value) click to toggle source
# File lib/codebreaker/guess.rb, line 7
def initialize(value)
  @value = value
  @errors = []
end

Public Instance Methods

valid?() click to toggle source
# File lib/codebreaker/guess.rb, line 12
def valid?
  assert
  errors.empty?
end

Private Instance Methods

assert() click to toggle source
# File lib/codebreaker/guess.rb, line 24
def assert
  validate_length
  validate_range if errors.empty?
end
validate_length() click to toggle source
# File lib/codebreaker/guess.rb, line 29
def validate_length
  error_message = I18n.t(:'errors.guess.length', length: Game::SECRET_CODE_LENGTH)
  errors << error_message unless @value.to_s.chars.length == Game::SECRET_CODE_LENGTH
end
validate_range() click to toggle source
# File lib/codebreaker/guess.rb, line 34
def validate_range
  error_message = I18n.t(:'errors.guess.range', range: Game::SECRET_CODE_RANGE)
  allowed_range = Game::SECRET_CODE_RANGE.to_a
  is_valid_range = @value.to_s.chars.map(&:to_i).all? { |n| allowed_range.include? n }
  errors << error_message unless is_valid_range
end