Class: Codebreaker::Game

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

Constant Summary

Constants included from Constants::Shared

Constants::Shared::CORRECT_RANGE, Constants::Shared::LENGTH_GOOD, Constants::Shared::REGULAR_FOR_CODE, Constants::Shared::TYPE_OF_DIFFICULTY, Constants::Shared::WINNING_INDEX

Constants included from Validations

Validations::ERROR_GUESS, Validations::ERROR_NAME, Validations::MAX_LENGTH, Validations::MIN_LENGTH

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validations

#validate_for_name

Constructor Details

#initialize(user:, type_of_difficulty:) ⇒ Game

Returns a new instance of Game.



10
11
12
13
14
15
16
17
18
# File 'lib/codebreaker/game.rb', line 10

def initialize(user:, type_of_difficulty:)
  @win = false
  @user = user
  @secret_code = RandomSecretCode.call
  @attempts = TYPE_OF_DIFFICULTY[type_of_difficulty][:attempts]
  @hints = TYPE_OF_DIFFICULTY[type_of_difficulty][:hints]
  @secret_code_for_hints = @secret_code.shuffle
  @type = type_of_difficulty
end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



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

def attempts
  @attempts
end

#hintsObject (readonly)

Returns the value of attribute hints.



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

def hints
  @hints
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

#userObject (readonly)

Returns the value of attribute user.



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

def user
  @user
end

#winObject (readonly)

Returns the value of attribute win.



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

def win
  @win
end

Class Method Details

.type_of_difficulty_validate?(difficulty) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.type_of_difficulty_validate?(difficulty)
  TYPE_OF_DIFFICULTY.key?(difficulty.to_sym)
end

.user_validate_in_game?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.user_validate_in_game?(user)
  user.is_a?(User)
end

Instance Method Details

#give_hintsObject



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

def give_hints
  @hints.positive? ? check_hints : raise(StandardError, I18n.t(:message_for_hints))
end

#my_guess(guess) ⇒ Object

Raises:

  • (StandardError)


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

def my_guess(guess)
  validate_for_guess(guess)
  raise(StandardError, I18n.t(:message_for_attempts)) unless @attempts.positive?

  @result = GuessChecker.new(guess.chars, @secret_code).check_guess
  @attempts -= 1
  @win = won?
  @result
end