Class: Codebreaker::Game

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

Constant Summary collapse

DIFFICULTIES =
{
  easy: { attempts: 15, hints: 2 },
  medium: { attempts: 10, hints: 1 },
  hell: { attempts: 5, hints: 1 }
}.freeze
CODE_LENGTH =
4
RANGE_GUESS_CODE =
(1..6).freeze

Constants included from Validation

Validation::ERROR_MESSAGES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validation

#raise_error, #validate_difficulty, #validate_file_existens, #validate_guess, #validate_hints, #validate_user_name

Constructor Details

#initialize(difficulty:, user:, date: Date.today) ⇒ Game



20
21
22
23
24
25
26
27
28
29
# File 'lib/codebreaker/game.rb', line 20

def initialize(difficulty:, user:, date: Date.today)
  validate_difficulty(difficulty, DIFFICULTIES)

  @user = user
  @difficulty = difficulty
  @date = date

  attempts
  number_of_hints
end

Instance Attribute Details

#attempts_usedObject (readonly)

Returns the value of attribute attempts_used.



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

def attempts_used
  @attempts_used
end

#cluesObject (readonly)

Returns the value of attribute clues.



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

def clues
  @clues
end

#dateObject (readonly)

Returns the value of attribute date.



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

def date
  @date
end

#difficultyObject (readonly)

Returns the value of attribute difficulty.



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

def difficulty
  @difficulty
end

#hints_usedObject (readonly)

Returns the value of attribute hints_used.



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

def hints_used
  @hints_used
end

#userObject (readonly)

Returns the value of attribute user.



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

def user
  @user
end

#very_secret_codeObject (readonly)

Returns the value of attribute very_secret_code.



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

def very_secret_code
  @very_secret_code
end

Instance Method Details

#attemptsObject



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

def attempts
  @attempts ||= DIFFICULTIES[difficulty.to_sym][:attempts]
end

#guess(user_guess) ⇒ Object



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

def guess(user_guess)
  guess = user_guess.each_char.map(&:to_i)
  validate_guess(guess, CODE_LENGTH, RANGE_GUESS_CODE)
  check_guess(guess, very_secret_code)

  increase_attempts
end

#lost?Boolean



59
60
61
# File 'lib/codebreaker/game.rb', line 59

def lost?
  @attempts_used >= @attempts
end

#number_of_hintsObject



71
72
73
# File 'lib/codebreaker/game.rb', line 71

def number_of_hints
  @number_of_hints ||= DIFFICULTIES[difficulty.to_sym][:hints]
end

#save_gameObject



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

def save_game
  SaveRes.new.save(self)
end

#show_hintObject



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

def show_hint
  validate_hints(hints_used, number_of_hints)

  @hints_used += 1
  @hints.shuffle!.pop
end

#start_new_gameObject



31
32
33
34
35
36
37
38
# File 'lib/codebreaker/game.rb', line 31

def start_new_game
  @very_secret_code = generate_random_code
  @hints = @very_secret_code.clone
  @attempts_used = 0
  @hints_used = 0
  @user_guess = []
  @clues = []
end

#won?Boolean



55
56
57
# File 'lib/codebreaker/game.rb', line 55

def won?
  @user_guess.nil?
end