Module: Codebreaker::Validator

Included in:
Console, Wellcome
Defined in:
lib/modules/validator.rb

Constant Summary collapse

COMMANDS =
{ start: 'start', rules: 'rules', statistics: 'stat', hint: 'hint', exit: 'exit' }.freeze
NAME_SIZE =
(3..25).freeze
LEVELS =
{ easy: 'easy', medium: 'medium', hard: 'hard' }.freeze
CB_ARRAY_SIZE =
4
GAME_NUMBER_RANGE =
(1..6).freeze

Instance Method Summary collapse

Instance Method Details

#valid_input?(input, validator) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/modules/validator.rb', line 10

def valid_input?(input, validator)
  goodbye if input == COMMANDS[:exit]

  case validator
  when 'guess'
    return false unless input.size == CB_ARRAY_SIZE
    return true if input == COMMANDS[:hint]

    input.split('').map { |number| GAME_NUMBER_RANGE.cover?(number.to_i) }.all?
  when 'name'
    NAME_SIZE.include?(input.size)
  when 'navigation'
    COMMANDS.value?(input)
  when 'complexity'
    LEVELS.value?(input)
  else
    false
  end
end