Class: Codebreaker::Game

Inherits:
Object
  • Object
show all
Includes:
Validations
Defined in:
lib/codebreaker/game.rb

Constant Summary collapse

CODE_DIGITS =
(1..6).freeze
CODE_LENGTH =
4
MATCH =
'+'
PRESENCE =
'-'
LEVELS =
{
  silly: {
    attempts: 15,
    hints: 2,
    command: 'silly'
  },
  moderate: {
    attempts: 10,
    hints: 1,
    command: 'moderate'
  },
  clever: {
    attempts: 5,
    hints: 1,
    command: 'clever'
  }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validations

#match_checker, #range_checker

Constructor Details

#initializeGame

Returns a new instance of Game.



30
31
32
33
# File 'lib/codebreaker/game.rb', line 30

def initialize
  @secret_code = Array.new(CODE_LENGTH) { rand(CODE_DIGITS) }
  @errors = []
end

Instance Attribute Details

#difficultyObject (readonly)

Returns the value of attribute difficulty.



6
7
8
# File 'lib/codebreaker/game.rb', line 6

def difficulty
  @difficulty
end

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'lib/codebreaker/game.rb', line 6

def errors
  @errors
end

#secret_codeObject (readonly)

Returns the value of attribute secret_code.



6
7
8
# File 'lib/codebreaker/game.rb', line 6

def secret_code
  @secret_code
end

Instance Method Details

#guess(input) ⇒ Object



35
36
37
38
39
# File 'lib/codebreaker/game.rb', line 35

def guess(input)
  user_number = input.chars.map(&:to_i)
  validate(user_number)
  @user_number = user_number
end

#hintObject



57
58
59
# File 'lib/codebreaker/game.rb', line 57

def hint
  @hints.pop
end

#hints_left?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/codebreaker/game.rb', line 61

def hints_left?
  @hints.any?
end

#lost?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/codebreaker/game.rb', line 53

def lost?
  @attempts.zero?
end

#set(input) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/codebreaker/game.rb', line 65

def set(input)
  return unless LEVELS.key?(input.to_sym)

  @difficulty = LEVELS.dig(input.to_sym, :command)
  @hints = @secret_code.sample(LEVELS.dig(@difficulty.to_sym, :hints))
  @attempts = LEVELS.dig(@difficulty.to_sym, :attempts)
end

#startObject



41
42
43
44
45
46
47
# File 'lib/codebreaker/game.rb', line 41

def start
  @code = @secret_code.dup
  @attempts -= 1
  matches_result = check_matches
  presence_result = check_presence
  @result = (matches_result + presence_result).join
end

#to_h(name) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/codebreaker/game.rb', line 73

def to_h(name)
  {
    name: name,
    difficulty: @difficulty,
    attempts_total: LEVELS.dig(@difficulty.to_sym, :attempts),
    attempts_used: (LEVELS.dig(@difficulty.to_sym, :attempts) - @attempts),
    hints_total: LEVELS.dig(@difficulty.to_sym, :hints),
    hints_used: (LEVELS.dig(@difficulty.to_sym, :hints) - @hints.size)
  }
end

#win?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/codebreaker/game.rb', line 49

def win?
  @result == MATCH * CODE_LENGTH
end