Class: Codebreaker::Game

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

Constant Summary collapse

INCREMENT =
1
AMOUNT_DIGITS =
4
POSITIVE_DIGIT =
'+'.freeze
NEGATIVE_DIGIT =
'-'.freeze
DIFFICULTIES =
{
  easy: { attempts: 15, hints: 2, difficulty: 'easy' },
  hard: { attempts: 10, hints: 2, difficulty: 'hard' },
  expert: { attempts: 5, hints: 1, difficulty: 'expert' }
}.freeze
RANGE_OF_DIGITS =
1..6.freeze
GUESS_CODE =
{ hint: 'hint', leave: 'exit' }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validation

#valid_digits?, #valid_name?, #validate_in_range?, #validate_length, #validate_match, #validate_presence?

Instance Attribute Details

#attempts_leftObject (readonly)

Returns the value of attribute attempts_left.



17
18
19
# File 'lib/entities/game.rb', line 17

def attempts_left
  @attempts_left
end

#attempts_totalObject (readonly)

Returns the value of attribute attempts_total.



17
18
19
# File 'lib/entities/game.rb', line 17

def attempts_total
  @attempts_total
end

#attempts_usedObject (readonly)

Returns the value of attribute attempts_used.



17
18
19
# File 'lib/entities/game.rb', line 17

def attempts_used
  @attempts_used
end

#dateObject (readonly)

Returns the value of attribute date.



17
18
19
# File 'lib/entities/game.rb', line 17

def date
  @date
end

#difficultyObject (readonly)

Returns the value of attribute difficulty.



17
18
19
# File 'lib/entities/game.rb', line 17

def difficulty
  @difficulty
end

#errorsObject

Returns the value of attribute errors.



18
19
20
# File 'lib/entities/game.rb', line 18

def errors
  @errors
end

#got_hintsObject (readonly)

Returns the value of attribute got_hints.



17
18
19
# File 'lib/entities/game.rb', line 17

def got_hints
  @got_hints
end

#have_hintsObject (readonly)

Returns the value of attribute have_hints.



17
18
19
# File 'lib/entities/game.rb', line 17

def have_hints
  @have_hints
end

#hints_totalObject (readonly)

Returns the value of attribute hints_total.



17
18
19
# File 'lib/entities/game.rb', line 17

def hints_total
  @hints_total
end

#hints_usedObject (readonly)

Returns the value of attribute hints_used.



17
18
19
# File 'lib/entities/game.rb', line 17

def hints_used
  @hints_used
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/entities/game.rb', line 17

def name
  @name
end

#secret_codeObject (readonly)

Returns the value of attribute secret_code.



17
18
19
# File 'lib/entities/game.rb', line 17

def secret_code
  @secret_code
end

#winnerObject (readonly)

Returns the value of attribute winner.



17
18
19
# File 'lib/entities/game.rb', line 17

def winner
  @winner
end

Instance Method Details

#attempt(input) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/entities/game.rb', line 28

def attempt(input)
  @errors = []
  return use_hint if hint?(input)

  converted = convert_to_array(input)
  return guessing(converted) if valid_input?(input, AMOUNT_DIGITS)
  miss_input && return
end

#game_options(user_difficulty:, player:) ⇒ Object



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

def game_options(user_difficulty:, player:)
  @got_hints = ''
  @hints_used = 0
  @attempts_used = 0
  @name = player.name
  assign_difficulty(DIFFICULTIES[user_difficulty.downcase.to_sym])
end

#miss_inputObject



37
38
39
# File 'lib/entities/game.rb', line 37

def miss_input
  @errors << I18n.t(:when_incorrect_guess) && return
end

#remove_instance_helpersObject



45
46
47
48
49
50
# File 'lib/entities/game.rb', line 45

def remove_instance_helpers
  remove_instance_variable(:@winner) if @winner
  remove_instance_variable(:@errors) if @errors
  remove_instance_variable(:@hints_array) if @hints_array
  remove_instance_variable(:@have_hints) if @have_hints
end

#valid_difficulties?(input) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/entities/game.rb', line 41

def valid_difficulties?(input)
  DIFFICULTIES.key?(input.to_sym)
end