Class: Codebreaker::Game

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

Constant Summary collapse

TIME_FORMAT =
'%d %b %Y - %H:%M:%S'
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.



32
33
34
35
# File 'lib/codebreaker/game.rb', line 32

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

Instance Attribute Details

#attempts_availableObject (readonly)

Returns the value of attribute attempts_available.



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

def attempts_available
  @attempts_available
end

#attempts_totalObject (readonly)

Returns the value of attribute attempts_total.



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

def attempts_total
  @attempts_total
end

#attempts_usedObject (readonly)

Returns the value of attribute attempts_used.



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

def attempts_used
  @attempts_used
end

#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

#hint_keeperObject (readonly)

Returns the value of attribute hint_keeper.



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

def hint_keeper
  @hint_keeper
end

#hintsObject (readonly)

Returns the value of attribute hints.



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

def hints
  @hints
end

#hints_availableObject (readonly)

Returns the value of attribute hints_available.



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

def hints_available
  @hints_available
end

#hints_totalObject (readonly)

Returns the value of attribute hints_total.



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

def hints_total
  @hints_total
end

#hints_usedObject (readonly)

Returns the value of attribute hints_used.



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

def hints_used
  @hints_used
end

#resultObject (readonly)

Returns the value of attribute result.



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

def result
  @result
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

#attempts_calculationsObject



79
80
81
82
83
# File 'lib/codebreaker/game.rb', line 79

def attempts_calculations
  @attempts_total = LEVELS.dig(@difficulty.to_sym, :attempts)
  @attempts_used = LEVELS.dig(@difficulty.to_sym, :attempts) - @attempts
  @attempts_available = @attempts_total - @attempts_used
end

#guess(input) ⇒ Object



37
38
39
40
41
# File 'lib/codebreaker/game.rb', line 37

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

#hintObject



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

def hint
  @hint_keeper << tip = @hints.pop
  tip
end

#hints_calculationsObject



85
86
87
88
89
# File 'lib/codebreaker/game.rb', line 85

def hints_calculations
  @hints_total = LEVELS.dig(@difficulty.to_sym, :hints)
  @hints_used = LEVELS.dig(@difficulty.to_sym, :hints) - @hints.size
  @hints_available = @hints_total - @hints_used
end

#hints_left?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/codebreaker/game.rb', line 66

def hints_left?
  @hints.any?
end

#lost?Boolean

Returns:

  • (Boolean)


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

def lost?
  @attempts.zero?
end

#set(input) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/codebreaker/game.rb', line 70

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)
  @hint_keeper = []
end

#startObject



43
44
45
46
47
48
49
50
51
# File 'lib/codebreaker/game.rb', line 43

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

#to_h(name) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/codebreaker/game.rb', line 91

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,
    time: Time.now.strftime(TIME_FORMAT)
  }
end

#win?Boolean

Returns:

  • (Boolean)


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

def win?
  @result == MATCH * CODE_LENGTH
end