Class: Codebreaker::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/codebreaker.rb

Constant Summary collapse

STATE =
{
  continue: 'continue',
  win: 'win',
  lose: 'lose'
}.freeze
VALID_NUMBERS =
(1..6).freeze
LENGTH_CODE =
4
CORRECT_SYMBOL =
'+'
INCORRECT_SYMBOL =
'-'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(player, difficulties) ⇒ Game

Returns a new instance of Game.



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

def initialize(player, difficulties)
  @player = player
  @difficulties = difficulties
  @attempts = @difficulties.attempts
  @hints = @difficulties.hints
  @number = generator_secret_code
  @available_hints = @number.clone
  @state = STATE[:continue]
end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



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

def attempts
  @attempts
end

#difficultiesObject (readonly)

Returns the value of attribute difficulties.



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

def difficulties
  @difficulties
end

#hintsObject (readonly)

Returns the value of attribute hints.



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

def hints
  @hints
end

#numberObject (readonly)

Returns the value of attribute number.



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

def number
  @number
end

#playerObject (readonly)

Returns the value of attribute player.



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

def player
  @player
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

Instance Method Details

#check_code(code) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/codebreaker.rb', line 33

def check_code(code)
  return nil unless valid_code?(code)

  temp_code = code.chars
  guess_number = Array.new(LENGTH_CODE) { 0 }
  calc_and_mark_correct(temp_code, guess_number) + calc_and_mark_incorrect(temp_code, guess_number)
end

#decrease_attempts!Object



71
72
73
# File 'lib/codebreaker.rb', line 71

def decrease_attempts!
  @attempts -= 1
end

#hintObject



28
29
30
31
# File 'lib/codebreaker.rb', line 28

def hint
  @hints -= 1
  @available_hints.delete_at(rand(@available_hints.size))
end

#hints?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/codebreaker.rb', line 60

def hints?
  !@hints.zero?
end

#lose?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
# File 'lib/codebreaker.rb', line 64

def lose?
  return false unless @attempts.zero?

  @state = STATE[:lose]
  true
end

#to_hObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/codebreaker.rb', line 41

def to_h
  {
    player: @player.name,
    difficulties: @difficulties.level,
    attempts_total: @difficulties.attempts,
    hints_total: @difficulties.hints,
    attempts_used: @difficulties.attempts - @attempts,
    hints_used: @difficulties.hints - @hints,
    state: @state
  }
end

#win?(code) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
# File 'lib/codebreaker.rb', line 53

def win?(code)
  return false unless code == @number.join

  @state = STATE[:win]
  true
end