Class: CodebreakerGem::Game

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

Constant Summary collapse

SECRET_CODE_LENGTH =
4
ELEMENT_VALUE_RANGE =
(1..6).freeze
DELIMITER =
''
EXACT_MATCH =
'+'
NUMBER_MATCH =
'-'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(difficulty) ⇒ Game

Returns a new instance of Game.



14
15
16
17
18
19
20
21
22
# File 'lib/app/entities/game.rb', line 14

def initialize(difficulty)
  @difficulty = difficulty[:level]
  @total_attempts = difficulty[:attempts]
  @used_attempts = 0
  @total_hints = difficulty[:hints]
  @used_hints = 0
  @secret_code = generate
  @shuffled_code = @secret_code.shuffle
end

Instance Attribute Details

#difficultyObject (readonly)

Returns the value of attribute difficulty.



5
6
7
# File 'lib/app/entities/game.rb', line 5

def difficulty
  @difficulty
end

#secret_codeObject (readonly)

Returns the value of attribute secret_code.



5
6
7
# File 'lib/app/entities/game.rb', line 5

def secret_code
  @secret_code
end

#total_attemptsObject (readonly)

Returns the value of attribute total_attempts.



5
6
7
# File 'lib/app/entities/game.rb', line 5

def total_attempts
  @total_attempts
end

#total_hintsObject (readonly)

Returns the value of attribute total_hints.



5
6
7
# File 'lib/app/entities/game.rb', line 5

def total_hints
  @total_hints
end

#used_attemptsObject (readonly)

Returns the value of attribute used_attempts.



5
6
7
# File 'lib/app/entities/game.rb', line 5

def used_attempts
  @used_attempts
end

#used_hintsObject (readonly)

Returns the value of attribute used_hints.



5
6
7
# File 'lib/app/entities/game.rb', line 5

def used_hints
  @used_hints
end

Instance Method Details

#hints_available?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/app/entities/game.rb', line 29

def hints_available?
  @used_hints >= @total_hints
end

#increment_used_attemptsObject



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

def increment_used_attempts
  @used_attempts += 1
end

#loss?Boolean

Returns:

  • (Boolean)


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

def loss?
  @used_attempts == @total_attempts
end

#mark_guess(guess_code) ⇒ Object



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

def mark_guess(guess_code)
  @converted_input = convert_to_digit_array(guess_code)
  @cloned_code = @secret_code.clone
  convert_to_string(exact_match.compact + number_match.compact)
end

#use_hintObject



24
25
26
27
# File 'lib/app/entities/game.rb', line 24

def use_hint
  increment_used_hints
  @shuffled_code.shift
end

#win?(guess_code) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/app/entities/game.rb', line 33

def win?(guess_code)
  guess_code == convert_to_string(@secret_code)
end