Class: CodebreakerVk::Game

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

Constant Summary collapse

SECRET_CODE_LENGTH =
4
RANGE_START =
1
RANGE_END =
6
NOT_YET =
'-'
GOT_IT =
'+'
DIFFICULTY_LEVEL =
{
    easy: { attempts: 15, hints: 3 },
    medium: { attempts: 10, hints: 2 },
    hell: { attempts: 5, hints: 1 }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Output

#rules, #start_info, #summary_info

Constructor Details

#initialize(name:, difficulty:) ⇒ Game

Returns a new instance of Game.



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

def initialize(name:, difficulty:)
  @name = name
  @difficulty = difficulty
  @attempts = DIFFICULTY_LEVEL[difficulty][:attempts]
  @hints = DIFFICULTY_LEVEL[difficulty][:hints]
  @secret = make_number
  @unused_hints = @secret.chars
end

Instance Attribute Details

#attemptsObject

Returns the value of attribute attempts.



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

def attempts
  @attempts
end

#attempts_totalObject

Returns the value of attribute attempts_total.



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

def attempts_total
  @attempts_total
end

#difficultyObject

Returns the value of attribute difficulty.



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

def difficulty
  @difficulty
end

#hintsObject

Returns the value of attribute hints.



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

def hints
  @hints
end

#hints_totalObject

Returns the value of attribute hints_total.



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

def hints_total
  @hints_total
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#secretObject

Returns the value of attribute secret.



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

def secret
  @secret
end

Instance Method Details

#check(number) ⇒ Object



32
33
34
35
# File 'lib/codebreaker_vk/game.rb', line 32

def check(number)
  @attempts -= 1
  @last_result = check_numbers(@secret.chars, number.chars)
end

#make_number(numbers = RANGE_END) ⇒ Object



28
29
30
# File 'lib/codebreaker_vk/game.rb', line 28

def make_number(numbers = RANGE_END)
  (1..SECRET_CODE_LENGTH).map { rand(RANGE_START..numbers) }.join
end

#use_hintObject



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

def use_hint
  return I18n.t(:no_hints) unless @hints.positive?

  @hints -= 1
  hint(@unused_hints)
end

#win?Boolean

Returns:

  • (Boolean)


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

def win?
  @last_result == GOT_IT * SECRET_CODE_LENGTH
end