Class: Codebreaker::Game

Inherits:
Object
  • Object
show all
Includes:
Validation
Defined in:
lib/codebreaker.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

Returns a new instance of Game.



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

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.



16
17
18
# File 'lib/codebreaker.rb', line 16

def attempts_used
  @attempts_used
end

#cluesObject (readonly)

Returns the value of attribute clues.



16
17
18
# File 'lib/codebreaker.rb', line 16

def clues
  @clues
end

#dateObject (readonly)

Returns the value of attribute date.



16
17
18
# File 'lib/codebreaker.rb', line 16

def date
  @date
end

#difficultyObject (readonly)

Returns the value of attribute difficulty.



16
17
18
# File 'lib/codebreaker.rb', line 16

def difficulty
  @difficulty
end

#hints_usedObject (readonly)

Returns the value of attribute hints_used.



16
17
18
# File 'lib/codebreaker.rb', line 16

def hints_used
  @hints_used
end

#userObject (readonly)

Returns the value of attribute user.



16
17
18
# File 'lib/codebreaker.rb', line 16

def user
  @user
end

#very_secret_codeObject (readonly)

Returns the value of attribute very_secret_code.



16
17
18
# File 'lib/codebreaker.rb', line 16

def very_secret_code
  @very_secret_code
end

Instance Method Details

#attemptsObject



76
77
78
# File 'lib/codebreaker.rb', line 76

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

#guess(user_guess) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/codebreaker.rb', line 49

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

Returns:

  • (Boolean)


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

def lost?
  @attempts_used >= @attempts
end

#number_of_hintsObject



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

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

#save_gameObject



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

def save_game
  FileLoader.new.save(self)
end

#show_hintObject



57
58
59
60
61
62
# File 'lib/codebreaker.rb', line 57

def show_hint
  validate_hints(hints_used, number_of_hints)

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

#start_new_gameObject



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

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

Returns:

  • (Boolean)


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

def won?
  @user_guess.nil?
end