Class: CodebreakerRostik::Game

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

Constant Summary collapse

LENGTH_GUESS_CODE =
4
RANGE_GUESS_CODE =
(1..6).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validator

#valid_length?, #validate_class?, #validate_each_char_in_range?, #validate_length_range?

Constructor Details

#initializeGame

Returns a new instance of Game.



9
10
11
12
13
14
# File 'lib/game.rb', line 9

def initialize
  @hints_left = 0
  @attempts_left = 0
  @secret_code = generate_secrete_code
  @secret_code_for_hints = @secret_code.clone.shuffle
end

Instance Attribute Details

#attempts_leftObject (readonly)

Returns the value of attribute attempts_left.



4
5
6
# File 'lib/game.rb', line 4

def attempts_left
  @attempts_left
end

#hints_leftObject (readonly)

Returns the value of attribute hints_left.



4
5
6
# File 'lib/game.rb', line 4

def hints_left
  @hints_left
end

#secret_codeObject (readonly)

Returns the value of attribute secret_code.



4
5
6
# File 'lib/game.rb', line 4

def secret_code
  @secret_code
end

#secret_code_for_hintsObject (readonly)

Returns the value of attribute secret_code_for_hints.



4
5
6
# File 'lib/game.rb', line 4

def secret_code_for_hints
  @secret_code_for_hints
end

Instance Method Details

#attempts_left_increase(user) ⇒ Object



25
26
27
28
# File 'lib/game.rb', line 25

def attempts_left_increase(user)
  user[:attempts_left] += 1
  @attempts_left += 1
end

#compare_guess_and_secret_codes(guess_code) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/game.rb', line 35

def compare_guess_and_secret_codes(guess_code)
  @result_signs = ''
  double_secret_code = @secret_code.clone
  code_arr = guess_code.split('').map(&:to_i)
  double_guess_code = code_arr

  check_same_index(code_arr, double_secret_code, double_guess_code)
  [double_secret_code, double_guess_code].each(&:compact!)
  check_different_index(double_guess_code, double_secret_code)
  @result_signs
end

#give_digit_hintObject



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

def give_digit_hint
  @hints_left += 1
  @secret_code_for_hints.pop
end

#hints_left_increase(user) ⇒ Object



20
21
22
23
# File 'lib/game.rb', line 20

def hints_left_increase(user)
  user[:hints_left] += 1
  @hints_left += 1
end

#valid_guess_code?(guess_code) ⇒ Boolean

Returns:

  • (Boolean)


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

def valid_guess_code?(guess_code)
  validate_each_char_in_range?(guess_code.split(''), RANGE_GUESS_CODE) && valid_length?(guess_code, LENGTH_GUESS_CODE)
end